拓展右键菜单
using UnityEditor;
using UnityEngine;
public class AssetsMyTools
{
//--第三个参数输入表示排序的整数,数值越小,它的排序就越靠前
//--(相邻数值之间差超过11会出现隔断线)
[MenuItem("Assets/拓展右键菜单/打印选中物体的名字1", false, 1)]
static void MyAssetsTools1()
{
if (Selection.activeObject != null)
{
Debug.Log("当前选中:" + Selection.activeObject.name);
}
}
[MenuItem("Assets/拓展右键菜单/打印选中物体的名字2", false, 12)]
static void MyAssetsTools2()
{
if (Selection.activeObject != null)
{
Debug.Log("当前选中:" + Selection.activeObject.name);
}
}
}
Project面板中选中某个资源,点击右键出现系统菜单。
//---------------------------------------------------------------------------
拓展创建菜单
using UnityEditor;
using UnityEngine;
public class AssetsCreateMyTools
{
[MenuItem("Assets/Create/拓展创建菜单/创建立方体", false, 1)]
static void CreateCube()
{
GameObject.CreatePrimitive(PrimitiveType.Cube);
}
}
点击Project面板中的Create或者右键然后Create,出现系统菜单。
//-----------------------------------------------------------------------
拓展布局
using UnityEditor;
using UnityEngine;
public class LayoutClickBtnMyTools
{
[InitializeOnLoadMethod]
static void ClickBtnMethod()
{
EditorApplication.projectWindowItemOnGUI = delegate (string guid, Rect selectionRect)
{
//--在Project视图中选择一个资源
if (Selection.activeObject && guid == AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(Selection.activeObject)))
{
//--设置拓展按钮区域
float width = 50.0f;
selectionRect.x += (selectionRect.width - width);
selectionRect.y += 2.0f;
selectionRect.width = width;
GUI.color = Color.red;
//--点击事件
if (GUI.Button(selectionRect, "Click"))
{
Debug.Log("Click:" + Selection.activeObject.name);
}
GUI.color = Color.white;
}
};
}
}
选中某个资源后出现按钮。
在方法前面添加[InitializeOnLoadMethod]表示此方法在C#代码每次编译完成后首先调用。
监听EditorApplication.projectWindowItemOnGUI 委托,即可使用 GUI 方法来绘制自定义的UI元素。