赞
踩
EditorWindow
引入命名空间“using UnityEditor;”,继承这个类的编辑器脚本可以用来创建一个编辑器窗口。脚本放到Editor文件夹下。
创建窗口
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
-
- public class Test:EditorWindow
- {
- [MenuItem("Window/TestWindow")]
- static void MyTest()
- {
- Test myWindow = (Test)EditorWindow.GetWindow(typeof(Test), false, "MyWindow", true);//创建窗口
- myWindow.Show();//展示
- }
- }
填充内容
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
-
- public class Test:EditorWindow
- {
- int param1 = 1;
- float param2 = 0.5f;
- string theName = "string";
-
- [MenuItem("Window/TestWindow")]
- static void MyTest()
- {
- Test myWindow = (Test)EditorWindow.GetWindow(typeof(Test), true, "MyWindow", true);//创建窗口
- myWindow.Show();//展示
- }
- int i = 0;
- void OnGUI()
- {
- GUILayout.Label("一", EditorStyles.boldLabel);//标题一
- param1 = EditorGUILayout.IntField("param1 int", param1);//参数:类型为int,初始值为1
- if (GUILayout.Button("TestButton"))//按钮
- {
- Debug.Log("TestButton");
- }
-
- GUILayout.Label("二", EditorStyles.boldLabel);//标题二
- param2 = EditorGUILayout.FloatField("param2 float", param2);//参数:类型为float,初始值为0.5f
- theName = EditorGUILayout.TextField("TestName", theName);
- i = EditorGUILayout.IntSlider("TestIntSlider", i, 0, 100);//滑动条
-
- //打印当前选中的窗口名称
- EditorGUILayout.LabelField(EditorWindow.focusedWindow.ToString());
- }
- }
创建窗口,指定区域大小
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
-
- public class Test:EditorWindow
- {
- int param1 = 1;
- float param2 = 0.5f;
- string theName = "string";
-
- [MenuItem("Window/TestWindow")]
- static void MyTest()
- {
- Rect r = new Rect(0, 0, 100, 100);
- Test window = (Test)EditorWindow.GetWindowWithRect(typeof(Test), r, true, "widow name");
- window.Show();
- }
- int i = 0;
- void OnGUI()
- {
- GUILayout.Label("一", EditorStyles.boldLabel);//标题一
- param1 = EditorGUILayout.IntField("param1 int", param1);//参数:类型为int,初始值为1
- if (GUILayout.Button("TestButton"))//按钮
- {
- Debug.Log("TestButton");
- }
-
- GUILayout.Label("二", EditorStyles.boldLabel);//标题二
- param2 = EditorGUILayout.FloatField("param2 float", param2);//参数:类型为float,初始值为0.5f
- theName = EditorGUILayout.TextField("TestName", theName);
- i = EditorGUILayout.IntSlider("TestIntSlider", i, 0, 100);//滑动条
-
- //打印当前选中的窗口名称
- EditorGUILayout.LabelField(EditorWindow.focusedWindow.ToString());
- }
- }
EditorWindow.GetWindowWithRect() 和 EditorWindow.GetWindow()都可以创建一个窗口。
前者可以规定窗口的固定区域,后者可通过鼠标动态的延伸窗口。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。