最近在做的unity的mmorpg项目
涉及到touch相关,
pc端和手机端不同使用方式,
找到了一个比较简单好用的控制器记录一下:
基类PlatformController.cs
代码是
using UnityEngine;
/// <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();
}
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;
}
/// <summary>
/// 初始化回调函数
/// </summary>
/// <param name="beginCallback">Begin callback.</param>
/// <param name="moveCallback">Move callback.</param>
/// <param name="scaleCallback">Scale callback.</param>
/// <param name="endCallback">End callback.</param>
public virtual void Init(TouchCallback.Begin beginCallback, TouchCallback.Move moveCallback, TouchCallback.Scale scaleCallback, TouchCallback.End endCallback) {
this.beginCallback = beginCallback;
this.moveCallback = moveCallback;
this.scaleCallback = scaleCallback;
this.endCallback = endCallback;
}
/// <summary>
/// 初始化回调函数
/// </summary>
/// <param name="beginCallback">Begin callback.</param>
/// <param name="updateCallback">Update callback.</param>
/// <param name="endCallback">End callback.</param>
public virtual void Init(TouchCallback.Begin beginCallback, TouchCallback.Update updateCallback, TouchCallback.End endCallback) {
this.beginCallback = beginCallback;
this.updateCallback = updateCallback;
this.endCallback = endCallback;
}
private void Update()
{
OnUpdate();
}
protected virtual bool OnUpdate()
{
return !UICamera.isOverUI;
}
}
两个派生类分别是鼠标控制器 MouseController.cs
using UnityEngine;
public class MouseController : PlatformController {
bool pressDown;
Vector2 oldPos;
Vector2 newPos;
protected override bool OnUpdate() {
if(!base.OnUpdate()) {
if(pressDown) {
pressDown = false;
if(endCallback != null) endCallback();
}
return false;
}
if(Input.GetMouseButtonDown(0)) {
pressDown = true;
if(beginCallback != null) beginCallback();
}
if(pressDown) {
newPos = Input.mousePosition;
Vector3 movePos = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
if(moveCallback != null) moveCallback(movePos);
oldPos = Input.mousePosition;
}
if(Input.GetMouseButtonUp(0)) {
pressDown = false;
if(endCallback != null) endCallback();
}
float zoom = Input.GetAxis("Mouse ScrollWheel");
if(zoom != 0) {
if(scaleCallback != null) scaleCallback(zoom);
}
if(updateCallback != null) updateCallback();
return true;
}
}
和触屏派生类
using UnityEngine;
public class TouchController : PlatformController {
float rate = 50;
Touch touch1;
Touch touch2;
float oldDis;
protected override bool OnUpdate() {
if(!base.OnUpdate()) return false;
if(Input.touchCount == 1) {
touch1 = Input.GetTouch(0);
if(touch1.phase == TouchPhase.Began) {
if(beginCallback != null) beginCallback();
} else if(touch1.phase == TouchPhase.Moved) {
float moveX = touch1.deltaPosition.x;
float moveY = touch1.deltaPosition.y;
if(moveCallback != null) moveCallback(new Vector2(moveX, moveY));
} else if(touch1.phase == TouchPhase.Ended) {
if(endCallback != null) endCallback();
}
} else if(Input.touchCount > 1) {
touch1 = Input.GetTouch(0);
touch2 = Input.GetTouch(1);
if(touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved) {
float dis = Vector3.Distance(touch1.position, touch2.position);
if(scaleCallback != null) scaleCallback((dis - oldDis) / rate);
oldDis = dis;
}
}
if(updateCallback != null) updateCallback();
return true;
}
}
使用方法也比较简单
public class Test
{
private PlatformController touchController;
void Init() {
//MainCamera.transform.LookAt(_target);
#if UNITY_EDITOR
touchController = gameObject.AddMissingComponent<MouseController>();
#else
touchController = gameObject.AddMissingComponent<TouchController>();
#endif
touchController.Init(TouchBegin, TouchMove, TouchScale, TouchEnd);
}
void TouchBegin() {
}
void TouchMove(Vector2 movePos) {
}
void TouchScale(float scale) {
}
void TouchEnd() {
}
}
代码都比较简单 就不加注释了
这个逻辑都比较简单 都是些常用的方法
就是这个代码风格本人比较喜欢
像这种功能合适最重要 反正也没有什么技术难度
发个文记录一下 方便以后遇到类似的功能