예제 #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            bool markSceneChanges = false;

            EditorGUI.BeginProperty(position, label, property);

            EditorGUI.BeginChangeCheck();

            // Calculate rects
            var colorRect        = new Rect(position.x, position.y, 80, position.height);
            var opRect           = new Rect(position.x + 90, position.y, 110, position.height);
            var replaceColorRect = new Rect(position.x + 200, position.y, 80, position.height);
            var removeRect       = new Rect(position.x + 290, position.y, 60, position.height);

            // Draw fields - passs GUIContent.none to each so they are drawn without labels
            EditorGUI.PropertyField(colorRect, property.FindPropertyRelative("color"), GUIContent.none);
            SerializedProperty prop = property.FindPropertyRelative("operation");

            EditorGUI.PropertyField(opRect, prop, GUIContent.none);
            if (prop.intValue == (int)ColorOperation.Replace)
            {
                EditorGUI.PropertyField(replaceColorRect, property.FindPropertyRelative("replaceColor"), GUIContent.none);
            }
            Recolor rc = (Recolor)property.serializedObject.targetObject;

            if (GUI.Button(removeRect, "Remove", EditorStyles.miniButton))
            {
                if (rc.colorOperations == null)
                {
                    return;
                }

                List <ColorEntry> od = new List <ColorEntry>(rc.colorOperations);
                int index            = property.GetArrayIndex();
                od.RemoveAt(index);
                rc.colorOperations = od.ToArray();
                markSceneChanges   = true;
                rc.dirty           = true;
                EditorUtility.SetDirty(rc);
            }

            EditorGUI.EndProperty();

            if (EditorGUI.EndChangeCheck())
            {
                markSceneChanges = true;
            }

            if (markSceneChanges && !Application.isPlaying)
            {
                rc.dirty = true;
                UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
            }
        }
예제 #2
0
        public override void OnInspectorGUI()
        {
            Recolor rc = (Recolor)target;

            if (rc.GetComponent <Renderer>() == null)
            {
                EditorGUILayout.HelpBox("Recolor script requires an GameObject with a MeshRenderer or SpriteRenderer component.", MessageType.Warning);
                return;
            }

            serializedObject.Update();

            EditorGUILayout.PropertyField(this.palette);

            CSPalette palette = (CSPalette)this.palette.objectReferenceValue;

            if (palette != null)
            {
                if (palette.material == null || palette.material.GetColorArray("_Colors") == null)
                {
                    palette.UpdateMaterial();
                }

                EditorGUILayout.BeginVertical(GUI.skin.box);

                Rect space = EditorGUILayout.BeginVertical();
                GUILayout.Space(64);
                EditorGUILayout.EndVertical();

                palette.material.SetVector("_CursorPos", Vector3.left);
                EditorGUI.DrawPreviewTexture(space, Texture2D.whiteTexture, palette.material);

                if (GUILayout.Button("Open in Color Studio"))
                {
                    CSWindow cs = CSWindow.ShowWindow();
                    cs.LoadPalette(palette);
                }

                EditorGUILayout.EndVertical();

                EditorGUILayout.PropertyField(applyPalette);
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Open Color Studio"))
                {
                    CSWindow.ShowWindow();
                }
                if (GUILayout.Button("Help"))
                {
                    EditorUtility.DisplayDialog("Quick Help", "This Recolor script changes colors of the gameobject or sprite at runtime.\n\nIf you assign a palette created with Color Studio, Recolor will transform the colors of the original texture to the nearest colors of the palette.\n\nYou can also specify custom color operations, like preserving or replacing individual colors from the original texture.", "Ok");
                }
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.PropertyField(mode, new GUIContent("Recolor Mode"));
            EditorGUILayout.PropertyField(colorMatch);
            EditorGUILayout.PropertyField(threshold, new GUIContent("Color Threshold"));
            EditorGUILayout.PropertyField(materialIndex);

            if (mode.intValue != (int)RecolorMode.MainColorOnly)
            {
                if (originalTexture == null)
                {
                    originalTexture = rc.GetOriginalTexture();
                    if (originalTexture != null)
                    {
                        originalTexture            = Instantiate <Texture2D>(originalTexture);
                        originalTexture.filterMode = FilterMode.Point;
                    }
                    originalColors = rc.GetOriginalUniqueColors();
                }

                EditorGUILayout.PropertyField(showOriginalTexture);
                if (showOriginalTexture.boolValue)
                {
                    if (originalTexture != null)
                    {
                        EditorGUILayout.BeginVertical(GUI.skin.box);

                        Rect space = EditorGUILayout.BeginVertical();
                        GUILayout.Space(128);
                        EditorGUILayout.EndVertical();

                        EditorGUI.DrawPreviewTexture(space, originalTexture);
                        EditorGUILayout.EndVertical();
                    }
                }
            }

            EditorGUILayout.Separator();
            EditorGUILayout.PropertyField(colorOperations, new GUIContent("Per Color Operations"), true);

            if (originalColors != null && originalColors.Count < 64 && GUILayout.Button("Add All Texture Colors"))
            {
                colorOperations.isExpanded = true;
                List <ColorEntry> cc = new List <ColorEntry>();
                if (rc.colorOperations != null)
                {
                    for (int k = 0; k < rc.colorOperations.Length; k++)
                    {
                        int index = originalColors.IndexOf(rc.colorOperations[k].color);
                        if (index >= 0)
                        {
                            originalColors.RemoveAt(index);
                        }
                        cc.Add(rc.colorOperations[k]);
                    }
                }
                for (int k = 0; k < originalColors.Count; k++)
                {
                    ColorEntry ce = new ColorEntry {
                        color = originalColors[k], operation = ColorOperation.Preserve, replaceColor = originalColors[k]
                    };
                    cc.Add(ce);
                }
                rc.colorOperations = cc.ToArray();
                EditorUtility.SetDirty(rc);
                serializedObject.Update();
                requireRefresh = true;
            }

            if (mode.intValue != (int)RecolorMode.MainColorOnly)
            {
                if (!rc.isSprite && originalTexture != null && GUILayout.Button("Add Main Texture Colors"))
                {
                    colorOperations.isExpanded = true;
                    List <ColorEntry> cc         = new List <ColorEntry>();
                    List <Color>      mainColors = rc.GetOriginalTextureMainColors();
                    if (mainColors != null)
                    {
                        if (rc.colorOperations != null)
                        {
                            for (int k = 0; k < rc.colorOperations.Length; k++)
                            {
                                int index = mainColors.IndexOf(rc.colorOperations[k].color);
                                if (index >= 0)
                                {
                                    mainColors.RemoveAt(index);
                                }
                                cc.Add(rc.colorOperations[k]);
                            }
                        }

                        for (int k = 0; k < mainColors.Count; k++)
                        {
                            ColorEntry ce = new ColorEntry {
                                color = mainColors[k], operation = ColorOperation.Preserve, replaceColor = mainColors[k]
                            };
                            cc.Add(ce);
                        }
                        rc.colorOperations = cc.ToArray();
                        EditorUtility.SetDirty(rc);
                        serializedObject.Update();
                        requireRefresh = true;
                    }
                }
                if (!rc.isSprite && mode.intValue == (int)RecolorMode.VertexColors && GUILayout.Button("Add Vertex Colors"))
                {
                    colorOperations.isExpanded = true;
                    List <ColorEntry> cc         = new List <ColorEntry>();
                    List <Color>      mainColors = rc.GetOriginalVertexColors();
                    if (rc.colorOperations != null)
                    {
                        for (int k = 0; k < rc.colorOperations.Length; k++)
                        {
                            int index = mainColors.IndexOf(rc.colorOperations[k].color);
                            if (index >= 0)
                            {
                                mainColors.RemoveAt(index);
                            }
                            cc.Add(rc.colorOperations[k]);
                        }
                    }

                    for (int k = 0; k < mainColors.Count; k++)
                    {
                        ColorEntry ce = new ColorEntry {
                            color = mainColors[k], operation = ColorOperation.Preserve, replaceColor = mainColors[k]
                        };
                        cc.Add(ce);
                    }
                    rc.colorOperations = cc.ToArray();
                    EditorUtility.SetDirty(rc);
                    serializedObject.Update();
                    requireRefresh = true;
                }
            }

            // Color adjustments
            EditorGUILayout.Separator();
            EditorGUILayout.PropertyField(enableColorAdjustments, new GUIContent("Color Correction"), true);
            if (enableColorAdjustments.boolValue)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(colorAdjustments, true);
                EditorGUI.indentLevel--;
            }

            CheckLUTSettings((Texture2D)lutProp.objectReferenceValue);

            if (rc.enabled)
            {
                if (GUILayout.Button("Refresh"))
                {
                    requireRefresh = false;
                    rc.Refresh();
                }
            }

            if (serializedObject.ApplyModifiedProperties() || rc.dirty || requireRefresh)
            {
                requireRefresh = true;
                rc.dirty       = false;
                if (rc.enabled)
                {
                    if (GUIUtility.hotControl == 0)
                    {
                        requireRefresh = false;
                        rc.Refresh();
                    }
                }
            }
        }