4.0创建编辑器窗体EditorWindow
2021-02-18 本文已影响0人
莫忘初心_倒霉熊
编辑器窗体类都需要继承自EditorWindow类,代码中有详细注释:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class MyEditorWindow : EditorWindow
{
public static MyEditorWindow instance;
//TextField
private string textField = "textField";
//TextArea
private string textArea = "这是一个textArea,可以用来多行输入文本\ntextArea1\ntextArea2";
//ScrollView相关变量
private Vector2 scrolViewPos=Vector2.zero;
//Toolbar相关变量
private List<string> listToolbar = new List<string>()
{
"tab0",
"tab1",
"tab2",
"tab3"
};
private int indexToolbar = 0;
//enum
private enum EnumPopup
{
EnumPopup0,
EnumPopup1,
EnumPopup2
}
private EnumPopup selectedEnumPopup;
//各种类型
private int intField = 0;
private float floatField = 1.0f;
private Texture2D texture2DField;
private Vector3 vector3Field;
private Color colorField;
private Animation animationField;
//菜单路径后,可以添加快捷键如#_y,具体含义请看下图
[MenuItem("Tools/ShowMyEditorWindow #_y")]
public static void ShowWindow()
{
instance = EditorWindow.GetWindow<MyEditorWindow>();
instance.titleContent = new GUIContent("MyEditorWindow");
}
private void OnEnable()
{
Debug.Log("OnEnable");
}
public void OnDisable()
{
Debug.Log("OnDisable");
}
private void OnGUI()
{
//文本框LabelField 字体加粗
EditorGUILayout.LabelField("这是一个EditorGUILayout.LabelField",EditorStyles.boldLabel);
//textField
textField = EditorGUILayout.TextField("这是一个TextField",textField);
//TextArea
textArea = EditorGUILayout.TextArea(textArea);
//Box
GUILayout.Box("这是一个Box");
//tab按钮
indexToolbar = GUILayout.Toolbar(indexToolbar, listToolbar.ToArray());
//scrolView 宽300,高200
scrolViewPos = GUILayout.BeginScrollView(scrolViewPos,GUILayout.Width(300),GUILayout.Height(200));
//grid Layout
//AssetPreview.GetAssetPreview()用于生成资源的预览图
Texture2D preview = AssetPreview.GetAssetPreview(Resources.Load<GameObject>("Player"));
List<GUIContent> guiContents = new List<GUIContent>();
for (int i = 0; i < 13; i++)
{
GUIContent guiContent = new GUIContent();
guiContent.text ="tab"+indexToolbar+" item"+i;
guiContent.image = preview;
guiContents.Add(guiContent);
}
GUIStyle guiStyle = new GUIStyle(GUI.skin.button);
guiStyle.alignment = TextAnchor.LowerCenter;
guiStyle.imagePosition = ImagePosition.ImageAbove;
guiStyle.fixedWidth = 80;
guiStyle.fixedHeight = 90;
int selectionGridIndex = -1;
selectionGridIndex = GUILayout.SelectionGrid(selectionGridIndex,guiContents.ToArray(), 5, guiStyle);
if(selectionGridIndex!=-1)
Debug.Log("选中了tab"+indexToolbar+" Item"+selectionGridIndex);
GUILayout.EndScrollView();
//Button
if (GUILayout.Button("Button"))
{
Debug.Log("我是一个Button");
}
//水平
EditorGUILayout.BeginHorizontal();
GUILayout.Button("HorizontalButton");
GUILayout.Button("HorizontalButton");
GUILayout.Button("HorizontalButton");
EditorGUILayout.EndHorizontal();
//垂直
EditorGUILayout.BeginVertical();
GUILayout.Button("VerticalButton");
GUILayout.Button("VerticalButton");
GUILayout.Button("VerticalButton");
EditorGUILayout.EndHorizontal();
//EnumPopup
selectedEnumPopup =(EnumPopup) EditorGUILayout.EnumPopup("EnumPopup", selectedEnumPopup);
//三种帮助框
EditorGUILayout.HelpBox("Warning HelpBox", MessageType.Info);
EditorGUILayout.HelpBox("Warning HelpBox", MessageType.Warning);
EditorGUILayout.HelpBox("Warning HelpBox", MessageType.Error);
//int
intField = EditorGUILayout.IntField("IntField", intField);
//float
floatField = EditorGUILayout.FloatField("FloatField", floatField);
//object
texture2DField =
(Texture2D) EditorGUILayout.ObjectField("ObjectField", texture2DField, typeof(Texture2D), true);
vector3Field = EditorGUILayout.Vector3Field("Vector3Field", vector3Field);
GUIContent guiContent1 = new GUIContent();
guiContent1.text = "ColorField";
guiContent1.tooltip = "这是一个ColorField";
colorField = EditorGUILayout.ColorField(guiContent1, colorField);
animationField =
(Animation) EditorGUILayout.ObjectField("ObjectField", animationField, typeof(Animation), true);
}
//1秒100次
private void Update()
{
}
private void OnDestroy()
{
Debug.Log("OnDestroy");
}
}
如图该自定义EditorWindow效果:
MyEditorWindow
文件资源路径如下:
文件资源路径
快捷键表示法如图:
EditorWindow快捷键
这篇文章对各种GUI控件有更详细的介绍:
https://blog.csdn.net/qq_38275140/article/details/84778344