当前位置:网站首页>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);
}
}
}
边栏推荐
- 796 · 开锁
- P1596 [USACO10OCT]Lake Counting S
- Haproxy+kept cluster setup 02
- the installer has encountered an unexpected error installing this package
- Osgearth target selection
- Installation of PHP FPM software +openresty cache construction
- [linear table] basic operation of bidirectional linked list specify node exchange
- Clion toolchains are not configured configure disable profile problem solving
- UE4 source code reading_ Mobile synchronization
- Haproxy+kept build 01
猜你喜欢

Base64 and base64url

C course design employee information management system

VIM learning notes from introduction to silk skating

Easy touch plug-in

Kwai 20200412 recruitment

Transfinite hacker cognition

Constraintlayout's constraintset dynamically modifies constraints
![[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)](/img/de/1c75b5e7ed79aca47462de365428a7.jpg)
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)

Dotween plug-in

MAE
随机推荐
L'installateur a été installé avec une erreur inattendue
数据的存储
P1596 [USACO10OCT]Lake Counting S
Three characteristics
Data analysis exercises
【音视频】ijkplayer错误码
Introduction to hexadecimal coding
Use of ue5 QRcode plug-in
Osgearth topographic shading map drawing
Haproxy+kept cluster setup 02
swagger文档配置
Redis的数据结构
Redis data structure
Some understandings of 3dfiles
Golang中删除字符串的最后一个字符
C course design employee information management system
Golang 字符串分割,替换和截取
796 · unlock
Base64编码简介
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)