Editor-顯示隱藏管理工具

weixin_34292287發表於2017-08-21

該工具主要用來快捷的控制場景中多個物體的顯示與隱藏。效果如下:
首先在Hierarchy檢視框中右鍵建立“顯示隱藏管理”

2978185-d00eac415927d510.png
2978185-d24d96b2ec9408b3.png

2978185-c8328a018b77cbd3.png
22.png

接著新增管理物件組,將場景中物體拖入指定位置,勾選或者取消它是否顯示
效果如下:


2978185-4a22db9c5469142e.png
YJ9`NQ_DQ)5Y4VLC4QTU0I8.png

點選執行按鈕,就可以快速的切換物體到相應的狀態(顯示還是隱藏)

具體實現程式碼如下:

using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class ShowHideControl:MonoBehaviour
{
    private static ShowHideControl _instance = null;
    public static ShowHideControl Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = (new GameObject("顯示隱藏管理")).AddComponent<ShowHideControl>();
            }
            return _instance;
        }
    }
    private void Awake()
    {
        _instance = this;
    }
    public List<GameObjectGroup> _list = new List<GameObjectGroup>();
    [System.Serializable]
    public class GameObjectGroup
    {
        public string _desc = "描述";
        public List<GameObjectInformation> _list = new List<GameObjectInformation>();
        [System.Serializable]
        public class GameObjectInformation
        {
            public GameObject _obj;
            public bool _show = true;
        }
    }
    /// <summary>
    /// 執行
    /// </summary>
    /// <param name="desc">描述</param>
    /// <param name="b">正執行還是反執行</param>
    public void Do(string desc, bool b = true)
    {
        for (int i = 0; i < _list.Count; i++)
        {
            if (_list[i]._desc == desc)
            {
                for (int j = 0; j < _list[i]._list.Count; j++)
                {
                    if (b)
                    {
                        _list[i]._list[j]._obj.SetActive(_list[i]._list[j]._show);
                    }
                    else
                    {
                        _list[i]._list[j]._obj.SetActive(!_list[i]._list[j]._show);
                    }
                }
                break;
            }
        }
    }
}

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[CustomEditor(typeof(ShowHideControl))]
public class ShowHideControlEditor : Editor
{
    private ShowHideControl _showHide;
    private ShowHideControl.GameObjectGroup _gameObjectGroup;
    private int _deleteGameObjectGroupIndex = -1;//刪除組索引
    private int _choseAreaIndex = -1;
    private int _deleteGameObjectIndex = -1;//物件索引
    private bool _isArea = false;//是否在指定的拖拽區域內
    private ShowHideControl.GameObjectGroup.GameObjectInformation _gameObjectInformation;
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        _showHide = target as ShowHideControl;
        if (GUILayout.Button("新增管理"))
        {
            _showHide._list.Add(new ShowHideControl.GameObjectGroup());
        }
        GUILayout.Space(30);
        _deleteGameObjectGroupIndex = -1;
        _isArea = false;
        for (int i = 0; i < _showHide._list.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            _gameObjectGroup = _showHide._list[i];
            _gameObjectGroup._desc = EditorGUILayout.TextField(_gameObjectGroup._desc);
            if (GUILayout.Button("正執行"))
            {
                for (int j = 0; j < _gameObjectGroup._list.Count; j++)
                {
                    _gameObjectGroup._list[j]._obj.SetActive(_gameObjectGroup._list[j]._show);
                }
            }
            if (GUILayout.Button("反執行"))
            {
                for (int j = 0; j < _gameObjectGroup._list.Count; j++)
                {
                    _gameObjectGroup._list[j]._obj.SetActive(!_gameObjectGroup._list[j]._show);
                }
            }
            if (GUILayout.Button("刪除"))
            {
                if (EditorUtility.DisplayDialog("警告", "你確定要刪除該組物件嗎?", "確定", "取消"))
                {
                    _deleteGameObjectGroupIndex = i;
                }
            }
            EditorGUILayout.EndHorizontal();
            var dragArea = GUILayoutUtility.GetRect(0f, 50f, GUILayout.ExpandWidth(true));
            GUI.Box(dragArea, new GUIContent("拖動物件到此區域"));
            if (dragArea.Contains(Event.current.mousePosition))
            {
                _isArea = true;
                _choseAreaIndex = i;
            }
            else if(Event.current.type == EventType.Repaint&&_isArea==false)
            {
                _choseAreaIndex = -1;
            }
            _deleteGameObjectIndex = -1;
            for (int k = 0; k < _gameObjectGroup._list.Count; k++)
            {
                EditorGUILayout.BeginHorizontal();
                _gameObjectGroup._list[k]._obj = (GameObject)EditorGUILayout.ObjectField(_gameObjectGroup._list[k]._obj, typeof(GameObject), true);
                _gameObjectGroup._list[k]._show = EditorGUILayout.Toggle(_gameObjectGroup._list[k]._show);
                if (GUILayout.Button("刪除"))
                {
                    _deleteGameObjectIndex = k;
                }
                EditorGUILayout.EndHorizontal();
            }
            if (_deleteGameObjectIndex != -1) _gameObjectGroup._list.RemoveAt(_deleteGameObjectIndex);
            GUILayout.Space(50);
        }
        if (Event.current.type == EventType.DragExited && _choseAreaIndex != -1)
        {
            _gameObjectGroup = _showHide._list[_choseAreaIndex];
            DragAndDrop.AcceptDrag();
            if (DragAndDrop.objectReferences.Length != 0)
            {
                for (int j = 0; j < DragAndDrop.objectReferences.Length; j++)
                {
                    if (DragAndDrop.objectReferences[j].GetType() == typeof(GameObject))
                    {
                        if (IsExistGameObject((GameObject)DragAndDrop.objectReferences[j]) == false)
                        {
                            _gameObjectInformation = new ShowHideControl.GameObjectGroup.GameObjectInformation();
                            _gameObjectInformation._obj = (GameObject)DragAndDrop.objectReferences[j];
                            _gameObjectGroup._list.Add(_gameObjectInformation);
                        }
                        else
                        {
                            Debug.LogError("列表已存在該物件");
                        }
                    }
                }
            }
        }
        if (_deleteGameObjectGroupIndex != -1) _showHide._list.RemoveAt(_deleteGameObjectGroupIndex);
        GUILayout.Space(1000);
        serializedObject.ApplyModifiedProperties();
        EditorUtility.SetDirty(_showHide);
        if (GUI.changed) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    }
    /// <summary>
    /// 是否已經存在該物件
    /// </summary>
    /// <param name="obj"></param>
    public bool IsExistGameObject(GameObject obj)
    {
        for (int i = 0; i < _gameObjectGroup._list.Count; i++)
        {
            if (obj == _gameObjectGroup._list[i]._obj) return true;
        }
        return false;
    }
    [MenuItem("GameObject/自定義/建立顯示隱藏管理", false, MenuItemConfig.顯示隱藏管理)]
    private static void CreateSoundControlObject()
    {
        GameObject gameObject = new GameObject("顯示隱藏管理");
        gameObject.AddComponent<ShowHideControl>();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = gameObject;
        EditorGUIUtility.PingObject(Selection.activeObject);
        Undo.RegisterCreatedObjectUndo(gameObject, "Create GameObject");
        EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    }
}

使用程式碼控制

ShowHideControl.Instance.Do("描述");

相關文章