如果项目UI管理不好,前期会出现很多不需要点击事件,但又打开着UI的Raycast Target属性,这个是很耗性能的。
然而 项目进行到一定时间再去管理UI射线,显然是很麻烦耗时的。
今天给大家通过Unity编辑器 来实现批量修改UI射线属性的工具。
工具图:
使用方法:
一、将下面脚本Copy到项目里,然后点击“Tools/检查UGUI射线”按钮
二、在“Hierarchy”或“Project”面板选择想要修改射线的预设体(可以多选)
三、点击“检查选中UI”按钮,然后所有带射线的UI都会显示在下面
四、可以点击每个UI前方的单选框按钮来设置UI射线状态、也可以点击“移除全部射线”按钮来移除当前显示的所有射线
五、改后可以查看之前选择的预设体,看看是不是改好了呢~~
上脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
using UnityEngine; using System.Collections; using UnityEditor; using UnityEngine.UI; using System.Collections.Generic; public class CheckRaycastUtil : EditorWindow { private List<Image> imageList; private List<RawImage> rawimageList; private List<Text> textList; private bool includeInactive = true; private Transform[] selectTrans; private Vector2 scrollPosition; static CheckRaycastUtil window; [MenuItem("Tools/检查UGUI射线")] static void ModifyAtlas() { window = (CheckRaycastUtil)EditorWindow.GetWindow(typeof(CheckRaycastUtil), false, "检查UGUI射线"); window.Show(); } private void OnEnable() { imageList = new List<Image>(); rawimageList = new List<RawImage>(); textList = new List<Text>(); } private void OnGUI() { EditorGUILayout.Space(); includeInactive = EditorGUILayout.Toggle("包含隐藏物体:", includeInactive); EditorGUILayout.Space(); if (GUILayout.Button("检查选中UI")) { CheckSelected(); } EditorGUILayout.Space(); if (GUILayout.Button("移除全部射线")) { RemoveRaycast(); } // 显示选中的(含子节点)的所有Image\RawImage\Text int allCount = imageList.Count + rawimageList.Count + textList.Count; if (allCount > 0) { int index = 0; scrollPosition = GUI.BeginScrollView(new Rect(0, 120, Screen.width, 500), scrollPosition, new Rect(0, 120, Screen.width, allCount * 20)); for (int i = 0; i < imageList.Count; i++) { index++; imageList[i] = EditorGUI.ObjectField(new Rect(30, 120 + index * 20, Screen.width, 20), imageList[i].name, imageList[i], typeof(Image)) as Image; imageList[i].raycastTarget = EditorGUI.Toggle(new Rect(5, 120 + index * 20, 20, 20), imageList[i].raycastTarget); } for (int i = 0; i < rawimageList.Count; i++) { index++; rawimageList[i] = EditorGUI.ObjectField(new Rect(30, 120 + index * 20, Screen.width, 20), rawimageList[i].name, rawimageList[i], typeof(RawImage)) as RawImage; rawimageList[i].raycastTarget = EditorGUI.Toggle(new Rect(5, 120 + index * 20, 20, 20), rawimageList[i].raycastTarget); } for (int i = 0; i < textList.Count; i++) { index++; textList[i] = EditorGUI.ObjectField(new Rect(30, 120 + index * 20, Screen.width, 20), textList[i].name, textList[i], typeof(Text)) as Text; textList[i].raycastTarget = EditorGUI.Toggle(new Rect(5, 120 + index * 20, 20, 20), textList[i].raycastTarget); } GUI.EndScrollView(); } } /// <summary> /// 检查已选中的项 /// </summary> void CheckSelected() { imageList = new List<Image>(); rawimageList = new List<RawImage>(); textList = new List<Text>(); selectTrans = Selection.GetTransforms(SelectionMode.TopLevel); if (selectTrans == null) { Debug.LogError("没有选中UI。"); return; } for (int i = 0; i < selectTrans.Length; i++) { Image[] images = selectTrans[i].GetComponentsInChildren<Image>(includeInactive); for (int j = 0; j < images.Length; j++) { if (images[j] != null) imageList.Add(images[j]); } RawImage[] rawimages = selectTrans[i].GetComponentsInChildren<RawImage>(includeInactive); for (int j = 0; j < rawimages.Length; j++) { if (rawimages[j] != null) rawimageList.Add(rawimages[j]); } Text[] texts = selectTrans[i].GetComponentsInChildren<Text>(includeInactive); for (int j = 0; j < texts.Length; j++) { if (texts[j] != null) textList.Add(texts[j]); } } } /// <summary> /// 移除UI射线 /// </summary> void RemoveRaycast() { for (int i = 0; i < imageList.Count; i++) { imageList[i].raycastTarget = false; } for (int i = 0; i < rawimageList.Count; i++) { rawimageList[i].raycastTarget = false; } for (int i = 0; i < textList.Count; i++) { textList[i].raycastTarget = false; } } } |
- 本文固定链接: http://www.u3d8.com/?p=1283
- 转载请注明: 网虫虫 在 u3d8.com 发表过