文章目錄
一. 拓展Project視圖
1. 右鍵菜單
[MenuItem("Assets/My Tools/Tools 1", false, 2)]
static void MyTools1()
{
Debug.Log(Selection.activeObject.name);
}
2. 拓展布局
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
EditorApplication.projectWindowItemOnGUI = delegate (string guid, Rect selectionRect) {
//在Project視圖中選擇一個資源
if (Selection.activeObject &&
guid == AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject)))
{
//設置拓展按鈕區域
float width = 50f;
selectionRect.x += (selectionRect.width - width);
selectionRect.width = width;
GUI.color = Color.yellow;
//點擊事件
if (GUI.Button(selectionRect, "click"))
{
Debug.LogFormat("click : {0}", Selection.activeObject.name);
}
GUI.color = Color.white;
}
};
}
二. 拓展Hierarchy視圖
1. 右鍵菜單
[MenuItem("GameObject/My Create/Cube",false,0)]
static void CreateCube()
{
GameObject.CreatePrimitive (PrimitiveType.Cube); //創建立方體
}
2. 拓展布局
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
EditorApplication.hierarchyWindowItemOnGUI = delegate (int instanceID, Rect selectionRect) {
//在Hierarchy視圖中選擇一個資源
if (Selection.activeObject &&
instanceID == Selection.activeObject.GetInstanceID())
{
//設置拓展按鈕區域
float width = 50f;
float height = 18f;
selectionRect.x += (selectionRect.width - width);
selectionRect.width = width;
selectionRect.height = height;
GUI.color = Color.yellow;
//點擊事件
if (GUI.Button(selectionRect, "click"))
{
Debug.LogFormat("click : {0}", Selection.activeObject.name);
}
GUI.color = Color.white;
}
};
}
三. 拓展Inspector視圖
1. 拓展組件
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Camera))]
public class EditorTest : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("拓展按鈕"))
{
}
base.OnInspectorGUI();
}
}
2. 拓展Context菜單
[MenuItem("CONTEXT/Transform/New Context 1")]
public static void NewContext1(MenuCommand command)
{
//獲取對象名
Debug.Log(command.context.name);
}
四. 拓展Scene視圖
1. 畫線或3D物體
using UnityEngine;
public class Test : MonoBehaviour
{
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
//畫線
Gizmos.DrawLine(transform.position, Vector3.one);
//立方體
Gizmos.DrawCube(Vector3.one, Vector3.one);
}
void OnDrawGizmos()
{
Gizmos.DrawSphere(transform.position, 1);
}
}
2. 畫GUI
using System.Collections;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Camera))]
public class EditorTest : Editor
{
void OnSceneGUI()
{
Camera camera = target as Camera;
if (camera != null)
{
Handles.color = Color.red;
Handles.Label(camera.transform.position, camera.transform.position.ToString());
Handles.BeginGUI();
GUI.backgroundColor = Color.red;
if (GUILayout.Button("click", GUILayout.Width(200f)))
{
Debug.LogFormat("click = {0}", camera.name);
}
GUILayout.Label("Label");
Handles.EndGUI();
}
}
}
using UnityEngine;
using UnityEditor;
public class EditorTest
{
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
SceneView.duringSceneGui += delegate (SceneView sceneView) {
Handles.BeginGUI();
GUI.Label(new Rect(0f, 0f, 50f, 15f), "標題");
GUI.Button(new Rect(0f, 20f, 50f, 20f), "click");
Handles.EndGUI();
};
}
}
五. 拓展頂部菜單
1. 帶勾選的菜單
[MenuItem("MenuTest/Test", false, 3)]
static void Test3()
{
//勾選中框的菜單
var menuPath = "MenuTest/Test";
bool mchecked = Menu.GetChecked(menuPath);
Menu.SetChecked(menuPath, !mchecked);
}
2. 帶快捷鍵的菜單
[MenuItem("Assets/HotKey %#d",false,-1)]
private static void HotKey()
{
Debug.Log ("Command Shift + D");
}
其他快捷鍵:
%:表示Windows的Ctrl鍵和macOS下的Command鍵
#:表示Shift鍵
&:表示Alt鍵
LEFT / RIGHT / UP / DOWN:表示四個方向鍵
HOME / END / PGUP / PGDN:表示對應的鍵