Esempio n. 1
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            DrawPropertiesExcluding(serializedObject, new string[] { layersListName });
            layersList.DoLayoutList();
            serializedObject.ApplyModifiedProperties();

            LayerCompositor layerCompositor = (LayerCompositor)target;

            // protect against empty Layers set, and hide the buttons when not needed
            if ((layerCompositor.Layers != null) && (layerCompositor.Layers.Length > 0))
            {
                if (LayerCompositorHasDuplicates(layerCompositor))
                {
                    EditorGUILayout.HelpBox("Duplicate scenes are listed in the layers.", MessageType.Warning);
                }

                if (LayerCompositorContainsParentScene(layerCompositor))
                {
                    EditorGUILayout.HelpBox("The scene of this gameobject is listed as a layer.", MessageType.Warning);
                }

                if (GUILayout.Button("Load/unload selected layer"))
                {
                    if (layersList.index != -1)
                    {
                        LayerCompositor.Layer selectedLayer = layerCompositor.Layers[layersList.index];
                        if (LayerCompositor.IsLayerLoaded(selectedLayer))
                        {
                            layerCompositor.UnloadLayer(selectedLayer.id);
                        }
                        else
                        {
                            layerCompositor.LoadLayer(selectedLayer.id);
                        }
                    }
                }

                if (GUILayout.Button("Refresh"))
                {
                    LayerCompositor.RefreshLayers(null, layerCompositor.Layers, true, layerCompositor.gameObject.scene);
                }
            }
        }
Esempio n. 2
0
        private void OnEnable()
        {
            LayerCompositor layerCompositor = (LayerCompositor)target;

            layersList = new ReorderableList(serializedObject, serializedObject.FindProperty(layersListName), true, true, true, true);

            layersList.drawHeaderCallback = (Rect rect) =>
            {
                BuildRowOfColumns(rect, true);

                EditorGUI.LabelField(rect, new GUIContent("Layer index and Identifier", "Layer data. The Id is used at runtime to selectively Load/Unload a layer. Autoload specifies if the layer autoloads at runtime. Active constrols if the layer root objects are active after loading. Finally, provide a reference to a Unity scene."));
                EditorGUI.LabelField(rect3, new GUIContent("Autoload"));
                EditorGUI.LabelField(rect4, new GUIContent("Active"));
                EditorGUI.LabelField(rect5, new GUIContent("Scene"));
            };

            layersList.drawElementBackgroundCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                if ((layerCompositor.Layers == null) || (layerCompositor.Layers.Length == 0))
                {
                    return;
                }
                Texture2D             bgTex = new Texture2D(1, 1);
                Color                 bgCol;
                LayerCompositor.Layer selectedLayer = layerCompositor.Layers[index];
                bgCol = (LayerCompositor.IsLayerLoaded(selectedLayer) ? greenTint : redTint);
                if (isActive)
                {
                    bgCol += highlightTint;
                }
                bgTex.SetPixel(0, 0, bgCol);
                bgTex.Apply();
                GUI.DrawTexture(rect, bgTex as Texture);
            };

            layersList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                EditorGUI.BeginChangeCheck();

                SerializedProperty element = layersList.serializedProperty.GetArrayElementAtIndex(index);
                {
                    BuildRowOfColumns(rect, false);

                    GUIContent layerIdx = new GUIContent(index.ToString(), "A layer ID.");
                    EditorGUI.LabelField(rect1, layerIdx);
                    EditorGUI.PropertyField(rect2, element.FindPropertyRelative(nameof(LayerCompositor.Layer.id)), GUIContent.none);
                    EditorGUI.PropertyField(rect3, element.FindPropertyRelative(nameof(LayerCompositor.Layer.autoload)), GUIContent.none);
                    EditorGUI.PropertyField(rect4, element.FindPropertyRelative(nameof(LayerCompositor.Layer.loadactive)), GUIContent.none);
                    EditorGUI.PropertyField(rect5, element.FindPropertyRelative(nameof(LayerCompositor.Layer.scene)), GUIContent.none);
                }

                if (EditorGUI.EndChangeCheck())
                {
                    LayerCompositor.Layer[] previousLayers = layerCompositor.Layers.Clone() as LayerCompositor.Layer[];
                    serializedObject.ApplyModifiedProperties();
                    LayerCompositor.RefreshLayers(previousLayers, layerCompositor.Layers, true, layerCompositor.gameObject.scene);
                }
            };

            layersList.onAddCallback = (ReorderableList list) =>
            {
                ReorderableList.defaultBehaviours.DoAddButton(list);
                list.serializedProperty.serializedObject.ApplyModifiedProperties();
                LayerCompositor.Layer newLayer = layerCompositor.Layers[layerCompositor.Layers.Length - 1];
                newLayer.id         = "NoID";
                newLayer.autoload   = true;
                newLayer.loadactive = true;
            };

            layersList.onRemoveCallback = (ReorderableList list) =>
            {
                LayerCompositor.Layer[] previousLayers = layerCompositor.Layers.Clone() as LayerCompositor.Layer[];
                ReorderableList.defaultBehaviours.DoRemoveButton(list);
                list.serializedProperty.serializedObject.ApplyModifiedProperties();
                LayerCompositor.RefreshLayers(previousLayers, layerCompositor.Layers, true, layerCompositor.gameObject.scene);
                Repaint(); // removes Refresh when no layers remain
            };
        }