public class EasyTouch : MonoBehaviour {
//拖动的图片
public GameObject touch;
//起始点
Vector3 origin;
//供外部调用的返回向量
public Vector3 Dir { get; set; }
void Start () {
UUIEventListener.Get(touch).onDown += OnDown;
UUIEventListener.Get(touch).onUp += OnUp;
origin = touch.transform.position;
}
private void OnUp(GameObject go) {
isDrag = false;
touch.transform.position = origin;
}
bool isDrag = false;
private void OnDown(GameObject go) {
isDrag = true;
}
float x = 0;
float y = 0;
void Update () {
if (isDrag) {
x = Input.mousePosition.x;
y = Input.mousePosition.y;
//限制拖动物体的移动范围
x = Mathf.Clamp(x, origin.x - 100, origin.x + 100);
y = Mathf.Clamp(y, origin.y - 100, origin.y + 100);
touch.transform.position = new Vector3(x, y, 0);
//返回当前拖动结果
Dir = touch.transform.position - origin;
}
}
}