178 lines
5.2 KiB
C#
178 lines
5.2 KiB
C#
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
using UnityEngine.EventSystems;
|
||
|
||
[RequireComponent(typeof(ProcessInput))]
|
||
public class ProcessInputModule : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
|
||
{
|
||
// 仅仅追踪两根手指
|
||
private InputPointerData[] _fingers;
|
||
|
||
public ProcessInput processInput { get; private set; }
|
||
public ProcessCamera processCamera { get; private set; }
|
||
|
||
// 当前是操作摄像机还是角色运动
|
||
public bool isCameraMode { get; private set; }
|
||
private bool _clickOnPointerUp;
|
||
|
||
public event UnityAction<bool> onCameraModeChange;
|
||
|
||
private void Awake()
|
||
{
|
||
processInput = GetComponent<ProcessInput>();
|
||
processCamera = new ProcessCamera();
|
||
_fingers = new InputPointerData[2];
|
||
for (var i = 0; i < _fingers.Length; i++)
|
||
_fingers[i] = new InputPointerData();
|
||
SetCameraModel(true);
|
||
}
|
||
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
private void Update()
|
||
{
|
||
var mouseWheel = Input.GetAxis("Mouse ScrollWheel");
|
||
if (mouseWheel != 0f)
|
||
processCamera.ScaleCamera(mouseWheel * 800f);
|
||
#if UNITY_EDITOR
|
||
if (Input.GetKeyDown(KeyCode.Space))
|
||
SetCameraModel(!isCameraMode);
|
||
#endif
|
||
}
|
||
#endif
|
||
public void SetCameraModel(bool cameraMode)
|
||
{
|
||
if (isCameraMode != cameraMode)
|
||
{
|
||
_clickOnPointerUp = false;
|
||
isCameraMode = cameraMode;
|
||
if (isCameraMode)
|
||
processInput.EndTouchPos();
|
||
if (onCameraModeChange != null)
|
||
onCameraModeChange(isCameraMode);
|
||
}
|
||
}
|
||
|
||
#region interface
|
||
|
||
private void CleanFingers()
|
||
{
|
||
_clickOnPointerUp = false;
|
||
for (var i = 0; i < _fingers.Length; i++)
|
||
_fingers[i].pointerId = null;
|
||
if (isCameraMode)
|
||
{
|
||
// 不需要处理
|
||
}
|
||
else
|
||
processInput.EndTouchPos();
|
||
}
|
||
|
||
private void OnApplicationFocus(bool hasFocus)
|
||
{
|
||
// App失去焦点时,释放当前输入状态
|
||
if (!hasFocus)
|
||
CleanFingers();
|
||
}
|
||
|
||
// 注:任意被追踪的手指抬起,清除全部手指信息而不是回滚到单手指
|
||
public void OnPointerUp(PointerEventData eventData)
|
||
{
|
||
// 检查是否触发抬起点击
|
||
if (isCameraMode && _clickOnPointerUp)
|
||
{
|
||
if (_fingers[0].pointerId == eventData.pointerId)
|
||
processInput.StartOneFrameTouch(eventData.position);
|
||
}
|
||
_clickOnPointerUp = false;
|
||
var remove = false;
|
||
for (var i = 0; i < _fingers.Length; i++)
|
||
if (_fingers[i].pointerId == eventData.pointerId)
|
||
{
|
||
remove = true;
|
||
break;
|
||
}
|
||
|
||
if (remove)
|
||
CleanFingers();
|
||
}
|
||
|
||
public void OnPointerDown(PointerEventData eventData)
|
||
{
|
||
if (_fingers[0].pointerId == null)
|
||
{
|
||
_fingers[0].pointerId = eventData.pointerId;
|
||
_fingers[0].lastPos = eventData.position;
|
||
// 常规Touch开始
|
||
if (isCameraMode)
|
||
_clickOnPointerUp = true;
|
||
else
|
||
processInput.StartTouchPos(eventData.position);
|
||
if (FunctionExLogic.Instance() != null)
|
||
FunctionExLogic.Instance().ShowDetail(false);
|
||
}
|
||
else if (_fingers[1].pointerId == null)
|
||
{
|
||
_clickOnPointerUp = false;
|
||
_fingers[1].pointerId = eventData.pointerId;
|
||
_fingers[1].lastPos = eventData.position;
|
||
// 进入摄像机缩放模式
|
||
if (isCameraMode)
|
||
{
|
||
}
|
||
else
|
||
processInput.EndTouchPos();
|
||
}
|
||
}
|
||
|
||
public void OnDrag(PointerEventData eventData)
|
||
{
|
||
_clickOnPointerUp = false;
|
||
// 单手指模式
|
||
if (_fingers[1].pointerId == null)
|
||
{
|
||
if (_fingers[0].pointerId == eventData.pointerId)
|
||
{
|
||
if (isCameraMode)
|
||
processCamera.MoveCamera(eventData.delta);
|
||
else
|
||
processInput.MoveTouchPos(eventData.position);
|
||
}
|
||
}
|
||
// 双手指模式
|
||
else
|
||
{
|
||
InputPointerData current = null;
|
||
InputPointerData other = null;
|
||
if (_fingers[0].pointerId == eventData.pointerId)
|
||
{
|
||
current = _fingers[0];
|
||
other = _fingers[1];
|
||
}
|
||
else if (_fingers[1].pointerId == eventData.pointerId)
|
||
{
|
||
current = _fingers[1];
|
||
other = _fingers[0];
|
||
}
|
||
|
||
if (current != null)
|
||
{
|
||
current.lastPos = eventData.position;
|
||
var line = eventData.position - other.lastPos;
|
||
if (line != Vector2.zero)
|
||
{
|
||
line = line.normalized;
|
||
var scale = Vector2.Dot(line, eventData.delta);
|
||
processCamera.ScaleCamera(scale);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
public class InputPointerData
|
||
{
|
||
public int? pointerId;
|
||
public Vector2 lastPos;
|
||
}
|
||
} |