Unity3D 滑鼠以及觸屏移動、縮放控制器(相機)
阿新 • • 發佈:2019-02-12
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 ();
}
}