天天看点

Unity Editor编辑器实用扩展1.MenuItem使用方法2.ContextMenu和ConTextMenuItem:3.Selection类4.编辑器实用方法汇总5.inspector 扩展GM工具6.Inspector_PopList 

目录

1.MenuItem使用方法

    .Unity编辑器工具栏扩展;

   .Inspector工具栏扩展;

   .Hierarchy工具栏扩展;

   .Project工具栏扩展。

2.ContextMenu和ConTextMenuItem:

3.Selection类

4.编辑器实用方法汇总

5.inspector 扩展GM工具

6.Inspector_PopList

1.MenuItem使用方法

♦ 参数

MenuItem("Tools/Test", true, 23):

1.菜单路径,以/分割;

2.是否为验证函数,默认为false;

3.菜单的优先级,数字越小显示在越上方。并且俩相邻菜单间差距为11以上的优先级会被分为两组,即菜单间会多个分隔线。默认为1000。

♦ 菜单的快捷键

单独的一个按键以“空格 + 下划线 + 想要的按键”增加在路径后缀,空格千万不要忘记。

“Tools/Test _g”,即在Unity中按下g就可以直接执行该菜单。

以“Tools/Test %&g”表示按住ctrl和alt,再按下g就能触发。特殊符号:%(ctrl/cmd)、#(shift)、&(alt)。

其他支持的按键:LEFT、RIGHT、UP、DOWN、F1..F12、HOME、END、PGUP、PGDN。

当然也支持类似#LEFT是左shift之类的按键。

♦ 菜单使用注意

1.创建GameObject时,使用GameObjectUtility.SetParentAndAlign方法设置父节点,可以重置坐标;

2.使用类似于Undo.RegisterCreatedObjectUndo方法注册撤销操作(这个我们之后在讨论)。

 .Unity编辑器工具栏扩展;

[MenuItem("Tools/Test", false, 23)]
public static void Test()
{
    Debug.Log("Test");
}
 
[MenuItem("Tools/Test", true, 23)]
public static bool ValidateTest()
{
    return true;
}
           

   .Inspector工具栏扩展;

为脚本右键添加菜单

[MenuItem("CONTEXT/Transform/Test4")]
public static void Test4()
{
    Debug.Log("Test4");
}
           

   .Hierarchy工具栏扩展;

Hierarchy窗口下的右键菜单就是GameObject菜单栏中的部分菜单

[MenuItem("GameObject/Test2", false, 11)]
public static void PasteTRValue()
{
    Debug.Log("Test2");
}
 
           

   .Project工具栏扩展。

Project中的是Assets所有的。

[MenuItem("Assets/Test3", false)]
public static void Test3()
{
    Debug.Log("Test3");
}
           

2.ContextMenu和ConTextMenuItem:

Inspector窗口中添加菜单,但与MenuItem不同的是写在本身Component脚本中,也就是隶属于UnityEngine

♦ ContextMenuItem参数

ContextMenuItem("Add Hour", "AddHour", order = 1):

1.菜单的名称;

2.菜单执行的方法,要求脚本中必须有;

3.按钮的先后顺序。

using UnityEngine;
 
public class Clock : MonoBehaviour
{
    //在右键变量名称时添加菜单
    [ContextMenuItem("Add Hour", "AddHour", order = 1)]
    public int Hour = 10;
 
    public void AddHour()
    {
        Hour += 1;
    }
 
    [ContextMenu("Sub Hour", false, 10)]
    public void SubHour()
    {
        Hour -= 1;
    }
}
           

3.Selection类

通过Selection类可以在编辑器下对选择的物体进行操作

using UnityEngine;
using UnityEditor;
 
public class SelectionTest : EditorWindow
{
    [MenuItem("Tool/DebugSelected")]
    private static void Test()
    {
        Debug.Log(Selection.activeObject);//返回当前在面板上选择的游戏物体Object,未选择则返回Null。可以选择Project文件夹下的任意资源(选择多个则返回第一个选择的游戏物体)
        Debug.Log(Selection.activeGameObject);//返回当前在面板上选择的游戏物体GameObject,未选择则返回Null。可以选择Project文件夹下的游戏物体(选择多个则返回第一个选择的游戏物体)
        Debug.Log(Selection.activeTransform);//返回当前在面板上选择的游戏物体的Transform,未选择则返回Null(选择多个则返回第一个选择的游戏物体)
        Debug.Log(Selection.gameObjects.Length); //返回当前在面板上选择的游戏物体GameObject数组,未选择则返回Null。可以选择Project文件夹下的游戏物体
        Debug.Log(Selection.transforms); //返回当前在面板上选择的游戏物体Transform数组,未选择则返回Null
 
        Selection.Contains(Selection.instanceIDs[0]);
        Selection.Contains(Selection.gameObjects[0]);
 
        Selection.selectionChanged += OnSelectionChange;//委托,选择的物体变化时调用
    }
 
    /// <summary>
    /// 选择的物体变化时调用的委托
    /// </summary>
    private static void OnSelectionChange()
    {
        Debug.Log("OnSelectionChange");
    }
}
           

4.编辑器实用方法汇总

//1.编辑器内物体查找(指定路径下搜索类型是scene/object.. ,并且名字中包含unity的文件)
var mlist = new List<string>(AssetDatabase.FindAssets("unity t:scene", new string[] { "Assets/path" }));
//2. 定位
EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath("filePath",typeof(UnityEngine.Object)));
//3.搜集编辑器引用
AssetDatabase.GetDependencies("pathName");
EditorUtility.CollectDependencies("Objects[]");
//4.选中查找 只有Object类型好使
UnityEditor.Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets)
//持续更新中... ...
           

5.inspector 扩展GM工具

使用方法:

将Inspector_GMOrder 放到编辑器目录下。

将GM_Order 挂载到游戏对象上

public class GM_Order : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

[CustomEditor(typeof(GM_Order))]
public class Inspector_GMOrder : Editor
{
    string gm_value = "";
    public override void OnInspectorGUI()
    {
        gm_value = GUILayout.TextField(gm_value);
        if (GUILayout.Button("获得指定车辆"))
        {
            for (int i = 1; i < 15; i++)
            {
                PassionSpeedMgrData.inst.SavePrefs(i);
            }
            //Hero.Inst.HeroReLive();
            

        }
        if (GUILayout.Button("暂定"))
        {
            //UserInfo.GetInst().myDiamond = 99;
        }
    }
}
           

6.Inspector_PopList

将指定文件目录内资源条目已pop类型展示在inspector面板中

Unity Editor编辑器实用扩展1.MenuItem使用方法2.ContextMenu和ConTextMenuItem:3.Selection类4.编辑器实用方法汇总5.inspector 扩展GM工具6.Inspector_PopList 
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
//自定义Tset脚本
[CustomEditor(typeof(BossBulletManager))]
public class Inspector_PopList : Editor
{
    static public string DrawList(string field, string[] list, string selection, params GUILayoutOption[] options)
    {
        if (list != null && list.Length > 0)
        {
            int index = 0;
            if (string.IsNullOrEmpty(selection)) selection = list[0];

            // We need to find the sprite in order to have it selected
            if (!string.IsNullOrEmpty(selection))
            {
                for (int i = 0; i < list.Length; ++i)
                {
                    if (selection.Equals(list[i], StringComparison.OrdinalIgnoreCase))
                    {
                        index = i;
                        break;
                    }
                }
            }
            // Draw the sprite selection popup
            //EditorGUILayout.BeginHorizontal(EditorStyles.toolbarPopup);
            index = string.IsNullOrEmpty(field) ?
                EditorGUILayout.Popup(index, list) :
                EditorGUILayout.Popup(field, index, list);

            //EditorGUILayout.EndHorizontal();
            return list[index];
        }
        return null;
    }
    //在这里方法中就可以绘制面板。
    public override void OnInspectorGUI()
    {
        //得到Test对象
        var test = (BossBulletManager)target;
        test.bossBulletType = DrawList("", ThreeToOneManager.Inst.ListToEnum("/Resources/BossBullet/ShotPattern/"), test.bossBulletType);
        if (GUILayout.Button("播放当前子弹类型"))
        {
            BossBulletManager.Inst.AddBossBullet(test.bossBulletType);
        }
        if (GUILayout.Button("增加当前子弹类型"))
        {
            BossBulletManager.Inst.AddBulletType(test.bossBulletType);
        }
        if (GUILayout.Button("演示队列子弹类型"))
        {
            BossBulletManager.Inst.PalyBulletTypeList();
        }
        base.OnInspectorGUI();
        for (int i = 0; i < test.bulletTypeList.Count; i++)
        {
            test.bulletTypeList[i] = DrawList("", ThreeToOneManager.Inst.ListToEnum("/Resources/BossBullet/ShotPattern/"), test.bulletTypeList[i]);
        }
    } 
}
           

获取文件夹条目方法:

public string[] ListToEnum(string _path)
    {
    var path = UnityEngine.Application.dataPath + _path;//"/Resources/BossBullet/ShotPattern/";
        var paras = Directory.GetFiles(path, "*.prefab")
        .Select(s => s.Substring(s.LastIndexOf('/') + 1, s.Length - s.LastIndexOf('/') - 1))
        .Select(b=> b.Substring(0,b.LastIndexOf('.')) ).ToArray();

        return paras;
    }