代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ETC : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
float dis = 0;
Vector3 dir;
float r = 50;
RectTransform RT;
Vector3 startPos;
public void OnBeginDrag(PointerEventData eventData)
{
}
public void OnDrag(PointerEventData eventData)
{
dis = Vector3.Distance(startPos, Input.mousePosition);
if (dis < r)
{
transform.position = Input.mousePosition;
}
else
{
dir = Input.mousePosition - startPos;
transform.position = dir.normalized * r + startPos;
}
}
public void OnEndDrag(PointerEventData eventData)
{
transform.position = startPos;
}
// Start is called before the first frame update
void Start()
{
startPos = transform.position;
RT = transform as RectTransform;
}
public float GetAxies(string name)
{
if (name == "Vertical")
{
return RT.anchoredPosition.y / r;
}
if (name == "Horizontal")
{
return RT.anchoredPosition.x / r;
}
return 0;
}
}