コード例 #1
0
 public void AssignColorsToSelection(GameObject gameObject, ColorSet colorSet)
 {
     recursiveLevel++;
     if (gameObject.GetComponent <UnityEngine.UI.Button>())
     {
         UnityEngine.UI.Button button = gameObject.GetComponent <UnityEngine.UI.Button>();
         SetColorBlock(button, colorSet);
         SetDetailColor(gameObject, colorSet);
         EditorUtility.SetDirty(button);
     }
     else if (gameObject.GetComponent <UnityEngine.UI.InputField>())
     {
         UnityEngine.UI.InputField input = gameObject.GetComponent <UnityEngine.UI.InputField>();
         SetColorBlock(input, colorSet);
         input.selectionColor = colorSet.highlighted;
         Undo.RecordObject(input.textComponent, "Change Text color");
         input.textComponent.color = colorSet.pressed;
         Undo.RecordObject(input.placeholder, "Change Placeholder color");
         input.placeholder.color = colorSet.highlighted;
         EditorUtility.SetDirty(input);
         EditorUtility.SetDirty(input.textComponent);
         EditorUtility.SetDirty(input.placeholder);
     }
     else if (gameObject.GetComponent <UnityEngine.UI.Scrollbar>())
     {
         UnityEngine.UI.Scrollbar sb = gameObject.GetComponent <UnityEngine.UI.Scrollbar>();
         SetColorBlock(sb, colorSet);
         Undo.RecordObject(gameObject.GetComponent <UnityEngine.UI.Image>(), "Change Image color");
         gameObject.GetComponent <UnityEngine.UI.Image>().color = colorSet.disabled;
         EditorUtility.SetDirty(sb);
         EditorUtility.SetDirty(gameObject.GetComponent <UnityEngine.UI.Image>());
     }
     else if (gameObject.GetComponent <UnityEngine.UI.Slider>())
     {
         UnityEngine.UI.Slider slider = gameObject.GetComponent <UnityEngine.UI.Slider>();
         SetColorBlock(slider, colorSet);
         Undo.RecordObject(slider.fillRect.gameObject.GetComponent <UnityEngine.UI.Image>(), "Change Image color");
         slider.fillRect.gameObject.GetComponent <UnityEngine.UI.Image>().color = colorSet.normal;
         SetTextColorRecursive(gameObject, colorSet);
         EditorUtility.SetDirty(slider);
         EditorUtility.SetDirty(slider.fillRect.gameObject.GetComponent <UnityEngine.UI.Image>());
     }
     else if (gameObject.GetComponent <UnityEngine.UI.Toggle>())
     {
         UnityEngine.UI.Toggle toggle = gameObject.GetComponent <UnityEngine.UI.Toggle>();
         SetColorBlock(toggle, colorSet);
         Undo.RecordObject(toggle.graphic, "Change Image color");
         toggle.graphic.color = colorSet.normal;
         SetTextColorRecursive(gameObject, colorSet);
         EditorUtility.SetDirty(toggle);
         EditorUtility.SetDirty(toggle.graphic);
     }
     else if (gameObject.transform.childCount > 0) // Recursive search for components
     {
         for (int i = 0; i < gameObject.transform.childCount; i++)
         {
             AssignColorsToSelection(gameObject.transform.GetChild(i).gameObject, colorSet);
         }
     }
     else if (recursiveLevel == 1)
     {
         if (gameObject.GetComponent <UnityEngine.UI.Image>())
         {
             UnityEngine.UI.Image image = gameObject.GetComponent <UnityEngine.UI.Image>();
             Undo.RecordObject(image, "Change color");
             image.color = colorSet.normal;
             EditorUtility.SetDirty(image);
         }
         else if (gameObject.GetComponent <UnityEngine.UI.Text>())
         {
             UnityEngine.UI.Text text = gameObject.GetComponent <UnityEngine.UI.Text>();
             Undo.RecordObject(text, "Change color");
             text.color = colorSet.normal;
             EditorUtility.SetDirty(text);
         }
     }
 }
コード例 #2
0
        void OnGUI()
        {
            if (theme == null)
            {
                FindTheme();
            }

            //Checks if there is any style not initialized
            SetStyles();

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos);

            EditorGUI.BeginChangeCheck();
            Undo.RecordObject(theme, "Content change in theme");

            GUILayout.BeginHorizontal();
            GamestrapTheme newTheme = (GamestrapTheme)EditorGUILayout.ObjectField(theme, typeof(GamestrapTheme), false);

            if (theme != newTheme)
            {
                string assetPathAndName = AssetDatabase.GetAssetPath(newTheme);
                EditorPrefs.SetString(prefsKey, assetPathAndName);
            }

            theme = newTheme;

            //Save as button UI Logic
            SaveAs();

            GUILayout.EndHorizontal();

            #region Palette GUI
            GUILayout.Label("Palette", titleStyle);
            GUILayout.Label("Right click to assign color to selection");

            EditorGUILayout.BeginVertical("Box");

            EditorGUILayout.BeginHorizontal();
            int counter = 0;
            foreach (ColorSet colorSet in theme.palette)
            {
                GUI.backgroundColor = colorSet.normal; // Sets the button color
                if (GUILayout.Button("", (colorSet == selectedColorSet) ? btnSelectedStyle : btnStyle, GUILayout.Width((position.width - 35f) / rowCount), GUILayout.Height(40)))
                {
                    // To remove focus from any input field which might lead to errors
                    GUI.FocusControl("Null");
                    if (Event.current.button == 1)
                    {
                        AssignColorsToSelection(colorSet);
                    }
                    else if (Event.current.button == 0)
                    {
                        if (colorSet == selectedColorSet)
                        {
                            selectedColorSet = null;
                        }
                        else
                        {
                            selectedColorSet = colorSet;
                        }
                    }
                }
                counter++;
                if (counter % rowCount == 0)
                {
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                }
            }
            GUI.backgroundColor = Color.white;
            if (GUILayout.Button("Add", btnStyle, GUILayout.Width((position.width - 20f) / rowCount), GUILayout.Height(40)))
            {
                GUI.FocusControl("Null");
                Color[] baseColor = colors[UnityEngine.Random.Range(0, colors.Count - 1)];
                selectedColorSet = new ColorSet(baseColor);

                theme.palette.Add(selectedColorSet);
            }
            GUI.backgroundColor = defaultBg;

            GUILayout.EndHorizontal();

            if (selectedColorSet != null)
            {
                GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) });

                GUILayout.BeginHorizontal();
                GUILayout.Label("Color Details", subtitleStyle);
                GUI.backgroundColor = deleteColor;
                if (GUILayout.Button("Delete", btnStyleWhiteFont, GUILayout.Width(50f)))
                {
                    theme.palette.Remove(selectedColorSet);
                    selectedColorSet = null;
                }
                GUI.backgroundColor = defaultBg;
                if (GUILayout.Button("X", GUILayout.Width(20)))
                {
                    selectedColorSet = null;
                }
                GUILayout.EndHorizontal();
                if (selectedColorSet != null)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Label("Name", GUILayout.Width(100));
                    selectedColorSet.name = EditorGUILayout.TextField(selectedColorSet.name);
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Label("Tag", GUILayout.Width(100));
                    selectedColorSet.tag = EditorGUILayout.TagField(selectedColorSet.tag);
                    EditorGUILayout.EndHorizontal();

                    selectedColorSet.normal      = EditorGUILayout.ColorField("Normal", selectedColorSet.normal);
                    selectedColorSet.highlighted = EditorGUILayout.ColorField("Highlighted", selectedColorSet.highlighted);
                    selectedColorSet.pressed     = EditorGUILayout.ColorField("Pressed", selectedColorSet.pressed);
                    selectedColorSet.disabled    = EditorGUILayout.ColorField("Disabled", selectedColorSet.disabled);
                    selectedColorSet.detail      = EditorGUILayout.ColorField("Detail", selectedColorSet.detail);

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Apply ColorSet to:", GUILayout.Width(125));
                    GUI.backgroundColor = applyColor;

                    if (GUILayout.Button("Selected", btnStyleWhiteFont))
                    {
                        AssignColorsToSelection(selectedColorSet);
                    }
                    GUI.backgroundColor = defaultBg;

                    GUI.enabled = selectedColorSet.tag.Length > 0 && selectedColorSet.tag != "Untagged";

                    if (GUILayout.Button("Tag"))
                    {
                        ApplyColorSetTag(selectedColorSet);
                    }

                    GUI.enabled = true;
                    GUILayout.EndHorizontal();

                    #region Helper color selection
                    GUILayout.BeginHorizontal();
                    bool lastShowSceneColors = showSceneColors;
                    showColors      = GUILayout.Toggle(showColors, "Suggestions", "Button");
                    showSuggestions = GUILayout.Toggle(showSuggestions, "Schemes", "Button");
                    showSceneColors = GUILayout.Toggle(showSceneColors, "Scene Colors", "Button");
                    // If the toggle was activated, refresh and search for the new colors in scene
                    if (showSceneColors != lastShowSceneColors && showSceneColors)
                    {
                        SearchSceneColors();
                    }
                    GUILayout.EndHorizontal();

                    if (showSceneColors)
                    {
                        GUILayout.Label("Scene Colors", EditorStyles.boldLabel);
                        GUI.backgroundColor = Color.black;
                        GUILayout.BeginVertical(bgStyle);
                        GUILayout.BeginHorizontal();
                        counter = 0;
                        foreach (Color color in sceneColors)
                        {
                            GUI.backgroundColor = color; // Sets the button color
                            if (GUILayout.Button("", btnStyle))
                            {
                                SetColors(GetColorDefault(color));
                                selectedColor = color;
                            }
                            counter++;
                            if (counter % 5 == 0)
                            {
                                // Start a new row each 5
                                GUILayout.EndHorizontal();
                                GUI.backgroundColor = Color.black;
                                GUILayout.BeginHorizontal();
                            }
                        }
                        GUILayout.EndHorizontal();
                        GUI.backgroundColor = defaultBg; // Resets the color background

                        GUILayout.EndVertical();
                    }

                    if (showSuggestions)
                    {
                        GUILayout.Label("Color Scheme Generator", EditorStyles.boldLabel);
                        paletteType   = (SchemeType)EditorGUILayout.EnumPopup("Scheme: ", paletteType);
                        selectedColor = EditorGUILayout.ColorField("Base:", selectedColor);

                        GUI.backgroundColor = Color.black;
                        GUILayout.BeginVertical(bgStyle);
                        GUI.backgroundColor = selectedColor; // Sets the button color
                        if (GUILayout.Button("", btnStyle))
                        {
                            SetColors(GetColorDefault(selectedColor));
                        }
                        Color[] paletteColors = GamestrapHelper.GetColorPalette(selectedColor, paletteType);
                        GUILayout.BeginHorizontal();
                        for (int i = 0; i < paletteColors.Length; i++)
                        {
                            GUI.backgroundColor = paletteColors[i]; // Sets the button color
                            if (GUILayout.Button("", btnStyle))
                            {
                                SetColors(GetColorDefault(paletteColors[i]));
                            }
                        }
                        GUI.backgroundColor = defaultBg; // Resets the color background
                        GUILayout.EndHorizontal();
                        GUILayout.EndVertical();
                    }

                    if (showColors)
                    {
                        GUILayout.Label("Color Suggestions", EditorStyles.boldLabel);
                        GUI.backgroundColor = Color.black;
                        GUILayout.BeginVertical(bgStyle);
                        GUILayout.BeginHorizontal();
                        counter = 0;
                        foreach (Color[] color in colors)
                        {
                            GUI.backgroundColor = color[0]; // Sets the button color
                            if (GUILayout.Button("", btnStyle) && color.Length >= 5)
                            {
                                SetColors(color);
                                selectedColor = color[0];
                            }
                            counter++;
                            if (counter % 5 == 0)
                            {
                                // Start a new row each 5
                                GUILayout.EndHorizontal();
                                GUI.backgroundColor = Color.black;
                                GUILayout.BeginHorizontal();
                            }
                        }
                        GUILayout.EndHorizontal();
                        GUI.backgroundColor = defaultBg; // Resets the color background

                        GUILayout.EndVertical();
                    }
                    #endregion
                }
            }
            EditorGUILayout.EndVertical();

            #endregion

            #region Effects
            GUILayout.Label("Effects", titleStyle);
            EditorGUILayout.BeginVertical("Box");
            Color effectBackground = new Color(0.6f, 0.6f, 0.6f);
            for (int i = 0; i < theme.effectSets.Count; i++)
            {
                EffectSet set     = theme.effectSets[i];
                bool      pressed = (set == selectedEffectSet);
                GUI.backgroundColor = effectBackground;
                bool newPressed = GUILayout.Toggle(pressed, set.name, "Button");
                GUI.backgroundColor = defaultBg;
                if (pressed != newPressed)
                {
                    // To remove focus from any input field which might lead to errors
                    GUI.FocusControl("Null");
                    if (newPressed)
                    {
                        if (Event.current.button == 1)
                        {
                            ActivateEffects(set);
                        }
                        else if (Event.current.button == 0)
                        {
                            selectedEffectSet = set;
                        }
                    }
                    else if (Event.current.button == 0)
                    {
                        selectedEffectSet = null;
                    }
                }
                if (selectedEffectSet != null && set == selectedEffectSet)
                {
                    GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) });

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Effect Details", subtitleStyle);
                    GUI.backgroundColor = deleteColor;
                    if (GUILayout.Button("Delete", btnStyleWhiteFont, GUILayout.Width(50f)))
                    {
                        theme.effectSets.Remove(selectedEffectSet);
                        selectedEffectSet = null;
                        i--;
                    }
                    GUI.backgroundColor = defaultBg;
                    if (GUILayout.Button("X", GUILayout.Width(20)))
                    {
                        selectedEffectSet = null;
                    }
                    GUILayout.EndHorizontal();

                    if (selectedEffectSet != null)
                    {
                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Label("Name", GUILayout.Width(75));
                        selectedEffectSet.name = EditorGUILayout.TextField(selectedEffectSet.name);
                        EditorGUILayout.EndHorizontal();

                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Label("Tag", GUILayout.Width(75));
                        selectedEffectSet.tag = EditorGUILayout.TagField(selectedEffectSet.tag);
                        EditorGUILayout.EndHorizontal();

                        selectedEffectSet.shadow = EditorGUILayout.ToggleLeft("Shadow", selectedEffectSet.shadow);
                        if (selectedEffectSet.shadow)
                        {
                            selectedEffectSet.shadowColor  = EditorGUILayout.ColorField("Color", selectedEffectSet.shadowColor);
                            selectedEffectSet.shadowOffset = EditorGUILayout.Vector2Field("Effect Distance", selectedEffectSet.shadowOffset);
                        }

                        selectedEffectSet.gradient = EditorGUILayout.ToggleLeft("Gradient", selectedEffectSet.gradient);
                        if (selectedEffectSet.gradient)
                        {
                            selectedEffectSet.gradientTop    = EditorGUILayout.ColorField("Color Top", selectedEffectSet.gradientTop);
                            selectedEffectSet.gradientBottom = EditorGUILayout.ColorField("Color Bottom", selectedEffectSet.gradientBottom);
                        }

                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Apply EffectSet to:", GUILayout.Width(125));
                        GUI.backgroundColor = applyColor;
                        if (GUILayout.Button("Selection", btnStyleWhiteFont))
                        {
                            ActivateEffects(selectedEffectSet);
                        }
                        GUI.backgroundColor = defaultBg;
                        GUI.enabled         = selectedEffectSet.tag.Length > 0 && selectedEffectSet.tag != "Untagged";
                        if (GUILayout.Button("Tag"))
                        {
                            ApplyEffectSetTag(selectedEffectSet);
                        }
                        GUI.enabled = true;
                        GUILayout.EndHorizontal();
                    }
                }
            }

            if (GUILayout.Button("Add Effect Set", GUILayout.Height(35)))
            {
                GUI.FocusControl("Null");
                selectedEffectSet      = new EffectSet();
                selectedEffectSet.name = "New Effect";
                theme.effectSets.Add(selectedEffectSet);
            }
            EditorGUILayout.EndVertical();
            #endregion

            #region Font
            GUILayout.Label("Font", titleStyle);
            GUILayout.BeginHorizontal();
            font = (Font)EditorGUILayout.ObjectField(font, typeof(Font), false);
            if (GUILayout.Button("Apply"))
            {
                AssignFontToSelection();
            }
            GUILayout.EndHorizontal();
            #endregion

            #region Additional Options
            showGlobalAction = EditorGUILayout.Foldout(showGlobalAction, "Additional Options");

            if (showGlobalAction)
            {
                GUI.backgroundColor = applyColor;
                if (GUILayout.Button("Apply theme to Tags in scene", btnStyleWhiteFont))
                {
                    foreach (ColorSet set in theme.palette)
                    {
                        ApplyColorSetTag(set);
                    }

                    foreach (EffectSet set in theme.effectSets)
                    {
                        ApplyEffectSetTag(set);
                    }
                }
                GUI.backgroundColor = defaultBg;
                if (GUILayout.Button("Generate Palette From Scene"))
                {
                    GetSceneColors();
                    foreach (Color c in sceneColors)
                    {
                        ColorSet cs     = new ColorSet();
                        Color[]  colors = GetColorDefault(c);
                        cs.normal      = colors[0];
                        cs.highlighted = colors[1];
                        cs.pressed     = colors[2];
                        cs.disabled    = colors[3];
                        cs.detail      = colors[4];
                        theme.palette.Add(cs);
                    }
                }
                GUI.backgroundColor = deleteColor;
                if (GUILayout.Button("Clear palette and effects", btnStyleWhiteFont))
                {
                    theme.palette.Clear();
                    theme.effectSets.Clear();
                }
                GUI.backgroundColor = defaultBg;
            }
            #endregion

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(theme);
            }
            EditorGUILayout.EndScrollView();
        }