自定义编辑器功能
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// 自定义unity编辑器功能
/// </summary>
public class CutomEditorFunc : MonoBehaviour
{
#if UNITY_EDITOR
/// <summary>
/// 移除当前场景中所有的MeshCollider组件
/// </summary>
[ContextMenu("Func_RemoveAllMeshColliderOfCurrScene")]
public void ReactiveObj()
{
List<MeshCollider> interactObjs = GetCurrentActiveSceneTypeObjs<MeshCollider>();
foreach (var mc in interactObjs)
{
GameObject.DestroyImmediate(mc);
}
interactObjs.Clear();
interactObjs = null;
}
#endif
/// <summary>
/// 此方法用来得到某场景下所有物体,查出所有挂载T类型的脚本
/// </summary>
private static List<T> GetCurrentActiveSceneTypeObjs<T>(bool includeInactive = true)
{
List<T> objs = new List<T>();
objs.Clear();
GameObject[] rootGameObjects = getSceneRootGameObjects(GetCurrentActiveScene());
for (int i = 0; i < rootGameObjects.Length; i++)
{
T[] objsTyps = rootGameObjects[i].transform.GetComponentsInChildren<T>(includeInactive);
objs.AddRange(objsTyps);
}
objs.RemoveAll(x => x == null);
return objs;
}
private static GameObject[] getSceneRootGameObjects(Scene scene)
{
return scene.GetRootGameObjects();
}
private static Scene GetCurrentActiveScene()
{
return SceneManager.GetActiveScene();
}
}
public class Patient : MonoBehaviour
{
public void Apply()
{
}
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(Patient))]
public class PatientEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
Patient patient = (Patient)target;
if (GUILayout.Button("Apply"))
{
patient.Apply();
}
}
}
#endif
public class PatientView : MonoBehaviour
{
public bool HairActive
{
get => hairActive;
set
{
hairActive = value;
}
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(PatientView))]
public class PatientViewEditor : UnityEditor.Editor
{
private PatientView _patientView;
public override void OnInspectorGUI()
{
_patientView = target as PatientView;
if (_patientView == null) return;
// 头发显示
_patientView.HairActive = GUILayout.Toggle(_patientView.HairActive, "头发显示");
base.OnInspectorGUI();
}
}
#endif