Unity3D 支持多平台的发布,但是平时在测试的过程中往往需要在各个平台之间进行切换,在平时开发中,对象的移动、舞台缩放使用鼠标控制比较方便,但是在安卓平台,鼠标就无法发挥作用了,只能使用触控操作,最近整理了两种控制器,适用于桌面以及安卓平台。
最终使用代码如下:
using UnityEngine; using System.Text; using System.Collections.Generic; public class ControllerObject : MonoBehaviour { public UILabel uiLabel; private PlatformController controller; private IListtextList; void Awake() { this.textList = new List(); #if UNITY_STANDALONE_WIN controller = this.gameObject.AddComponent(); #elif UNITY_ANDROID controller = this.gameObject.AddComponent(); #endif controller.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.csusing UnityEngine;using System.Collections;////// 触碰回调函数
///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();}[csharp] view plain copy 在CODE上查看代码片派生到我的代码片using UnityEngine; using System.Collections; ////// 平台控制器
///public class PlatformController : MonoBehaviour { protected TouchCallback.Begin beginCallback; protected TouchCallback.Move moveCallback; protected TouchCallback.Scale scaleCallback; protected TouchCallback.Update updateCallback; protected TouchCallback.End endCallback; ////// 初始化回调函数
//////Begin callback. ///Move callback. ///Scale callback. ///Update callback. ///End callback. 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; } } [csharp] view plain copy 在CODE上查看代码片派生到我的代码片using UnityEngine; using System.Collections; ////// 鼠标控制器
///public class MouseController : PlatformController { /// 鼠标枚举 enum MouseTypeEnum { LEFT = 0 } ////// 缩放距离
///private float scrollDistance; ////// 鼠标按住状态
///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 (); } } [csharp] view plain copy 在CODE上查看代码片派生到我的代码片using UnityEngine; using System.Collections; ////// 触碰控制器
///public class TouchController : PlatformController { ////// 修正比例
///private float rate = 50f; ////// 触点一
///private Touch oneTouch; ////// 触点二
///private Touch twoTouch; ////// 最后一次缩放距离
///private float lastScaleDistance; ////// 当前缩放距离
///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 (); } }