Unity 3D 拓展编辑器 拓展project视图

 拓展右键菜单 

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元素。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值