Unity编辑器开发分享Unity教程合集

Editor-显示隐藏管理工具

2017-08-21  本文已影响8人  循环渐进123456

该工具主要用来快捷的控制场景中多个物体的显示与隐藏。效果如下:
首先在Hierarchy视图框中右键创建“显示隐藏管理”

2978185-d24d96b2ec9408b3.png
22.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("描述");
上一篇下一篇

猜你喜欢

热点阅读