当前位置:   article > 正文

Unity编辑器Editor常用命令_editor 命令

editor 命令
因为Unity的编辑器一些方法,是项目中不常用到的,为了避免以后忘记
今天特意抽时间把最近使用和一些不常用的Editor用到的方法都列举出来
有些写了注释,后面就懒得写注释了,不过看预览效果,都能找得到。
上预览图:

具体内容直接上代码:
  1. usingUnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. public class EditorTest : EditorWindow
  5. {
  6. enumEnumTest { 网, 虫, 测, 试 }
  7. staticEditorTest window;
  8. [MenuItem("Tools/测试编辑器功能")]
  9. staticvoid Test()
  10. {
  11. window = (EditorTest)EditorWindow.GetWindow(typeof(EditorTest),false, "测试编辑器功能");
  12. window.Show();
  13. }
  14. #region 属性
  15. inttestInt = 0;
  16. floattestFloat = 0;
  17. floatfloatSlider = 0;
  18. floatmaxValue = 20;
  19. floatminValue = -50;
  20. stringtestStr = "网虫虫";
  21. booltestBool = true;
  22. inttoolbarOption = 0;
  23. string[] toolbarStr =new string[] {"这", "是", "Toolbar"};
  24. EnumTest enumTest;
  25. EnumTest enumTest2;
  26. intenumInt = 0;
  27. intselectedSize = 1;
  28. string[] names = {"网", "虫", "测","试" };
  29. int[] sizes = { 1, 2, 4 };
  30. stringtagStr = "";
  31. intlayerInt = 0;
  32. intmaskInt = 0;
  33. Vector3 testVector3;
  34. Color testColor;
  35. Rect testRect;
  36. GameObject gameObject;
  37. Texture texture;
  38. boolisShowScrollView = false;
  39. Vector2 scrollPosition;
  40. #endregion
  41. privatevoid OnGUI()
  42. {
  43. this.Repaint(); // 强制重绘
  44. if(secondWindow || secondWindow2)
  45. GUI.enabled =false;
  46. GUILayout.Label(testStr); // 文本格式:网虫虫 不可输入、不可选
  47. GUIStyle fontStyle =new GUIStyle();
  48. fontStyle.normal.background =null; //设置背景填充
  49. fontStyle.normal.textColor =new Color(1, 0, 0); //设置字体颜色
  50. fontStyle.fontStyle = FontStyle.BoldAndItalic; // 字体加粗倾斜
  51. fontStyle.fontSize = 18; //字体大小
  52. GUILayout.Label(testStr, fontStyle); // 文本格式:网虫虫 不可输入、不可选 添加字体样式
  53. GUILayout.TextField(testStr); // 文本格式:网虫虫 可输入、不可选
  54. EditorGUILayout.LabelField("姓名:", testStr); // 文本格式: 姓名:网虫虫 不可输入、不可选
  55. testStr = EditorGUILayout.TextField("姓名:", testStr); // 文本格式:姓名:网虫虫 可输入、不可选
  56. testInt = EditorGUILayout.IntField("IntField:", testInt);
  57. testFloat = EditorGUILayout.FloatField("FloatField:", testFloat);
  58. testStr = GUILayout.TextArea(testStr, GUILayout.Height(40)); // 区域输入文本
  59. EditorGUILayout.SelectableLabel(testStr); // 可选择文本
  60. testStr = GUILayout.PasswordField(testStr,"*"[0]);
  61. testStr = EditorGUILayout.PasswordField("密码:", testStr);
  62. floatSlider = EditorGUILayout.Slider(floatSlider, 1, 100);
  63. EditorGUILayout.MinMaxSlider(refminValue, refmaxValue, -100, 100);
  64. testBool = GUILayout.Toggle(testBool,"开关");
  65. testBool = EditorGUILayout.Toggle("开关:", testBool);
  66. toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarStr);
  67. switch(toolbarOption)
  68. {
  69. case0:
  70. GUILayout.Label("1111111111111111111");
  71. break;
  72. case1:
  73. GUILayout.Label("2222222222222222222");
  74. break;
  75. case2:
  76. GUILayout.Label("3333333333333333333");
  77. break;
  78. }
  79. EditorGUILayout.Space(); // 空一行
  80. enumTest = (EnumTest)EditorGUILayout.EnumPopup("Enum类型Popup:", enumTest);
  81. enumTest2 = (EnumTest)EditorGUILayout.EnumMaskField("Enum枚举多选:", enumTest2);
  82. enumInt = EditorGUILayout.Popup("String类型Popup:", enumInt, names);
  83. selectedSize = EditorGUILayout.IntPopup("Int类型Popup: ", selectedSize, names, sizes);
  84. tagStr = EditorGUILayout.TagField("选择Tag:", tagStr);
  85. layerInt = EditorGUILayout.LayerField("选择Layer:", layerInt);
  86. maskInt = EditorGUILayout.MaskField("数组多选:", maskInt, names);
  87. testColor = EditorGUILayout.ColorField("颜色:", testColor);
  88. GUI.backgroundColor = Color.magenta; // 修改背景颜色
  89. testVector3 = EditorGUILayout.Vector3Field("Vector3坐标:", testVector3);
  90. GUI.backgroundColor = Color.green;
  91. testRect = EditorGUILayout.RectField("Rect尺寸:", testRect);
  92. GUI.backgroundColor = Color.gray * 1.8f; // 恢复背景默认颜色
  93. gameObject = (GameObject)EditorGUILayout.ObjectField("任意类型 举例GameObject:", gameObject,typeof(GameObject));
  94. texture = EditorGUILayout.ObjectField("任意类型 举例贴图", texture,typeof(Texture),true) as Texture;
  95. GUILayout.BeginHorizontal();
  96. GUILayout.Label("横向自动排列演示:");
  97. testStr = GUILayout.PasswordField(testStr,"*"[0]);
  98. testBool = GUILayout.Toggle(testBool,"开关");
  99. GUILayout.Button("按钮");
  100. GUILayout.EndHorizontal();
  101. if(GUILayout.Button("点击按钮 弹出系统提示消息"))
  102. ShowNotification(newGUIContent("这是网虫虫提示消息~~"));
  103. if(GUILayout.Button("点击按钮 显示滚动视图"))
  104. isShowScrollView = !isShowScrollView;
  105. if(isShowScrollView)
  106. {
  107. scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  108. for(int i = 0; i < 100; i++)
  109. {
  110. GUILayout.Label(i.ToString());
  111. }
  112. GUILayout.EndScrollView();
  113. }
  114. if(GUILayout.Button("点击按钮 显示二级窗口"))
  115. secondWindow = !secondWindow;
  116. if(secondWindow)
  117. {
  118. GUI.enabled =true;
  119. BeginWindows();
  120. secondWindowRect = GUILayout.Window(1, secondWindowRect, SecondWindow,"二级窗口");
  121. EndWindows();
  122. }
  123. if(GUILayout.Button("点击按钮 绘制图形"))
  124. secondWindow2 = !secondWindow2;
  125. if(secondWindow2)
  126. {
  127. GUI.backgroundColor = Color.red / 2; // 修改背景颜色
  128. GUI.enabled =true;
  129. BeginWindows();
  130. secondWindowRect2 = GUILayout.Window(2, secondWindowRect2, SecondWindow2,"绘制图形");
  131. EndWindows();
  132. }
  133. GUI.backgroundColor = Color.gray * 1.8f; // 恢复背景默认颜色
  134. GUI.enabled =false;
  135. GUILayout.Button("置灰按钮");
  136. GUI.enabled =true;
  137. }
  138. Rect secondWindowRect =new Rect(0, 0, 400, 400);
  139. boolsecondWindow = false;
  140. voidSecondWindow(intunusedWindowID)
  141. {
  142. Application.targetFrameRate = EditorGUILayout.IntSlider("限定帧率:", Application.targetFrameRate, 10, 300);
  143. Application.runInBackground = EditorGUILayout.Toggle("允许Unity后台运行:", Application.runInBackground);
  144. gameObject = (GameObject)EditorGUILayout.ObjectField("当前选中的物体:", Selection.activeGameObject,typeof(GameObject));
  145. EditorGUILayout.Vector3Field("鼠标在Scene视图的坐标:", mousePosition);
  146. EditorGUILayout.Vector3Field("鼠标在当前二级窗口坐标:", Event.current.mousePosition);
  147. hitGo = (GameObject)EditorGUILayout.ObjectField("鼠标发送射线:", hitGo !=null ? hitGo :null, typeof(GameObject));
  148. GUILayout.Label("UsedTextureCount: "+ UnityStats.usedTextureCount);
  149. GUILayout.Label("UsedTextureMemorySize: "+ (UnityStats.usedTextureMemorySize / 1000000f + "Mb"));
  150. GUILayout.Label("RenderTextureCount: "+ UnityStats.renderTextureCount);
  151. GUILayout.Label("FrameTime: "+ UnityStats.frameTime);
  152. GUILayout.Label("RenderTime: "+ UnityStats.renderTime);
  153. GUILayout.Label("DrawCalls: "+ UnityStats.drawCalls);
  154. GUILayout.Label("Batchs: "+ UnityStats.batches);
  155. GUILayout.Label("Static Batch DC: "+ UnityStats.staticBatchedDrawCalls);
  156. GUILayout.Label("Static Batch: "+ UnityStats.staticBatches);
  157. GUILayout.Label("DynamicBatch DC: "+ UnityStats.dynamicBatchedDrawCalls);
  158. GUILayout.Label("DynamicBatch: "+ UnityStats.dynamicBatches);
  159. GUILayout.Label("Triangles: "+ UnityStats.triangles);
  160. GUILayout.Label("Vertices: "+ UnityStats.vertices);
  161. if(GUILayout.Button("关闭二级窗口"))
  162. secondWindow =false;
  163. GUI.DragWindow();//画出子窗口
  164. }
  165. Rect secondWindowRect2 =new Rect(0, 0, 400, 400);
  166. boolsecondWindow2 = false;
  167. intcapSize = -50;
  168. Vector3 capEuler =new Vector3(200, 200, 200);
  169. voidSecondWindow2(intunusedWindowID)
  170. {
  171. capSize = EditorGUILayout.IntField("尺寸:", capSize);
  172. capEuler = EditorGUILayout.Vector3Field("testVector3:", capEuler);
  173. if(GUILayout.Button("关闭绘制图形"))
  174. secondWindow2 =false;
  175. Handles.color = Color.red;
  176. Handles.DrawLine(newVector2(75, 100), newVector3(150, 200));
  177. Handles.CircleCap(1,new Vector2(300, 150), Quaternion.identity, capSize);
  178. Handles.color = Color.green;
  179. Handles.SphereCap(2,new Vector2(100, 250), Quaternion.Euler(capEuler), capSize);
  180. Handles.CubeCap(3,new Vector2(300, 250), Quaternion.Euler(capEuler), capSize);
  181. Handles.color = Color.blue;
  182. Handles.CylinderCap(4,new Vector2(100, 350), Quaternion.Euler(capEuler), capSize);
  183. Handles.ConeCap(5,new Vector2(300, 350), Quaternion.Euler(capEuler), capSize);
  184. GUI.DragWindow();//画出子窗口
  185. }
  186. privatevoid OnEnable()
  187. {
  188. SceneView.onSceneGUIDelegate += SceneGUI;
  189. }
  190. privatevoid OnDisable()
  191. {
  192. SceneView.onSceneGUIDelegate -= SceneGUI;
  193. }
  194. GameObject hitGo;
  195. Vector3 mousePosition;
  196. voidSceneGUI(SceneView sceneView)
  197. {
  198. if(Event.current.type == EventType.mouseMove)
  199. {
  200. mousePosition = Event.current.mousePosition;
  201. RaycastHit hit;
  202. Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
  203. if(Physics.Raycast(ray, outhit))
  204. {
  205. hitGo = hit.collider.gameObject;
  206. }
  207. else
  208. hitGo =null;
  209. }
  210. }
  211. }

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/92967
推荐阅读
相关标签
  

闽ICP备14008679号