当前位置:网站首页>Unity editor expansion - window, sub window, menu, right-click menu (context menu)
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
2022-07-03 08:26:00 【T.D.C】
summary
- This paper studies the knowledge necessary to create editor windows
Post code
using UnityEditor;
using UnityEngine;
namespace DC.DCIMGUIBox
{
public class ChildWindowWindow : EditorWindow
{
[MenuItem("DC/IMGUI/ChildWindowWindow")]
public static void Open()
{
var window = GetWindow<ChildWindowWindow>();
window.minSize = new Vector2(400, 400);
}
Rect windowRect = new Rect(20, 20, 400, 50);
Rect windowRect2 = new Rect(320, 120, 120, 50);
void OnGUI()
{
BeginWindows();
windowRect = GUILayout.Window(0, windowRect, ChildWindow, "CW1");
windowRect2 = GUILayout.Window(1, windowRect2, ChildWindow, "CW2");
EndWindows();
}
void ChildWindow(int windowID)
{
GUI.DragWindow(new Rect(0, 0, windowRect.width, 16));
if (GUILayout.Button("Btn " + windowID))
{
Debug.Log(windowID);
}
}
}
}
Set window properties
// After creation, you can set the supported properties
var window = GetWindow<ChildWindowWindow>();
window.minSize = new Vector2(400, 400);
child window
- Just adjust one api Pass it on like a similar OnGUI Function to draw sub windows
- Incoming window id And size , Draw in the callback function ui that will do , Get the controls you need
- You can encapsulate child windows as objects as needed , This size can also be adjusted , Otherwise, if the sub window is too small, it may not display all
void OnGUI()
{
BeginWindows();
windowRect = GUILayout.Window(0, windowRect, ChildWindow, "CW1");
windowRect2 = GUILayout.Window(1, windowRect2, ChildWindow, "CW2");
EndWindows();
}
void ChildWindow(int windowID)
{
GUI.DragWindow(new Rect(0, 0, windowRect.width, 16));
if (GUILayout.Button("Btn " + windowID))
{
Debug.Log(windowID);
}
}
menu
// So you can do it unity Create a path for the menu bar of DC/IMGUI/ChildWindowWindow Here's the menu
[MenuItem("DC/IMGUI/ChildWindowWindow")]
public static void Open()
{
var window = GetWindow<ChildWindowWindow>();
window.minSize = new Vector2(400, 400);
}
Right-click menu ( Context menu )
- The right-click context menu is unity Subset of the right-click menu , It is shown that unity Right click menu or a group of menus in the right-click menu
- How to add items to the right-click menu
// Similar to the menu , But with Assets start
[MenuItem("Assets/DC/xxx")]
- Listen for right click events
Event evt = Event.current;
if (evt.type == EventType.ContextClick){
...
}
- Show the right-click menu
// Key messages 2 individual , Location , Menu path
EditorUtility.DisplayPopupMenu(new Rect(mousePos.x, mousePos.y, 0, 0), "Assets/DC", null);
- Complete code
using UnityEditor;
using UnityEngine;
namespace DC.DCIMGUIBox
{
public class PopMenuWindow_01 : EditorWindow
{
[MenuItem("DC/IMGUI/PopMenuWindow_01")]
public static void Open()
{
var window = GetWindow<PopMenuWindow_01>();
window.minSize = new Vector2(800, 600);
}
int index = 0;
string[] options = { "Rigidbody", "Box Collider", "Sphere Collider" };
void OnGUI()
{
Event evt = Event.current;
Rect contextRect = new Rect(10, 10, 100, 100);
EditorGUI.DrawRect(contextRect, Color.blue);
if (evt.type == EventType.ContextClick)
{
Vector2 mousePos = evt.mousePosition;
if (contextRect.Contains(mousePos))
{
EditorUtility.DisplayPopupMenu(new Rect(mousePos.x, mousePos.y, 0, 0), "Assets/DC", null);
}
}
var areaRect = new Rect(0, 200, 200, 40);
EditorGUI.DrawRect(areaRect, Color.blue);
index = EditorGUI.Popup(areaRect, "Component:", index, options);
}
}
}
边栏推荐
猜你喜欢

Base64 and base64url

matlab神經網絡所有傳遞函數(激活函數)公式詳解

jupyter远程服务器配置以及服务器开机自启

Get to know unity2 for the first time

Oracle insert single quotation mark

C#课程设计之员工信息管理系统

Dealing with duplicate data in Excel with xlwings

Minimap plug-in

了解小程序的笔记 2022/7/3

Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
随机推荐
OpenGL learning notes
Basic operation and process control
Image processing 8-cnn image classification
About Wireshark's unsuccessful installation of npcap
Oracle insert single quotation mark
Osgearth north arrow display
Kunlunbase meetup is waiting for you!
LinkList
C#课程设计之学生教务管理系统
redis集群系列四
Kwai 20200412 recruitment
Shader foundation 01
Graphics_ Games101/202 learning notes
What is BFC?
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
Redis data structure
VIM learning notes from introduction to silk skating
String class
the installer has encountered an unexpected error installing this package
Student educational administration management system of C # curriculum design