zoukankan      html  css  js  c++  java
  • 列出场景对象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

     

      

  • 相关阅读:
    java 判断是否满足正则表达式
    bootstrap-select多选框与Vue整合,下拉的数据刷新不出
    Windows 修改host文件不起作用
    【服务器】【tomcat】Tomcat 应用目录重定向
    【Spring】Spring装配Bean的顺序
    【Maven】m2e以原型创建Maven Project再次失败
    【Maven】m2eclipse以maven-archetype-quickstart原型创建Maven Project失败的解决
    【Java SE】jar xvf "path"命令
    【Java SE】形参被初始化为实参的值的拷贝
    【Java SE】斐波那契数列
  • 原文地址:https://www.cnblogs.com/sifenkesi/p/5213986.html
Copyright © 2011-2022 走看看