美文网首页
UnityEngine.EventSystems

UnityEngine.EventSystems

作者: 知交 | 来源:发表于2020-09-05 10:53 被阅读0次

    1.使用代码添加 EventTrigger.Entry:

    代码:

    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class SkillCell
    {
        public Transform transform = null;
        private EventTrigger eventTrigger;
        protected SkillCell (Transform transform)
        {
            this.transform = transform;
            InitView();
        }
        
        private void InitView() {
            //给物体添加EventTrigger组件
             eventTrigger = transform.gameObject.AddComponent<EventTrigger>();
            //按下
            EventTrigger.Entry downEntry = new EventTrigger.Entry();
            downEntry.eventID = EventTriggerType.PointerDown;
           //添加TriggerEvent
            downEntry.callback = new EventTrigger.TriggerEvent();
            downEntry.callback.AddListener(OnPointerDown);
            eventTrigger.triggers.Add(downEntry);
            //抬起
            EventTrigger.Entry upEntry = new EventTrigger.Entry();
            upEntry.eventID = EventTriggerType.PointerUp;
            upEntry.callback = new EventTrigger.TriggerEvent();
            upEntry.callback.AddListener(OnPointerUp);
            eventTrigger.triggers.Add(upEntry);
        }
        public void OnPointerDown(BaseEventData eventData)
        {   
        }
    
        public void OnPointerUp(BaseEventData eventData)
        {
        }
        //封装方法
        public static void RegisterEventClick(EventTrigger eventTrigger, EventTriggerType eventTriggerType, UnityAction<BaseEventData> callback)
        {
            EventTrigger.Entry entry = new EventTrigger.Entry {
                eventID = eventTriggerType
            };
            entry.callback.AddListener(callback);
            eventTrigger.triggers.Add(entry);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:UnityEngine.EventSystems

          本文链接:https://www.haomeiwen.com/subject/owkstktx.html