Unity UI 镂空遮罩组件:TutorialHollowOutMask 实现与优化
{"title":"using Skillx.Utils;\nusing UnityEngine;\nusing UnityEngine.UI;\n\nnamespace NoviceTutorial\n{\n /// csharp\nusing UnityEngine;\nusing UnityEngine.UI;\n\nnamespace NoviceTutorial\n{\n public class TutorialHollowOutRaycaster : GraphicRaycaster\n {\n public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)\n {\n // 获取场景中所有的TutorialHollowOutMask组件\n TutorialHollowOutMask[] masks = FindObjectsOfType<TutorialHollowOutMask>();\n\n // 遍历每个TutorialHollowOutMask组件\n foreach (TutorialHollowOutMask mask in masks)\n {\n // 如果点击的位置在高亮区域内,则跳过该TutorialHollowOutMask组件\n if (RectTransformUtility.RectangleContainsScreenPoint(mask.HighLightTarget, eventData.position, eventCamera))\n {\n continue;\n }\n\n // 将点击事件交给TutorialHollowOutMask处理\n mask.ProcessRaycast(eventData, resultAppendList);\n }\n }\n }\n}\n\n\n在上述代码中,我们创建了一个名为TutorialHollowOutRaycaster的类,继承自GraphicRaycaster。在Raycast方法中,我们获取场景中所有的TutorialHollowOutMask组件,并遍历每个组件。如果点击的位置在高亮区域内,则跳过该组件;否则,将点击事件交给TutorialHollowOutMask的ProcessRaycast方法处理。\n\n要使用这个TutorialHollowOutRaycaster,需要将它挂载到场景中的EventSystem对象上的GraphicRaycaster组件上。然后,将场景中的所有TutorialHollowOutMask组件的Canvas属性的Raycaster Type设置为None,这样它们就不会被默认的GraphicRaycaster处理。\n\n请注意,这只是一个示例代码,可能需要根据具体项目的需求进行修改和适配。
原文地址: https://www.cveoy.top/t/topic/qeSZ 著作权归作者所有。请勿转载和采集!