Unity3D 鼠标以及触屏移动、缩放控制器(相机)
来源:互联网 发布:电气设计软件 编辑:程序博客网 时间:2024/06/06 10:02
Unity3D 支持多平台的发布,但是平时在测试的过程中往往需要在各个平台之间进行切换,在平时开发中,对象的移动、舞台缩放使用鼠标控制比较方便,但是在安卓平台,鼠标就无法发挥作用了,只能使用触控操作,最近整理了两种控制器,适用于桌面以及安卓平台。
最终使用代码如下:
using UnityEngine;using System.Text;using System.Collections.Generic;public class ControllerObject : MonoBehaviour {public UILabel uiLabel;private PlatformController controller;private IList<string> textList;void Awake(){this.textList = new List<string> ();#if UNITY_STANDALONE_WINcontroller = this.gameObject.AddComponent<MouseController>();#elif UNITY_ANDROIDcontroller = this.gameObject.AddComponent<TouchController>();#endifcontroller.Init (OnBeginEndCallback, OnMoveCallback, OnScaleCallback, OnUpdateCallback, OnEndCallback);}private void OnBeginEndCallback(){this.uiLabel.text = "开始";}private void OnMoveCallback(Vector2 direction){if(this.textList.Count > 10){this.textList.RemoveAt(0);}this.textList.Add("移动:" + direction.x + ":" + direction.y);this.uiLabel.text = this.GetText ();}private void OnScaleCallback(float distance){if(this.textList.Count > 10){this.textList.RemoveAt(0);}this.textList.Add("缩放:" + distance);this.uiLabel.text = this.GetText ();}private void OnUpdateCallback(){}private void OnEndCallback(){this.uiLabel.text = "结束";}private string GetText(){StringBuilder stringBuilder = new StringBuilder ();foreach(string text in this.textList){stringBuilder.Append(text);stringBuilder.Append("\n");}return stringBuilder.ToString ();}}
下面是各个类的代码:TouchCallback.cs
using UnityEngine;using System.Collections;/// <summary>/// 触碰回调函数/// </summary>public class TouchCallback{// 开始回调函数,(按钮按下、触碰)触发一次public delegate void Begin();// 移动回调函数,移动时触发public delegate void Move(Vector2 direction);// 缩放回调函数,缩放时触发public delegate void Scale(float distance);// 结束回调函数,(按钮松开,触离)触发一次public delegate void End();// 更新回调函数,每侦触发public delegate void Update();}
using UnityEngine;using System.Collections;/// <summary>/// 平台控制器/// </summary>public class PlatformController : MonoBehaviour{protected TouchCallback.Begin beginCallback;protected TouchCallback.Move moveCallback;protected TouchCallback.Scale scaleCallback;protected TouchCallback.Update updateCallback;protected TouchCallback.End endCallback;/// <summary>/// 初始化回调函数/// </summary>/// <param name="beginCallback">Begin callback.</param>/// <param name="moveCallback">Move callback.</param>/// <param name="scaleCallback">Scale callback.</param>/// <param name="updateCallback">Update callback.</param>/// <param name="endCallback">End callback.</param>public virtual void Init(TouchCallback.Begin beginCallback, TouchCallback.Move moveCallback, TouchCallback.Scale scaleCallback, TouchCallback.Update updateCallback, TouchCallback.End endCallback){this.beginCallback = beginCallback;this.moveCallback = moveCallback;this.scaleCallback = scaleCallback;this.updateCallback = updateCallback;this.endCallback = endCallback;}}
using UnityEngine;using System.Collections;/// <summary>/// 鼠标控制器/// </summary>public class MouseController : PlatformController{/// 鼠标枚举enum MouseTypeEnum{LEFT = 0}/// <summary>/// 缩放距离/// </summary>private float scrollDistance;/// <summary>/// 鼠标按住状态/// </summary>private bool mousePressStatus;void Update(){// 按下鼠标、轴if (Input.GetMouseButtonDown ((int)MouseTypeEnum.LEFT)) {this.mousePressStatus = true;// 触发开始回调函数if(this.beginCallback != null) this.beginCallback();}// 松开鼠标、轴if (Input.GetMouseButtonUp ((int)MouseTypeEnum.LEFT)) {this.mousePressStatus = false;// 触发结束回调函数if(this.endCallback != null) this.endCallback();}// 如果鼠标在按住状态if (this.mousePressStatus) {// 触发移动回调函数if(this.moveCallback != null) this.moveCallback(new Vector2(Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y")));}// 鼠标滚轮拉近拉远this.scrollDistance = Input.GetAxis ("Mouse ScrollWheel");// 触发缩放回调函数if (this.scrollDistance != 0f && this.scaleCallback != null) this.scaleCallback(this.scrollDistance);// 触发每帧执行更新if (this.updateCallback != null) this.updateCallback ();}}
using UnityEngine;using System.Collections;/// <summary>/// 触碰控制器/// </summary>public class TouchController : PlatformController {/// <summary>/// 修正比例/// </summary>private float rate = 50f;/// <summary>/// 触点一/// </summary>private Touch oneTouch;/// <summary>/// 触点二/// </summary>private Touch twoTouch;/// <summary>/// 最后一次缩放距离/// </summary>private float lastScaleDistance;/// <summary>/// 当前缩放距离/// </summary>private float scaleDistance;void Update(){// 如果只有一个触点if (Input.touchCount == 1) {this.oneTouch = Input.touches[0];// 触点开始if(this.oneTouch.phase == TouchPhase.Began){// 触发开始回调函数if(this.beginCallback != null) this.beginCallback();}// 触点移动else if(oneTouch.phase == TouchPhase.Moved){// 触发移动回调函数if(this.moveCallback != null) this.moveCallback(new Vector2(this.oneTouch.deltaPosition.x, this.oneTouch.deltaPosition.y) / this.rate);}// 触点结束else if(oneTouch.phase == TouchPhase.Ended){// 触发结束回调函数if(this.endCallback != null) this.endCallback();}}// 如果有多个触点if(Input.touchCount > 1){this.oneTouch = Input.touches[0];this.twoTouch = Input.touches[1];// 如果是缩放if(oneTouch.phase == TouchPhase.Moved && twoTouch.phase == TouchPhase.Moved){this.scaleDistance = Vector2.Distance(this.oneTouch.position, this.twoTouch.position);// 触发缩放回调函数this.scaleCallback((this.scaleDistance - this.lastScaleDistance) / this.rate);this.lastScaleDistance = this.scaleDistance;}}// 触发每帧执行更新if (this.updateCallback != null) this.updateCallback ();}}
0 0
- Unity3D 鼠标以及触屏移动、缩放控制器(相机)
- unity 触摸事件 移动,缩放,以及相机移动超出边界
- Unity3D的相机跟随、视野缩放以及旋转
- Unity中用鼠标缩放相机。
- Unity中用鼠标缩放相机
- Unity3d相机跟随角色移动
- [Unity3D]手机3D游戏开发:简单的移动和缩放鼠标手势脚本源码
- 相机跟随和鼠标滚轮控制缩放
- Unity3d实现相机的跟随缩放旋转
- unity 鼠标移动 缩放,旋转
- 008-实现Unity3d中使用LeanTouch插件进行触屏控制(拖拽、移动、缩放旋转...)
- Unity中使用鼠标控制相机的视口方向以及移动
- MouseFollowRotation Unity3d鼠标点击旋转相机
- unity3d鼠标移动、旋转物体
- Unity3d 物体跟随鼠标移动
- Unity3D鼠标控制角色移动
- 使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果
- [Unity3d]鼠标旋转缩放拖动模型
- mixChannels()函数测试
- android帧动画
- shell命令-sed
- 关于error:Cannot assign to 'self' outside of a method in the init family
- CentOS7 设置截图的快捷键
- Unity3D 鼠标以及触屏移动、缩放控制器(相机)
- RTEMS 最新基于ubuntu和RSB编译环境的安装配置
- 103. Binary Tree Zigzag Level Order Traversal
- solrj使用
- CSS学习之Layouts方法float - CSS: The Missing Manual
- 【bzoj4554】【Tjoi2016&Heoi2016】【游戏】【二分图】
- Java-JVM的基本结构
- decompile挖财和小牛在线
- C语言指针变量可以当做数组运用