1. 程式人生 > >Unity Editor編輯器實用擴充套件

Unity Editor編輯器實用擴充套件

1.編輯器實用方法彙總

//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)
//持續更新中... ...

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