Ⅰ, A和B是兄弟ide
Ⅱ, C是A的孩子測試
Ⅰ, 代碼spa
A , B , C 分別掛載A.cs , B.cs , C.cs , 分別以下設計
A.cs 代碼以下code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class A : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { Debug.Log("A click"); } }
B.cs 代碼以下blog
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class B : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { Debug.Log("B click"); } }
C.cs 代碼以下繼承
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class C : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { Debug.Log("C click"); } }
Ⅱ, 測試事件
A, 卸載 B 和 C的腳本 B.cs 和 C.csit
1, 點擊C的時候A會觸發事件io
2, 點擊B的時候A不會觸發事件(兄弟攔截了)
B, 將B 和 C 的 腳本B.cs 和 C.cs從新掛載到B,C上
1, 點擊C的時候A不會觸發事件(被C攔截了)
2, 和上面同樣點擊B,A也不會觸發事件
1, 特殊需求: 不管點擊B或者C都會觸發下面的A
B.cs 代碼以下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class B : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { Debug.Log("B click"); 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); } } } }
C.cs 代碼
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class C : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { Debug.Log("C click"); 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); } } } }
1, 腳本
using UnityEngine; using UnityEngine.UI; public class ImageExtends : Image { override public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) { return false;//不攔截, (本身也不會觸發Event) } }
2, 將腳本掛載到B的GameObject上
1, A.cs 繼承 ICanvasRaycastFilter
2, 重寫 IsRaycastLocationValid() 方法
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class A : MonoBehaviour, IPointerClickHandler, ICanvasRaycastFilter { [SerializeField] private bool IsFocus = false; public void OnPointerClick(PointerEventData eventData) { Debug.Log("A click"); } public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) { return IsFocus; } }
當 IsFocus 爲false 時, A,C不觸發Event