Unity3D UGUI优化方法( RaycastTarget )

在游戏开发过程中笔者发现,UGUI创建的所有UI组件都会带有一个RaycastTarget的属性并且默认勾选。几番百度,原来UGUI会遍历屏幕中所有勾选了RaycastTarget属性的UI,接着就会发射线,并且排序找到玩家最先触发的那个UI,在抛出事件给逻辑层去响应。所以很多不需要响应事件的Text,Image组件是不需要勾选RaycastTarget的。开发过程中也应当减少这些不必要的消耗,笔者在SerenaHaven的CSDN博客上上找个一个很好用的工具,先收藏在这里备用吧。

原文链接 https://blog.csdn.net/SerenaHaven/article/details/80972601

工具代码(放进Editor文件夹,在菜单栏Tools->RaycastTargetChecker 下打开工具)

using UnityEditor;
using UnityEngine.UI;
using UnityEngine;

public class RaycastTargetChecker : EditorWindow
{
    private MaskableGraphic[] graphics;
    private bool hideUnchecked = false;
    private bool showBorders = true;
    private Color borderColor = Color.blue;
    private Vector2 scrollPosition = Vector2.zero;

    private static RaycastTargetChecker instance = null;

    [MenuItem("Tools/RaycastTarget Checker")]
    private static void Open()
    {
        instance = instance ?? EditorWindow.GetWindow<RaycastTargetChecker>("RaycastTargets");
        instance.Show();
    }

    void OnEnable()
    {
        instance = this;
    }

    void OnDisable()
    {
        instance = null;
    }

    void OnGUI()
    {
        using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope())
        {
            showBorders = EditorGUILayout.Toggle("Show Gizmos", showBorders, GUILayout.Width(200.0f));
            borderColor = EditorGUILayout.ColorField(borderColor);
        }
        hideUnchecked = EditorGUILayout.Toggle("Hide Unchecked", hideUnchecked);

        GUILayout.Space(12.0f);
        Rect rect = GUILayoutUtility.GetLastRect();
        GUI.color = new Color(0.0f, 0.0f, 0.0f, 0.25f);
        GUI.DrawTexture(new Rect(0.0f, rect.yMin + 6.0f, Screen.width, 4.0f), EditorGUIUtility.whiteTexture);
        GUI.DrawTexture(new Rect(0.0f, rect.yMin + 6.0f, Screen.width, 1.0f), EditorGUIUtility.whiteTexture);
        GUI.DrawTexture(new Rect(0.0f, rect.yMin + 9.0f, Screen.width, 1.0f), EditorGUIUtility.whiteTexture);
        GUI.color = Color.white;

        graphics = GameObject.FindObjectsOfType<MaskableGraphic>();

        using (GUILayout.ScrollViewScope scrollViewScope = new GUILayout.ScrollViewScope(scrollPosition))
        {
            scrollPosition = scrollViewScope.scrollPosition;
            for (int i = 0; i < graphics.Length; i++)
            {
                MaskableGraphic graphic = graphics[i];
                if (hideUnchecked == false || graphic.raycastTarget == true)
                {
                    DrawElement(graphic);
                }
            }
        }
        foreach (var item in graphics)
        {
            EditorUtility.SetDirty(item);
        }
        Repaint();
    }

    private void DrawElement(MaskableGraphic graphic)
    {
        using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope())
        {
            Undo.RecordObject(graphic, "Modify RaycastTarget");
            graphic.raycastTarget = EditorGUILayout.Toggle(graphic.raycastTarget, GUILayout.Width(20));
            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.ObjectField(graphic, typeof(MaskableGraphic), true);
            EditorGUI.EndDisabledGroup();
        }
    }

    [DrawGizmo(GizmoType.Selected | GizmoType.NonSelected)]
    private static void DrawGizmos(MaskableGraphic source, GizmoType gizmoType)
    {
        if (instance != null && instance.showBorders == true && source.raycastTarget == true)
        {
            Vector3[] corners = new Vector3[4];
            source.rectTransform.GetWorldCorners(corners);
            Gizmos.color = instance.borderColor;
            for (int i = 0; i < 4; i++)
            {
                Gizmos.DrawLine(corners[i], corners[(i + 1) % 4]);
            }
            if (Selection.activeGameObject == source.gameObject)
            {
                Gizmos.DrawLine(corners[0], corners[2]);
                Gizmos.DrawLine(corners[1], corners[3]);
            }
        }
        SceneView.RepaintAll();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39824797/article/details/83866671