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
原创粉丝点击