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_WIN
controller = this.gameObject.AddComponent<MouseController>();
#elif UNITY_ANDROID
controller = this.gameObject.AddComponent<TouchController>();
#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.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 ();
}
}