当前位置:   article > 正文

Unity 编辑器扩展(六)EditorWindow_unity editorwindow 宽高

unity editorwindow 宽高

EditorWindow

引入命名空间“using UnityEditor;”,继承这个类的编辑器脚本可以用来创建一个编辑器窗口。脚本放到Editor文件夹下。 

创建窗口 

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. public class Test:EditorWindow
  7. {
  8. [MenuItem("Window/TestWindow")]
  9. static void MyTest()
  10. {
  11. Test myWindow = (Test)EditorWindow.GetWindow(typeof(Test), false, "MyWindow", true);//创建窗口
  12. myWindow.Show();//展示
  13. }
  14. }

填充内容 

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. public class Test:EditorWindow
  7. {
  8. int param1 = 1;
  9. float param2 = 0.5f;
  10. string theName = "string";
  11. [MenuItem("Window/TestWindow")]
  12. static void MyTest()
  13. {
  14. Test myWindow = (Test)EditorWindow.GetWindow(typeof(Test), true, "MyWindow", true);//创建窗口
  15. myWindow.Show();//展示
  16. }
  17. int i = 0;
  18. void OnGUI()
  19. {
  20. GUILayout.Label("一", EditorStyles.boldLabel);//标题一
  21. param1 = EditorGUILayout.IntField("param1 int", param1);//参数:类型为int,初始值为1
  22. if (GUILayout.Button("TestButton"))//按钮
  23. {
  24. Debug.Log("TestButton");
  25. }
  26. GUILayout.Label("二", EditorStyles.boldLabel);//标题二
  27. param2 = EditorGUILayout.FloatField("param2 float", param2);//参数:类型为float,初始值为0.5f
  28. theName = EditorGUILayout.TextField("TestName", theName);
  29. i = EditorGUILayout.IntSlider("TestIntSlider", i, 0, 100);//滑动条
  30. //打印当前选中的窗口名称
  31. EditorGUILayout.LabelField(EditorWindow.focusedWindow.ToString());
  32. }
  33. }

 

创建窗口,指定区域大小 

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. public class Test:EditorWindow
  7. {
  8. int param1 = 1;
  9. float param2 = 0.5f;
  10. string theName = "string";
  11. [MenuItem("Window/TestWindow")]
  12. static void MyTest()
  13. {
  14. Rect r = new Rect(0, 0, 100, 100);
  15. Test window = (Test)EditorWindow.GetWindowWithRect(typeof(Test), r, true, "widow name");
  16. window.Show();
  17. }
  18. int i = 0;
  19. void OnGUI()
  20. {
  21. GUILayout.Label("一", EditorStyles.boldLabel);//标题一
  22. param1 = EditorGUILayout.IntField("param1 int", param1);//参数:类型为int,初始值为1
  23. if (GUILayout.Button("TestButton"))//按钮
  24. {
  25. Debug.Log("TestButton");
  26. }
  27. GUILayout.Label("二", EditorStyles.boldLabel);//标题二
  28. param2 = EditorGUILayout.FloatField("param2 float", param2);//参数:类型为float,初始值为0.5f
  29. theName = EditorGUILayout.TextField("TestName", theName);
  30. i = EditorGUILayout.IntSlider("TestIntSlider", i, 0, 100);//滑动条
  31. //打印当前选中的窗口名称
  32. EditorGUILayout.LabelField(EditorWindow.focusedWindow.ToString());
  33. }
  34. }

EditorWindow.GetWindowWithRect() 和 EditorWindow.GetWindow()都可以创建一个窗口。
前者可以规定窗口的固定区域,后者可通过鼠标动态的延伸窗口。

 

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

闽ICP备14008679号