public class InputHelper
{
public TouchOperation OnTouchDown;
public TouchOperation OnTouchMove;
public TouchOperation OnTouchUp;
public TouchOperation OnClick;
public long TouchDownTime;
public long TouchUpTime;
[HideInInspector]
public float Pressure = 1;
private bool FixMouseBegan = false;
public void MouseInput()
{
UnityEngine.EventSystems.EventSystem eventSystem = UnityEngine.EventSystems.EventSystem.current;
if (eventSystem != null && eventSystem.IsPointerOverGameObject())
{
if (!FixMouseBegan) return;
}
if (Input.GetMouseButton(0) && FixMouseBegan)
{
Vector3 mousePositon = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
OnUserTouchMove(Input.mousePosition);
}
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePositon = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
OnUserTouchDown(Input.mousePosition);
FixMouseBegan = true;
}
if (Input.GetMouseButtonUp(0) && FixMouseBegan)
{
Vector3 mousePositon = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
OnUserTouchUp(Input.mousePosition);
FixMouseBegan = false;
}
}
private TouchPhase preTouchPhase;
private bool fixTouchBegan = false;
public void TouchInput()
{
if (Input.touchCount > 0)
{
Pressure = Input.GetTouch(0).pressure;
bool isClickOverUI;
isClickOverUI = EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId);
if (isClickOverUI)
{
if (!fixTouchBegan) return;
}
Touch firstTouch = Input.GetTouch(0);
if (firstTouch.phase == TouchPhase.Began)
{
fixTouchBegan = true;
Vector3 touchPositon = Camera.main.ScreenToWorldPoint(new Vector3(firstTouch.position.x, firstTouch.position.y, -Camera.main.transform.position.z));
OnUserTouchDown(firstTouch.position);
}
if (firstTouch.phase == TouchPhase.Moved)
{
Vector3 touchPositon = Camera.main.ScreenToWorldPoint(new Vector3(firstTouch.position.x, firstTouch.position.y, -Camera.main.transform.position.z));
OnUserTouchMove(firstTouch.position);
}
if (firstTouch.phase == TouchPhase.Ended && preTouchPhase != TouchPhase.Ended)
{
Vector3 touchPositon = Camera.main.ScreenToWorldPoint(new Vector3(firstTouch.position.x, firstTouch.position.y, -Camera.main.transform.position.z));
OnUserTouchUp(firstTouch.position);
fixTouchBegan = false;
}
if (firstTouch.phase == TouchPhase.Stationary && preTouchPhase != TouchPhase.Ended)
{
Vector3 touchPositon = Camera.main.ScreenToWorldPoint(new Vector3(firstTouch.position.x, firstTouch.position.y, -Camera.main.transform.position.z));
OnUserTouchMove(firstTouch.position);
}
preTouchPhase = firstTouch.phase;
}
}
private void OnUserTouchDown(Vector3 position)
{
TouchDownTime = GetTimestamp();
if (OnTouchDown != null)
{
OnTouchDown(position);
}
}
private void OnUserTouchMove(Vector3 position)
{
if (OnTouchMove != null)
{
OnTouchMove(position);
}
}
private void OnUserTouchUp(Vector3 position)
{
TouchUpTime = GetTimestamp();
if (OnTouchUp != null)
{
OnTouchUp(position);
}
if (TouchUpTime - TouchDownTime <= 100)
{
OnUserClick(position);
}
}
private void OnUserClick(Vector3 position)
{
if (OnClick != null)
{
OnClick(position);
}
}
public static long GetTimestamp()
{
long unixTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
return unixTimestamp;
}
public delegate void TouchOperation(Vector3 position);
}
using System.Collections;
using UnityEngine;
public class MyPlayer : MonoBehaviour
{
void Awake()
{
}
private InputHelper PlayerInput;
private Vector3 PreTouchPosition;
private Vector3 MoveOffset;
private bool FirstTouchDown = true;
void Start()
{
this.InitPlayerInput();
}
private void InitPlayerInput()
{
Debug.Log("注册");
PlayerInput = new InputHelper();
PlayerInput.OnTouchDown = OnPlayerTouchDown;
PlayerInput.OnTouchMove = OnPlayerTouchMove;
PlayerInput.OnTouchUp = OnPlayerTouchUp;
}
private void OnPlayerTouchDown(Vector3 position)
{
if (FirstTouchDown)
{
FirstTouchDown = false;
}
PreTouchPosition = position;
Debug.Log("点击" + position);
}
private void OnPlayerTouchUp(Vector3 position)
{
Debug.Log("抬起" + position);
}
private void OnPlayerTouchMove(Vector3 position)
{
Debug.Log("移动" + position);
float dis = position.x - PreTouchPosition.x;
float disY = position.y - PreTouchPosition.y;
if (System.Math.Abs(dis) > 2)
{
float value = dis / Screen.width;
float length = System.Math.Abs(4 * value * 1.5f);
if (dis < 0)
{
MoveOffset.x += -length;
}
else
{
MoveOffset.x += length;
}
}
if (System.Math.Abs(disY) > 2)
{
float valueY = disY / Screen.height;
float lengthY = System.Math.Abs(4 * valueY * 1.5f);
if (disY < 0)
{
MoveOffset.y += -lengthY;
}
else
{
MoveOffset.y += lengthY;
}
}
PreTouchPosition = position;
}
private void HandleControl()
{
#if UNITY_EDITOR
PlayerInput.MouseInput();
#else
PlayerInput.TouchInput();
#endif
}
private bool AcceptInput = true;
void Update()
{
if (AcceptInput)
{
HandleControl();
}
this.Move();
}
public void Move()
{
if (MoveOffset.magnitude > 0)
{
this.gameObject.transform.position = MoveOffset;
}
}
}
|