1. 程式人生 > >EventTrigger和滑鼠事件介面

EventTrigger和滑鼠事件介面

EventTrigger將滑鼠事件進行了一層封裝,只要重寫EventTrigger裡面的對應的虛擬函式,就能實現滑鼠對應的方法。

~

public class UIeventTrigger : EventTrigger
{

    public override void OnPointerEnter(PointerEventData eventData)//滑鼠進入
    {
        base.OnPointerEnter(eventData);
        print("進入了");
    }
    public override void OnPointerExit
(PointerEventData eventData)//滑鼠移出 { base.OnPointerExit(eventData); print("出來了"); } Vector3 pos1;//開始的位置 Vector3 pos2;//結束的位置 float distance;//滑鼠滑動的距離 public override void OnBeginDrag(PointerEventData eventData)//開始拖拽 { base.OnBeginDrag(eventData); print("開始拖拽時間:"
+Time.time); pos1 = Input.mousePosition; } public bool isdrage; public override void OnDrag(PointerEventData eventData)//滑鼠拖拽 { base.OnDrag(eventData); print("拖得動嗎"); isdrage = true; print("拖。。。"+Time.time); } public override void OnEndDrag
(PointerEventData eventData)//滑鼠結束拖拽 { base.OnEndDrag(eventData); pos2 = Input.mousePosition; distance = Vector3.Distance(pos1,pos2); Vector3 op = pos2 - pos1; print("在x方向拖動距離:"+(pos2.x-pos1.x)); print("在y方向的距離:" + (pos2.y - pos1.y)); print("滑鼠拖拽的距離1:"+distance); print("滑鼠拖拽的距離2:" + op.magnitude); print("結束拖拽時間:"+Time.time); } public override void OnPointerUp(PointerEventData eventData)//滑鼠擡起 { base.OnPointerUp(eventData); isdrage = false; } public override void OnDrop(PointerEventData eventData)//?還不清楚有什麼用處 { base.OnDrop(eventData); } public override void OnInitializePotentialDrag(PointerEventData eventData)//拖拽前的初始化 { base.OnInitializePotentialDrag(eventData); print("初始化~~~拖拽" + Time.time); } public override void OnPointerClick(PointerEventData eventData)//滑鼠點選 { base.OnPointerClick(eventData); } public override void OnScroll(PointerEventData eventData)//滑鼠滾輪 { base.OnScroll(eventData); print("滾了麼"); } // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (isdrage==true) { this.transform.position = Input.mousePosition; } } }