ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-02-07 14:01:49  阅读:167  来源: 互联网

标签:UnityEngine gameObject void 键盘 Unity 事件 using eventData public


效果

在这里插入图片描述

事件

事件在Canvas的GraphicRaycaster组件启用时生效
在这里插入图片描述

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");
        }
    }
    

标签:UnityEngine,gameObject,void,键盘,Unity,事件,using,eventData,public
来源: https://blog.csdn.net/weixin_43526371/article/details/122807568

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有