UnityEngine.EventSystems常用事件系統 轉載集合

IBeginDragHandler, IDragHandler, IEndDragHandler

1.三個拖拽事件相關接口
  * IBeginDragHandler: 開始拖拽事件處理器;開始拖拽的一瞬間觸發。
  * IDragHandler: 拖拽中事件處理器;拖拽過程中持續觸發。
  * IEndDragHandler: 結束拖拽事件處理器;拖拽結束的一瞬間觸發。

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ItemDrag : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private RectTransform m_RT;
    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
    {
        print("IBeginDragHandler.OnBeginDrag");
        gameObject.GetComponent<Transform>().position = Input.mousePosition;
        print("這是實現的拖拽開始接口");
    }

    void IDragHandler.OnDrag(PointerEventData eventData)
    {
        print("IDragHandler.OnDrag");
        //雖然用Input.mousePosition可以得到一個2D座標,不過我們現在需要的是3D座標,看下面
        //gameObject.GetComponent<Transform>().position = Input.mousePosition;
        
        //3D座標獲取方法
        Vector3 pos;
        m_RT = gameObject.GetComponent<RectTransform>();
        //屏幕座標到世界座標
        RectTransformUtility.ScreenPointToWorldPointInRectangle(m_RT,eventData.position,eventData.enterEventCamera,out pos);
        m_RT.position = pos;
        print("拖拽中……");
    }

    void IEndDragHandler.OnEndDrag(PointerEventData eventData)
    {
        print("IEndDragHandler.OnEndDrag");
        gameObject.GetComponent<Transform>().position = Input.mousePosition;
        print("實現的拖拽結束接口");
    }
}

原文出處

IDragHandler

public void OnDrag(PointerEventData eventData)
    {
        Vector2 vect = eventData.position - centerPOs;

        expectedHandle.position = centerPOs + vect.normalized * distance; //更新位置,中心點加上鼠標向量的歸一值乘上距離

        angle = Vector2.Angle(Vector2.down, vect); //計算夾角
        if (vect.x < 0)
        {
            angle = 360 - angle;
        }
        this.transform.localEulerAngles = new Vector3(0, 0, angle);
    }

原文出處

IPointerEnterHandler, IPointerExitHandler

實現監控鼠標的進入和退出UI觸發事件

using UnityEngine.EventSystems;
using UnityEngine;

public class UITrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("進入");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("退出");
    }
}


原文出處

IsPointerOverGameObject

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class MouseExample : MonoBehaviour
{
    void Update()
    {
        // Check if the left mouse button was clicked
        if (Input.GetMouseButtonDown(0))
        {
            // Check if the mouse was clicked over a UI element
            if (EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("Clicked on the UI");
            }
        }
        // Check if there is a touch
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            // Check if finger is over a UI element
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                Debug.Log("Touched the UI");
            }
        }
    }
}

原文出處

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章