当前位置:网站首页>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);
}
}
}
边栏推荐
- 简易入手《SOM神经网络》的本质与原理
- Unity learning notes
- Golang's range
- Cesium service deployment, and import and display local 3dfiles data
- Why can void * be a general pointer
- Osgearth target selection
- Gradle's method of dynamically modifying APK package name
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- [public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
- P1596 [USACO10OCT]Lake Counting S
猜你喜欢

Easy touch plug-in

Three characteristics

Dotween plug-in

About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed

简易入手《SOM神经网络》的本质与原理

Student educational administration management system of C # curriculum design

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

A tunnel to all ports of the server

the installer has encountered an unexpected error installing this package
![[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)
随机推荐
L'installateur a été installé avec une erreur inattendue
C语言-入门-精华版-带你走进编程(一)
[public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
Encoding and decoding of golang URL
Use filechannel to copy files
Chocolate installation
Graphics_ Games101/202 learning notes
MAE
MySQL 8
Use of ue5 QRcode plug-in
Abstract classes and interfaces
Huawei interview summary during the epidemic
Why can void * be a general pointer
Installation of PHP FPM software +openresty cache construction
[K & R] Chinese Second Edition personal questions Chapter1
Transfinite hacker cognition
UE4 source code reading_ Mobile synchronization
Golang中删除字符串的最后一个字符
[linear table] basic operation of bidirectional linked list specify node exchange
Redis cluster series 4