使用Unity实现触屏按下、拖拽、抬起的代码。
1、触屏按下:
using UnityEngine;
public class Touch : MonoBehaviour
{
void Update ()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
Debug.Log("Touch Began");
}
}
}
}
2、拖拽:
using UnityEngine;
public class Touch : MonoBehaviour
{
void Update ()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
Vector2 pos = touch.position;
Debug.Log("Touch Moved to x:" + pos.x + " y: " + pos.y);
}
}
}
}
3、抬起:
using UnityEngine;
public class Touch : MonoBehaviour
{
void Update ()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Ended)
{
Debug.Log("Touch Ended");
}
}
}
}
FROM CHATGPT