0
点赞
收藏
分享

微信扫一扫

Unity(二十八):事件、渗透事件、键盘事件

禾木瞎写 2022-02-07 阅读 53

效果

事件

1、2 的事件

在这里插入图片描述

  • Assets/Scripts/Event_01_02.cs
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Event_01_02 : MonoBehaviour
    {
        public Image EventUI01;
    
        public Button EventUI02;
    
        private void Awake()
        {
            // EventUI01
            UGUIEventListener.Get(EventUI01.gameObject).OnClick = (go, eventData) => { Debug.Log($"{go.name}"); };
    
            // EventUI02
            EventUI02.onClick.AddListener(delegate { Debug.Log(EventUI02.name); });
        }
    }
    
  • Assets/Scripts/UGUIEventListener.cs
    using UnityEngine;
    using UnityEngine.Events;
    using UnityEngine.EventSystems;
    
    public class UGUIEventListener : EventTrigger
    {
        public UnityAction<GameObject, PointerEventData> OnClick;
    
        public override void OnPointerClick(PointerEventData eventData)
        {
            base.OnPointerClick(eventData);
            OnClick?.Invoke(gameObject, eventData);
        }
    
        public static UGUIEventListener Get(GameObject go)
        {
            return go.GetComponent<UGUIEventListener>() == null
                ? go.AddComponent<UGUIEventListener>()
                : go.GetComponent<UGUIEventListener>();
        }
    }
    

3 的事件

在这里插入图片描述

  • Assets/Scripts/Event_03.cs
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class Event_03 : MonoBehaviour, IPointerClickHandler
    {
        public void OnPointerClick(PointerEventData eventData)
        {
            Debug.Log($"{gameObject.name}");
        }
    }
    

4 的事件(渗透事件)

在这里插入图片描述

  • Assets/Scripts/Event_04.cs
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class Event_04 : MonoBehaviour, IPointerClickHandler
    {
        // 监听点击
        public void OnPointerClick(PointerEventData eventData)
        {
            Debug.Log($"★{gameObject.name}");
            PassEvent(eventData, ExecuteEvents.pointerClickHandler);
        }
    
        // 事件渗透
        private void PassEvent<T>(PointerEventData eventData, ExecuteEvents.EventFunction<T> function)
            where T : IEventSystemHandler
        {
            // 存储当前点击位置的射线投射结果
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(eventData, results);
            foreach (var result in results.Where(result => gameObject != result.gameObject))
            {
                ExecuteEvents.Execute(result.gameObject, eventData, function);
                // break; // RaycastAll后UGUI会自己排序,如果你只想响应透下去的最近的一个响应,这里ExecuteEvents.Execute后直接break就行。
            }
        }
    }
    

键盘事件

在这里插入图片描述

  • Assets/Scripts/KeyboardEvent.cs
    using UnityEngine;
    using UnityEngine.Events;
    
    public class KeyboardEvent : MonoBehaviour
    {
        private UnityAction<int, string> _uAction;
        private readonly UnityEvent<int, string> _uEvent = new UnityEvent<int, string>();
    
        private void Start()
        {
            _uAction = (num, str) => { Debug.Log($"{num} ---> {str}"); };
    
            _uEvent.AddListener(_uAction);
        }
    
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.A))
                _uAction.Invoke(0, "Lee");
            if (Input.GetKeyDown(KeyCode.B))
                _uEvent.Invoke(1, "AaA");
        }
    }
    
举报

相关推荐

0 条评论