当前位置:网站首页>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);
}
}
}
边栏推荐
- swagger文档配置
- Xlua task list youyou
- Kwai 20200412 recruitment
- Clion toolchains are not configured configure disable profile problem solving
- Base64编码简介
- Classes and objects
- Advanced OSG collision detection
- [set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
- Encoding and decoding of golang URL
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
猜你喜欢
Clion toolchains are not configured configure disable profile problem solving
P1596 [USACO10OCT]Lake Counting S
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Student educational administration management system of C # curriculum design
C#课程设计之员工信息管理系统
数据分析练习题
数据的存储
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
Editor Extensions
Redis data structure
随机推荐
Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
Cesium service deployment, and import and display local 3dfiles data
Data analysis exercises
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
C course design employee information management system
Redis的数据结构
Transmit pictures with Base64 encoding
Student educational administration management system of C # curriculum design
Simple demo of solving BP neural network by gradient descent method
How to establish rectangular coordinate system in space
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
Wpf: solve the problem that materialdesign:dialoghost cannot be closed
One dimensional array two dimensional array (sort Max insert sort)
十六进制编码简介
【K&R】中文第二版 个人题解 Chapter1
What does (+) in Oracle mean
VIM learning notes from introduction to silk skating
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
梯度下降法求解BP神经网络的简单Demo
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)