渗透UI事件
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Script_5 : MonoBehaviour,IPointerClickHandler {

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.LogError("图片点击");
        PassEvent(eventData, ExecuteEvents.pointerClickHandler);
    }

    public void PassEvent<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function) where T : IEventSystemHandler
    {
        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(data, results);
        GameObject current = data.pointerCurrentRaycast.gameObject;
        for (int i = 0; i < results.Count; i++)
        {
            if (current != results[i].gameObject)
            {
                ExecuteEvents.Execute(results[i].gameObject, data, function);
                //break;
            }
        }
    }
}

//按钮的脚本
using UnityEngine;

public class Script_5_1 : MonoBehaviour {

    public void OnButtonClick()//方法在Inspector中绑定给了按钮
    {
        Debug.LogError("按钮点击");
    }

}


图片.png

图片.png


首页 我的博客
粤ICP备17103704号