Unity 给UI注册hover状态的事件
/// <summary> /// button增加鼠标hover的回调 /// </summary> public void AddTrigger(GameObject obj, EventTriggerType triggerType, UnityEngine.Events.UnityAction<BaseEventData> action) { EventTrigger trigger = obj.GetComponent<EventTrigger>(); if (trigger == null) { trigger = obj.AddComponent<EventTrigger>(); } EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = triggerType; entry.callback = new EventTrigger.TriggerEvent(); entry.callback.AddListener(action); trigger.triggers.Add(entry); }
调用
AddTrigger(btn.gameObject, EventTriggerType.PointerEnter, (eventData) => { Debug.LogError(eventData.selectedObject); });
博客园Jason_c微信打赏码
如果本篇文档对你有帮助,打赏Jason_c根华子吧,他的私房钱被老婆没收了,呜呜!
![](https://files-cdn.cnblogs.com/files/Jason-c/%E5%BE%AE%E4%BF%A1%E6%89%93%E8%B5%8F%E7%A0%81.bmp)