Unity Editor编辑器实用扩展

目录

备忘:Unity编辑器常用路径

1.MenuItem使用方法

 .Unity编辑器工具栏扩展;

   .Inspector工具栏扩展;

   .Hierarchy工具栏扩展;

   .Project工具栏扩展。

2.ContextMenu和ConTextMenuItem:

3.Selection类

4.编辑器实用方法汇总

5.inspector 扩展GM工具

6.Inspector_PopList

7.MenuTool查找预制中的中文

8.unity 检测 资源导入


备忘:Unity编辑器常用路径

   // System.Environment.CurrentDirectory : 在Unity中指工程的跟目录,即与Assets目录是平级的。

ex:
BuildPipeline.BuildPlayer(scenes,  System.Environment.CurrentDirectory+"xcode", BuildTarget.iOS, BuildOptions.None);

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面板中

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;
    }

7.MenuTool查找预制中的中文

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;
using TMPro;
using System.IO;
using System;
 
public class UiPrefabs 
{
 
    [MenuItem("Tools/检查预设中文并且生成路径")]
    static void CheckChinesePrefabsAndSerialization()
    {
        string[] prefabPaths = GetAllPrefabs("Assets/Prefab");
 
        if (prefabPaths == null)
        {
            return;
        }
 
        List<string> text = new List<string>();
 
        for (int i = 0; i < prefabPaths.Length; i++)
        {
            string prefabPath = prefabPaths[i];
            GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
 
            if (prefab == null)
            {
                continue;
            }
            
             //UIText或者是其它文本组件
            TextMeshProUGUI[] uiLabels = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);
 
            for (int j = 0; j < uiLabels.Length; j++)
            {
                TextMeshProUGUI uiLabel = uiLabels[j];
 
                if (IsIncludeChinese(uiLabel.text))
                {
                    Debug.LogError(string.Format("路径:{0} 预设名:{1} 对象名:{2} 中文:{3}", prefabPath, prefab.name, uiLabel.name, uiLabel.text));
                    text.Add(uiLabel.text);
                }
            }
 
            //进度条
            float progressBar = (float)i / prefabPaths.Length;
            EditorUtility.DisplayProgressBar("检查预设中文", "进度 :" + ((int)(progressBar * 100)).ToString() + "%", progressBar);
        }
 
 
        EditorUtility.ClearProgressBar();
 
        AssetDatabase.Refresh();
 
        Debug.Log("完成检查预设中文并且生成路径");
    }
 
 
    //string path = "Assets/UI/Prefab";
    static string[] GetAllPrefabs(string directory)
    {
        if (string.IsNullOrEmpty(directory) || !directory.StartsWith("Assets"))
            throw new ArgumentException("folderPath");
 
        string[] subFolders = Directory.GetDirectories(directory);
        string[] guids = null;
        string[] assetPaths = null;
        int i = 0, iMax = 0;
        foreach (var folder in subFolders)
        {
            guids = AssetDatabase.FindAssets("t:Prefab", new string[] { folder });
            assetPaths = new string[guids.Length];
            for (i = 0, iMax = assetPaths.Length; i < iMax; ++i)
            {
                assetPaths[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
            }
        }
        return assetPaths;
    }
 
 
    public static bool IsIncludeChinese(string content)
    {
        string regexstr = @"[\u4e00-\u9fa5]";
 
        if (Regex.IsMatch(content, regexstr))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
 
 
}

8.unity 检测 资源导入

用途:当向Unity导入 纹理资源、音效、fbx 等所有Unity支持格式的资源,想通过代码来修改或者验证资源命名是否合乎规范,如果已经在Unity中了则可以在 Assets 文件夹内鼠标右键 然后点击 Reimport,也会执行下面的逻辑

创建一个 Editor 文件夹,然后在Editor 文件夹中随便创建一个脚本,脚本命名随意
添加 using UnityEditor;
继承 AssetPostprocessor

using UnityEngine;
using UnityEditor;

public class TextureAssetCheck : AssetPostprocessor
{

    // 纹理导入之前调用
    public void OnPreprocessTexture()
    {
        // 可以修改一些属性或者使用代码验证纹理的命名是否合乎规范
        TextureImporter textureImporter = assetImporter as TextureImporter;
        Debug.LogError("Pre Texture:" + textureImporter.assetPath);
    }

    // 纹理导入之后调用
    public void OnPostprocessTexture(Texture2D texture)
    {
        TextureImporter textureImporter = assetImporter as TextureImporter;
        Debug.LogError("Post Texutre:" + assetPath.ToLower());
        Debug.LogError("Post Texture:" + texture.width + "    " + texture.height);
    }

    // 所有资源导入都会调用这里
    public void OnPreprocessAsset()
    {
        //目录信息也会被获取到
        //Debug.LogError("Asset:" + assetImporter.assetPath);
    }

    /// <summary>
    /// 在任意数量的资源导入完成后调用
    /// 此函数必须声明为 static
    /// </summary>
    /// <param name="importedAssets"></param>
    /// <param name="deletedAssets"></param>
    /// <param name="movedAssets"></param>
    /// <param name="movedFromAssetPaths"></param>
    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        foreach(var asset in importedAssets)
        {
            Debug.LogError("import:" + asset);
        }

        foreach (var asset in deletedAssets)
        {
            Debug.LogError("delete:" + asset);
        }

        foreach (var asset in movedAssets)
        {
            Debug.LogError("move:" + asset);
        }

        foreach (var asset in movedFromAssetPaths)
        {
            Debug.LogError("moveFromAsset:" + asset);
        }
    }
}

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值