码迷,mamicode.com
首页 > Web开发 > 详细

列出场景对象Lightmap属性

时间:2016-02-24 20:45:38      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:

首先上效果图:

技术分享

  编辑器代码:

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

 

 

  

列出场景对象Lightmap属性

标签:

原文地址:http://www.cnblogs.com/sifenkesi/p/5213986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!