列出场景对象Lightmap属性
首先上效果图:
编辑器代码:
using UnityEngine; using UnityEditor; using System.Collections; public class LightmapAnalysisEditor : EditorWindow { private static EditorWindow window; [MenuItem("MyEditor/LightmapAnalysis &q")] private static void Execute() { if (window == null) window = (LightmapAnalysisEditor)GetWindow(typeof(LightmapAnalysisEditor)); window.minSize = new Vector2(500, 500); window.Show(); } private void OnGUI() { if (GUILayout.Button("光照贴图比例精度", GUILayout.Height(50f))) { GameObject go = GameObject.Find("LightmapScaleInfo"); if(go == null) { go = new GameObject("LightmapScaleInfo"); } var comp = go.GetComponent<LightmapScaleAnalysis>(); if(comp == null) { comp = go.AddComponent<LightmapScaleAnalysis>(); } Selection.activeObject = go; EditorGUIUtility.PingObject(go); } } }
脚本代码:
#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.Linq; using System.Collections; using System.Collections.Generic; [ExecuteInEditMode] public class LightmapScaleAnalysis : MonoBehaviour { public GameObject target = null; public Dictionary<GameObject, float> dic = new Dictionary<GameObject, float>(); public void Parse() { dic.Clear(); if (target == null) { dic.Clear(); return; } Renderer[] lstRenderer = target.GetComponentsInChildren<Renderer>(); foreach(var r in lstRenderer) { // 非LightmapStatic StaticEditorFlags flag = GameObjectUtility.GetStaticEditorFlags(r.gameObject); if ((flag & StaticEditorFlags.LightmapStatic) == 0) continue; SerializedObject so = new SerializedObject(r); if (dic.ContainsKey(r.gameObject) == false) { dic.Add(r.gameObject, so.FindProperty("m_ScaleInLightmap").floatValue); } // dic = dic.OrderBy(o => o.Value).ToDictionary(o => o.Key, o => o.Value); List<KeyValuePair<GameObject, float>> lst = new List<KeyValuePair<GameObject, float>>(dic); lst.Sort(delegate(KeyValuePair<GameObject, float> s1, KeyValuePair<GameObject, float> s2) { return s2.Value.CompareTo(s1.Value); }); dic.Clear(); foreach(var l in lst) { dic.Add(l.Key, l.Value); } } } } #endif
脚本检视窗口:
#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.Collections; [CustomEditor(typeof(LightmapScaleAnalysis))] public class LightmapScaleAnalysisInspector : Editor { private SerializedObject obj; private float specialRange = 0.8f; private Color specialColor = Color.red; private void OnEnable() { obj = new SerializedObject(target); } public override void OnInspectorGUI() { DrawDefaultInspector(); var analysis = target as LightmapScaleAnalysis; GUILayout.BeginHorizontal(); specialRange = EditorGUILayout.Slider(specialRange, 0f, 1f); specialColor = EditorGUILayout.ColorField(specialColor); GUILayout.EndHorizontal(); GUILayout.BeginVertical(); GUILayout.BeginHorizontal(); if (GUILayout.Button("解析吧")) { analysis.Parse(); } GUILayout.EndHorizontal(); foreach (var pair in analysis.dic) { GUILayout.BeginHorizontal(); GUI.color = pair.Value >= specialRange ? specialColor : Color.white; EditorGUILayout.ObjectField(pair.Key, typeof(GameObject)); EditorGUILayout.FloatField(pair.Value); GUI.color = Color.white; GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } } #endif