• [Unity优化]UI优化(一):RaycastTarget


    参考链接:

    http://www.manew.com/thread-100366-1-1.html

    https://www.jianshu.com/p/3082ebf8a342

    https://blog.csdn.net/serenahaven/article/details/80972601

    1.RaycastTarget的检测

     1 using UnityEngine;
     2 using UnityEngine.UI;
     3 
     4 public class DrawRaycastTarget : MonoBehaviour {
     5 
     6     Vector3[] worldCorners = new Vector3[4];
     7 
     8     private void OnDrawGizmos()
     9     {
    10         foreach (MaskableGraphic maskableGraphic in FindObjectsOfType<MaskableGraphic>())
    11         {
    12             if (maskableGraphic.raycastTarget)
    13             {
    14                 RectTransform rectTransform = maskableGraphic.transform as RectTransform;
    15                 rectTransform.GetWorldCorners(worldCorners);
    16                 Gizmos.color = Color.red;
    17                 for (int i = 0; i < 4; i++)
    18                 {
    19                     Gizmos.DrawLine(worldCorners[i], worldCorners[(i + 1) % 4]);
    20                 }
    21             }
    22         }
    23     }
    24 }

     效果如下:

    2.批量修改RaycastTarget

      1 using UnityEditor;
      2 using UnityEngine.UI;
      3 using UnityEngine;
      4 
      5 public class RaycastTargetChecker : EditorWindow
      6 {
      7     private MaskableGraphic[] graphics;
      8     private bool hideUnchecked = false;
      9     private bool showBorders = true;
     10     private Color borderColor = Color.blue;
     11     private Vector2 scrollPosition = Vector2.zero;
     12 
     13     private static RaycastTargetChecker instance = null;
     14 
     15     [MenuItem("Tools/RaycastTarget Checker")]
     16     private static void Open()
     17     {
     18         instance = instance ?? EditorWindow.GetWindow<RaycastTargetChecker>("RaycastTargets");
     19         instance.Show();
     20     }
     21 
     22     void OnEnable()
     23     {
     24         instance = this;
     25     }
     26 
     27     void OnDisable()
     28     {
     29         instance = null;
     30     }
     31 
     32     void OnGUI()
     33     {
     34         using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope())
     35         {
     36             showBorders = EditorGUILayout.Toggle("Show Gizmos", showBorders, GUILayout.Width(200.0f));
     37             borderColor = EditorGUILayout.ColorField(borderColor);
     38         }
     39         hideUnchecked = EditorGUILayout.Toggle("Hide Unchecked", hideUnchecked);
     40 
     41         GUILayout.Space(12.0f);
     42         Rect rect = GUILayoutUtility.GetLastRect();
     43         GUI.color = new Color(0.0f, 0.0f, 0.0f, 0.25f);
     44         GUI.DrawTexture(new Rect(0.0f, rect.yMin + 6.0f, Screen.width, 4.0f), EditorGUIUtility.whiteTexture);
     45         GUI.DrawTexture(new Rect(0.0f, rect.yMin + 6.0f, Screen.width, 1.0f), EditorGUIUtility.whiteTexture);
     46         GUI.DrawTexture(new Rect(0.0f, rect.yMin + 9.0f, Screen.width, 1.0f), EditorGUIUtility.whiteTexture);
     47         GUI.color = Color.white;
     48 
     49         graphics = GameObject.FindObjectsOfType<MaskableGraphic>();
     50 
     51         using (GUILayout.ScrollViewScope scrollViewScope = new GUILayout.ScrollViewScope(scrollPosition))
     52         {
     53             scrollPosition = scrollViewScope.scrollPosition;
     54             for (int i = 0; i < graphics.Length; i++)
     55             {
     56                 MaskableGraphic graphic = graphics[i];
     57                 if (hideUnchecked == false || graphic.raycastTarget == true)
     58                 {
     59                     DrawElement(graphic);
     60                 }
     61             }
     62         }
     63         foreach (var item in graphics)
     64         {
     65             EditorUtility.SetDirty(item);
     66         }
     67         Repaint();
     68     }
     69 
     70     private void DrawElement(MaskableGraphic graphic)
     71     {
     72         using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope())
     73         {
     74             Undo.RecordObject(graphic, "Modify RaycastTarget");
     75             graphic.raycastTarget = EditorGUILayout.Toggle(graphic.raycastTarget, GUILayout.Width(20));
     76             EditorGUI.BeginDisabledGroup(true);
     77             EditorGUILayout.ObjectField(graphic, typeof(MaskableGraphic), true);
     78             EditorGUI.EndDisabledGroup();
     79         }
     80     }
     81 
     82     [DrawGizmo(GizmoType.Selected | GizmoType.NonSelected)]
     83     private static void DrawGizmos(MaskableGraphic source, GizmoType gizmoType)
     84     {
     85         if (instance != null && instance.showBorders == true && source.raycastTarget == true)
     86         {
     87             Vector3[] corners = new Vector3[4];
     88             source.rectTransform.GetWorldCorners(corners);
     89             Gizmos.color = instance.borderColor;
     90             for (int i = 0; i < 4; i++)
     91             {
     92                 Gizmos.DrawLine(corners[i], corners[(i + 1) % 4]);
     93             }
     94             if (Selection.activeGameObject == source.gameObject)
     95             {
     96                 Gizmos.DrawLine(corners[0], corners[2]);
     97                 Gizmos.DrawLine(corners[1], corners[3]);
     98             }
     99         }
    100         SceneView.RepaintAll();
    101     }
    102 }

    效果如下:

    3.创建UI控件时自动取消RaycastTarget

     1 using UnityEngine;
     2 using UnityEditor;
     3 using UnityEngine.UI;
     4 
     5 public class CancelRaycastTarget {
     6 
     7     [MenuItem("GameObject/UI/Image")]
     8     static void CreatImage()
     9     {
    10         if (Selection.activeTransform)
    11         {
    12             if (Selection.activeTransform.GetComponentInParent<Canvas>())
    13             {
    14                 GameObject go = new GameObject("Image", typeof(Image));
    15                 go.GetComponent<Image>().raycastTarget = false;
    16                 go.transform.SetParent(Selection.activeTransform);
    17             }
    18         }
    19     }
    20 
    21     [MenuItem("GameObject/UI/Text")]
    22     static void CreatText()
    23     {
    24         if (Selection.activeTransform)
    25         {
    26             if (Selection.activeTransform.GetComponentInParent<Canvas>())
    27             {
    28                 GameObject go = new GameObject("Text", typeof(Text));  
    29                 go.GetComponent<Text>().raycastTarget = false;  
    30                 go.transform.SetParent(Selection.activeTransform);
    31             }
    32         }
    33     }
    34   
    35     [MenuItem("GameObject/UI/Raw Image")]
    36     static void CreatRawImage()
    37     {
    38         if (Selection.activeTransform)
    39         {
    40             if (Selection.activeTransform.GetComponentInParent<Canvas>())
    41             { 
    42                 GameObject go = new GameObject("RawImage", typeof(RawImage)); 
    43                 go.GetComponent<RawImage>().raycastTarget = false;
    44                 go.transform.SetParent(Selection.activeTransform);
    45             }
    46         }
    47     }
    48 }
  • 相关阅读:
    重要网址(工具的使用)
    navicat的使用(测试库和正式库同步)以及用plsql改表字段属性
    linux
    linux一些基本命令
    LINUX的一些基本概念和操作
    LINUX简介
    Redis系统管理
    Redis之数据类型
    Redis的简介与安装
    论文阅读笔记(二十七)【AAAI2019】:A Bottom-Up Clustering Approach to Unsupervised Person Re-Identification
  • 原文地址:https://www.cnblogs.com/lyh916/p/9748898.html
Copyright © 2020-2023  润新知