鼠标点击了UI,并且移出此UI执行事件(松开鼠标后不执行)

一个Unity朋友突然间让我做这么一个功能,有点懵,花了大概是10分钟左右写出来的,需要注意的是继承IPointerDownHandler 这个接口。OnPointerDown 是这个接口的实现。UnityEngine.EventSystems 引用这个命名空间

	private bool MouseCilcking = false;
    public PointerEventData.InputButton MouseType;
    void Update()
    {
    
    
        if (Input.GetMouseButtonUp((int)MouseType))
        {
    
    
            Debug.Log("鼠标松开了");
            MouseCilcking = false;
        }
        if (MouseCilcking)
        {
    
    
            Debug.Log("鼠标点击了UI,并且鼠标没有松开");
        }
    }
    public void OnPointerDown(PointerEventData eventData)
    {
    
    
        if (eventData.button == MouseType)
        {
    
    
            Debug.Log("鼠标点击了本UI");
            MouseCilcking = true;
        }
    }

猜你喜欢

转载自blog.csdn.net/LWKlwk11/article/details/106541822