Unity 界面编辑实现如图所示界面(类似Console界面)
using System;
using UnityEditor;
using UnityEngine;
public class ZZSTestWindow02 : EditorWindow
{
private ZZSScriptableObject _zzsScriptableObject;
private const string saveZzsObjPath = "Assets/Scripts/ScriptableObject/ZzsScriptableObject.asset";
private GUIContent firstGUIContent;
private GUIContent secondGUIContent;
private int firstSelectIndex = 0;
private int secondSelectIndex = 0;
private void OnInspectorUpdate()
{
Repaint();
}
private void OnGUI()
{
if (_zzsScriptableObject == null)
{
var guids = AssetDatabase.FindAssets("t:ZZSScriptableObject");
if (guids != null && guids.Length > 0)
{
_zzsScriptableObject = AssetDatabase.LoadAssetAtPath<ZZSScriptableObject>(AssetDatabase.GUIDToAssetPath(guids[0]));
}
GUILayout.Space(50);
if (GUILayout.Button("Create ZzsScriptableObject"))
{
var zzsObj = CreateInstance<ZZSScriptableObject>();
AssetDatabase.CreateAsset(zzsObj,saveZzsObjPath);
}
return;
}
GUILayout.BeginHorizontal(GUI.skin.box);
{
if (firstGUIContent == null)
firstGUIContent = new GUIContent("First Test");
var r1 = GUILayoutUtility.GetRect(firstGUIContent, EditorStyles.toolbarDropDown, GUILayout.Width(200));
if (EditorGUI.DropdownButton(r1, firstGUIContent, FocusType.Passive, EditorStyles.toolbarDropDown))
{
var menu = new GenericMenu();
for (int i = 0; i < 5; i++)
{
menu.AddItem(new GUIContent($"Test{i}"),i==firstSelectIndex, (index) =>
{
firstSelectIndex = (int) index;
},i);
}
menu.DropDown(r1);
}
if(secondGUIContent == null)
secondGUIContent = new GUIContent("Second Test");
var r2 = GUILayoutUtility.GetRect(secondGUIContent, EditorStyles.toolbarDropDown, GUILayout.Width(200));
if (EditorGUI.DropdownButton(r2, secondGUIContent, FocusType.Passive, EditorStyles.toolbarDropDown))
{
var menu = new GenericMenu();
for (int i = 0; i < 5; i++)
{
menu.AddItem(new GUIContent($"Test{i}"),i==secondSelectIndex, (index) =>
{
secondSelectIndex = (int) index;
},i);
}
menu.DropDown(r2);
}
GUILayout.FlexibleSpace();
for (int i = 0; i < 3; i++)
{
var buttonGuiContent = new GUIContent("Test Button"+i);
var r3 = GUILayoutUtility.GetRect(buttonGuiContent, EditorStyles.miniButton,GUILayout.Width(100));
if(GUI.Button(r3,buttonGuiContent))
Debug.Log($"按钮{i}点击");
}
}
GUILayout.EndHorizontal();
foreach (var str in _zzsScriptableObject.allStr)
{
GUILayout.Label(str);
}
}
}