Пример #1
0
        void OnEnable()
        {
            MeshTreeBase meshTree = target as MeshTreeBase;

            m_isBuilding = meshTree.IsBuilding();
            if (!m_isBuilding)
            {
                m_memoryUsage = meshTree.GetMemoryUsage();
            }
            m_showBuiltData = true;
        }
Пример #2
0
        public override void OnInspectorGUI()
        {
            MeshTreeBase meshTreeBase = target as MeshTreeBase;

            if (meshTreeBase.IsBuilding())
            {
                GUI.enabled = false;
            }
            DrawDefaultInspector();
            if (meshTreeBase is MeshTree)
            {
                MeshTree meshTree   = meshTreeBase as MeshTree;
                Object   meshObject = EditorGUILayout.ObjectField(meshTree.srcMesh is Mesh ? "Mesh" : "Root Object", meshTree.srcMesh, typeof(Object), true);
                if (meshObject != meshTree.srcMesh && meshObject is GameObject && Event.current.command)
                {
                    MeshFilter meshFilter = ((GameObject)meshObject).GetComponent <MeshFilter>();
                    if (meshFilter != null && meshFilter.sharedMesh != null)
                    {
                        meshObject = meshFilter.sharedMesh;
                    }
                }
                if (meshObject != meshTree.srcMesh)
                {
                    Undo.RegisterCompleteObjectUndo(meshTree, "Inspector");
                    meshTree.srcMesh = meshObject;
                    EditorUtility.SetDirty(meshTree);
                }
                if (meshObject is GameObject)
                {
                    PrefabType prefabType = PrefabUtility.GetPrefabType(meshObject);
                    if (prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab)
                    {
                        if (m_errorStyle == null)
                        {
                            m_errorStyle          = new GUIStyle();
                            m_errorStyle.richText = true;
                            m_errorStyle.wordWrap = true;
                        }
                        GUILayout.TextArea("<color=red>A reference to a scene object will not be serialized in Asset data. You will need to set the root object again when you rebuild the tree. Use a prefab instead.</color>", m_errorStyle);
                    }
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_layerMask"));
                    SerializedProperty excludeRenderTypes = serializedObject.FindProperty("m_excludeRenderTypes");
                    excludeRenderTypes.isExpanded = EditorGUILayout.Foldout(excludeRenderTypes.isExpanded, "Exclude Render Types");
                    if (excludeRenderTypes.isExpanded)
                    {
                        for (int i = 0; i < excludeRenderTypes.arraySize + 1; ++i)
                        {
                            string renderType;
                            if (m_editingRenderType == i)
                            {
                                renderType = m_editingString;
                            }
                            else if (i < excludeRenderTypes.arraySize)
                            {
                                renderType = excludeRenderTypes.GetArrayElementAtIndex(i).stringValue;
                            }
                            else
                            {
                                renderType = "";
                            }
                            string controlName = "RenderType" + i.ToString();
                            GUI.SetNextControlName(controlName);
                            string newRenderType  = EditorGUILayout.TextField(renderType);
                            string focusedControl = GUI.GetNameOfFocusedControl();
                            if (m_editingRenderType == i)
                            {
                                m_editingString = newRenderType;
                                if (focusedControl != controlName || (/*Event.current.isKey &&*/ (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)))
                                {
                                    ApplyEditingRenderType();
                                }
                                else if (/*Event.current.isKey &&*/ Event.current.keyCode == KeyCode.Escape)
                                {
                                    CancelEditingRenderType();
                                }
                            }
                            else if (renderType != newRenderType)
                            {
                                ApplyEditingRenderType();
                                m_editingRenderType = i;
                                m_editingString     = newRenderType;
                            }
                        }
                    }
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_scaledOffset"));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_fixedOffset"));
                    serializedObject.ApplyModifiedProperties();
                }
            }
            if (meshTreeBase is TerrainMeshTree)
            {
                TerrainMeshTree terrainMeshTree = meshTreeBase as TerrainMeshTree;
                Object          terrainObj      = EditorGUILayout.ObjectField("Terrain Data", terrainMeshTree.terrainData, typeof(Object), true);
                if (terrainObj != terrainMeshTree.terrainData)
                {
                    TerrainData terrainData = terrainObj as TerrainData;
                    if (terrainData == null && terrainObj is GameObject)
                    {
                        Terrain terrain = ((GameObject)terrainObj).GetComponent <Terrain>();
                        if (terrain != null)
                        {
                            terrainData = terrain.terrainData;
                        }
                    }
                    if (terrainData != null || terrainObj == null)
                    {
                        terrainMeshTree.terrainData = terrainData;
                    }
                }
            }
            if (meshTreeBase.IsBuilding())
            {
                GUI.enabled  = true;
                m_isBuilding = true;
                EditorGUILayout.LabelField("Building... " + Mathf.FloorToInt(100 * meshTreeBase.GetBuildProgress()).ToString() + "%");
            }
            else
            {
                GUI.enabled = meshTreeBase.IsReadyToBuild();
                if (m_isBuilding)
                {
                    m_isBuilding  = false;
                    m_memoryUsage = meshTreeBase.GetMemoryUsage();
                    EditorUtility.SetDirty(meshTreeBase);
                }
                if (meshTreeBase.IsPrebuilt())
                {
                    m_showBuiltData = EditorGUILayout.Foldout(m_showBuiltData, "Built Tree Info");
                    if (m_showBuiltData)
                    {
                        string memorySize;
                        float  mb = m_memoryUsage / (1024.0f * 1024.0f);
                        if (1.0f <= mb)
                        {
                            memorySize = mb.ToString("f3") + "MB";
                        }
                        else
                        {
                            float kb = m_memoryUsage / 1024.0f;
                            memorySize = kb.ToString("f3") + "KB";
                        }
                        EditorGUILayout.LabelField("Memory", memorySize);
                        EditorGUILayout.LabelField("Node Count", meshTreeBase.GetNodeCount().ToString());
                    }
                }
                if (GUILayout.Button(meshTreeBase.IsPrebuilt() ? "Rebuild" : "Build"))
                {
                    ApplyEditingRenderType();
                    m_isBuilding = true;
                    BuildMeshTree(meshTreeBase);
                }
            }
            GUI.enabled = true;
        }