Editor-顯示隱藏管理工具
該工具主要用來快捷的控制場景中多個物體的顯示與隱藏。效果如下:
首先在Hierarchy檢視框中右鍵建立“顯示隱藏管理”
接著新增管理物件組,將場景中物體拖入指定位置,勾選或者取消它是否顯示
效果如下:
點選執行按鈕,就可以快速的切換物體到相應的狀態(顯示還是隱藏)
具體實現程式碼如下:
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("描述");
相關文章
- mac顯示隱藏檔案,取消顯示隱藏檔案Mac
- iOS 隱藏&顯示tabBariOStabBar
- MacOS X隱藏和顯示隱藏檔案Mac
- Mac顯示和隱藏“隱藏檔案”命令Mac
- jQuery 效果 – 隱藏和顯示jQuery
- mac隱藏檔案顯示Mac
- view的隱藏和顯示View
- Mac 顯示隱藏檔案Mac
- Mac顯示/不顯示隱藏檔案教程!Mac
- MAC如何顯示隱藏檔案和隱藏隱藏檔案的命令Mac
- Mac下顯示和隱藏隱藏檔案的命令Mac
- steam隱藏的遊戲怎麼顯示出來 steam隱藏顯示遊戲方法介紹遊戲
- excel隱藏的部分如何顯示出來 excel裡面怎麼顯示隱藏部分Excel
- 【macOS】顯示/隱藏 指定檔案Mac
- MACOS 如何顯示隱藏檔案Mac
- 隱藏密碼?顯示密碼?密碼
- 區塊的顯示和隱藏
- OSX中隱藏和顯示[隱藏檔案]的命令列命令列
- JavaScript 設定div顯示與隱藏JavaScript
- CSS——文字超出隱藏顯示省略號CSS
- javascript隱藏和顯示div的方法JavaScript
- jQuery動畫的顯示與隱藏效果jQuery動畫
- jQuery動畫的顯示與隱藏效果!jQuery動畫
- jQuery 實現顯示與隱藏效果jQuery
- jQuery table表格tr行顯示隱藏jQuery
- JavaScript側邊欄顯示和隱藏JavaScript
- NavigationBar的隱藏與顯示Navigation
- javaScript顯示和隱藏(display屬性)JavaScript
- 不能正常顯示WINDOWS隱藏檔案Windows
- mac 隱藏檔案顯示 快捷鍵Mac
- win10 如何顯示隱藏資料夾_win10 如何顯示隱藏的資料夾Win10
- vue 滑鼠移入顯示圖示 ,滑鼠移出隱藏圖示Vue
- win10怎麼顯示隱藏檔案_Win10系統顯示隱藏檔案步驟Win10
- win10怎麼顯示隱藏資料夾 win10顯示隱藏資料夾的方法Win10
- win10桌面檔案被隱藏如何顯示_win10怎麼顯示桌面隱藏檔案Win10
- mac隱藏資料夾怎麼顯示?Mac
- CSS點選隱藏和顯示div效果CSS
- Android 眼睛 顯示隱藏密碼(ImageView)Android密碼View