因为Unity的编辑器一些方法,是项目中不常用到的,为了避免以后忘记
今天特意抽时间把最近使用和一些不常用的Editor用到的方法都列举出来
有些写了注释,后面就懒得写注释了,不过看预览效果,都能找得到。
上预览图:
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
using UnityEngine; using System.Collections; using UnityEditor; public class EditorTest : EditorWindow { enum EnumTest { 网, 虫, 测, 试 } static EditorTest window; [MenuItem("Tools/测试编辑器功能")] static void Test() { window = (EditorTest)EditorWindow.GetWindow(typeof(EditorTest), false, "测试编辑器功能"); window.Show(); } #region 属性 int testInt = 0; float testFloat = 0; float floatSlider = 0; float maxValue = 20; float minValue = -50; string testStr = "网虫虫"; bool testBool = true; int toolbarOption = 0; string[] toolbarStr = new string[] { "这", "是", "Toolbar" }; EnumTest enumTest; EnumTest enumTest2; int enumInt = 0; int selectedSize = 1; string[] names = { "网", "虫", "测", "试" }; int[] sizes = { 1, 2, 4 }; string tagStr = ""; int layerInt = 0; int maskInt = 0; Vector3 testVector3; Color testColor; Rect testRect; GameObject gameObject; Texture texture; bool isShowScrollView = false; Vector2 scrollPosition; #endregion private void OnGUI() { this.Repaint(); // 强制重绘 if(secondWindow || secondWindow2) GUI.enabled = false; GUILayout.Label(testStr); // 文本格式:网虫虫 不可输入、不可选 GUIStyle fontStyle = new GUIStyle(); fontStyle.normal.background = null; //设置背景填充 fontStyle.normal.textColor = new Color(1, 0, 0); //设置字体颜色 fontStyle.fontStyle = FontStyle.BoldAndItalic; // 字体加粗倾斜 fontStyle.fontSize = 18; //字体大小 GUILayout.Label(testStr, fontStyle); // 文本格式:网虫虫 不可输入、不可选 添加字体样式 GUILayout.TextField(testStr); // 文本格式:网虫虫 可输入、不可选 EditorGUILayout.LabelField("姓名:", testStr); // 文本格式: 姓名:网虫虫 不可输入、不可选 testStr = EditorGUILayout.TextField("姓名:", testStr); // 文本格式:姓名:网虫虫 可输入、不可选 testInt = EditorGUILayout.IntField("IntField:", testInt); testFloat = EditorGUILayout.FloatField("FloatField:", testFloat); testStr = GUILayout.TextArea(testStr, GUILayout.Height(40)); // 区域输入文本 EditorGUILayout.SelectableLabel(testStr); // 可选择文本 testStr = GUILayout.PasswordField(testStr, "*"[0]); testStr = EditorGUILayout.PasswordField("密码:", testStr); floatSlider = EditorGUILayout.Slider(floatSlider, 1, 100); EditorGUILayout.MinMaxSlider(ref minValue, ref maxValue, -100, 100); testBool = GUILayout.Toggle(testBool, "开关"); testBool = EditorGUILayout.Toggle("开关:", testBool); toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarStr); switch (toolbarOption) { case 0: GUILayout.Label("1111111111111111111"); break; case 1: GUILayout.Label("2222222222222222222"); break; case 2: GUILayout.Label("3333333333333333333"); break; } EditorGUILayout.Space(); // 空一行 enumTest = (EnumTest)EditorGUILayout.EnumPopup("Enum类型Popup:", enumTest); enumTest2 = (EnumTest)EditorGUILayout.EnumMaskField("Enum枚举多选:", enumTest2); enumInt = EditorGUILayout.Popup("String类型Popup:", enumInt, names); selectedSize = EditorGUILayout.IntPopup("Int类型Popup: ", selectedSize, names, sizes); tagStr = EditorGUILayout.TagField("选择Tag:", tagStr); layerInt = EditorGUILayout.LayerField("选择Layer:", layerInt); maskInt = EditorGUILayout.MaskField("数组多选:", maskInt, names); testColor = EditorGUILayout.ColorField("颜色:", testColor); GUI.backgroundColor = Color.magenta; // 修改背景颜色 testVector3 = EditorGUILayout.Vector3Field("Vector3坐标:", testVector3); GUI.backgroundColor = Color.green; testRect = EditorGUILayout.RectField("Rect尺寸:", testRect); GUI.backgroundColor = Color.gray * 1.8f; // 恢复背景默认颜色 gameObject = (GameObject)EditorGUILayout.ObjectField("任意类型 举例GameObject:", gameObject, typeof(GameObject)); texture = EditorGUILayout.ObjectField("任意类型 举例贴图", texture, typeof(Texture), true) as Texture; GUILayout.BeginHorizontal(); GUILayout.Label("横向自动排列演示:"); testStr = GUILayout.PasswordField(testStr, "*"[0]); testBool = GUILayout.Toggle(testBool, "开关"); GUILayout.Button("按钮"); GUILayout.EndHorizontal(); if (GUILayout.Button("点击按钮 弹出系统提示消息")) ShowNotification(new GUIContent("这是网虫虫提示消息~~")); if (GUILayout.Button("点击按钮 显示滚动视图")) isShowScrollView = !isShowScrollView; if (isShowScrollView) { scrollPosition = GUILayout.BeginScrollView(scrollPosition); for (int i = 0; i < 100; i++) { GUILayout.Label(i.ToString()); } GUILayout.EndScrollView(); } if (GUILayout.Button("点击按钮 显示二级窗口")) secondWindow = !secondWindow; if (secondWindow) { GUI.enabled = true; BeginWindows(); secondWindowRect = GUILayout.Window(1, secondWindowRect, SecondWindow, "二级窗口"); EndWindows(); } if (GUILayout.Button("点击按钮 绘制图形")) secondWindow2 = !secondWindow2; if (secondWindow2) { GUI.backgroundColor = Color.red / 2; // 修改背景颜色 GUI.enabled = true; BeginWindows(); secondWindowRect2 = GUILayout.Window(2, secondWindowRect2, SecondWindow2, "绘制图形"); EndWindows(); } GUI.backgroundColor = Color.gray * 1.8f; // 恢复背景默认颜色 GUI.enabled = false; GUILayout.Button("置灰按钮"); GUI.enabled = true; } Rect secondWindowRect = new Rect(0, 0, 400, 400); bool secondWindow = false; void SecondWindow(int unusedWindowID) { Application.targetFrameRate = EditorGUILayout.IntSlider("限定帧率:", Application.targetFrameRate, 10, 300); Application.runInBackground = EditorGUILayout.Toggle("允许Unity后台运行:", Application.runInBackground); gameObject = (GameObject)EditorGUILayout.ObjectField("当前选中的物体:", Selection.activeGameObject, typeof(GameObject)); EditorGUILayout.Vector3Field("鼠标在Scene视图的坐标:", mousePosition); EditorGUILayout.Vector3Field("鼠标在当前二级窗口坐标:", Event.current.mousePosition); hitGo = (GameObject)EditorGUILayout.ObjectField("鼠标发送射线:", hitGo != null ? hitGo : null, typeof(GameObject)); GUILayout.Label("UsedTextureCount: " + UnityStats.usedTextureCount); GUILayout.Label("UsedTextureMemorySize: " + (UnityStats.usedTextureMemorySize / 1000000f + "Mb")); GUILayout.Label("RenderTextureCount: " + UnityStats.renderTextureCount); GUILayout.Label("FrameTime: " + UnityStats.frameTime); GUILayout.Label("RenderTime: " + UnityStats.renderTime); GUILayout.Label("DrawCalls: " + UnityStats.drawCalls); GUILayout.Label("Batchs: " + UnityStats.batches); GUILayout.Label("Static Batch DC: " + UnityStats.staticBatchedDrawCalls); GUILayout.Label("Static Batch: " + UnityStats.staticBatches); GUILayout.Label("DynamicBatch DC: " + UnityStats.dynamicBatchedDrawCalls); GUILayout.Label("DynamicBatch: " + UnityStats.dynamicBatches); GUILayout.Label("Triangles: " + UnityStats.triangles); GUILayout.Label("Vertices: " + UnityStats.vertices); if (GUILayout.Button("关闭二级窗口")) secondWindow = false; GUI.DragWindow();//画出子窗口 } Rect secondWindowRect2 = new Rect(0, 0, 400, 400); bool secondWindow2 = false; int capSize = -50; Vector3 capEuler = new Vector3(200, 200, 200); void SecondWindow2(int unusedWindowID) { capSize = EditorGUILayout.IntField("尺寸:", capSize); capEuler = EditorGUILayout.Vector3Field("testVector3:", capEuler); if (GUILayout.Button("关闭绘制图形")) secondWindow2 = false; Handles.color = Color.red; Handles.DrawLine(new Vector2(75, 100), new Vector3(150, 200)); Handles.CircleCap(1, new Vector2(300, 150), Quaternion.identity, capSize); Handles.color = Color.green; Handles.SphereCap(2, new Vector2(100, 250), Quaternion.Euler(capEuler), capSize); Handles.CubeCap(3, new Vector2(300, 250), Quaternion.Euler(capEuler), capSize); Handles.color = Color.blue; Handles.CylinderCap(4, new Vector2(100, 350), Quaternion.Euler(capEuler), capSize); Handles.ConeCap(5, new Vector2(300, 350), Quaternion.Euler(capEuler), capSize); GUI.DragWindow();//画出子窗口 } private void OnEnable() { SceneView.onSceneGUIDelegate += SceneGUI; } private void OnDisable() { SceneView.onSceneGUIDelegate -= SceneGUI; } GameObject hitGo; Vector3 mousePosition; void SceneGUI(SceneView sceneView) { if (Event.current.type == EventType.mouseMove) { mousePosition = Event.current.mousePosition; RaycastHit hit; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); if (Physics.Raycast(ray, out hit)) { hitGo = hit.collider.gameObject; } else hitGo = null; } } } |
- 本文固定链接: http://www.u3d8.com/?p=1323
- 转载请注明: 网虫虫 在 u3d8.com 发表过