Пример #1
0
        void SaveAs()
        {
            if (theme == null)
            {
                return;
            }

            if (GUILayout.Button("Save as...", btnLinestyle))
            {
                string themePath = AssetDatabase.GetAssetPath(theme.GetInstanceID());
                themePath = themePath.Substring(0, themePath.LastIndexOf('/') + 1);

                string path = EditorUtility.SaveFilePanel("Save Theme", themePath, "New Theme.asset", "asset");
                if (path.Length != 0)
                {
                    path = path.Substring(path.IndexOf("Assets/"));
                    GamestrapTheme newTheme = ScriptableObject.CreateInstance <GamestrapTheme>();
                    foreach (ColorSet cs in theme.palette)
                    {
                        newTheme.palette.Add(cs.Clone());
                    }
                    foreach (EffectSet es in theme.effectSets)
                    {
                        newTheme.effectSets.Add(es.Clone());
                    }
                    theme = newTheme;
                    AssetDatabase.CreateAsset(theme, path);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();

                    EditorPrefs.SetString(prefsKey, path);
                }
            }
        }
Пример #2
0
        public static void CreateAsset()
        {
            GamestrapTheme t = CreateAsset <GamestrapTheme>();

            t.palette    = new List <ColorSet>();
            t.effectSets = new List <EffectSet>();
        }
Пример #3
0
 public static bool OpenInProjectWindow(int instanceID, int line)
 {
     UnityEngine.Object obj = EditorUtility.InstanceIDToObject(instanceID);
     if (obj is GamestrapTheme)
     {
         GamestrapTheme theme  = obj as GamestrapTheme;
         ThemeWindow    window = ShowWindow();
         window.Theme = theme;
         return(true);
     }
     return(false);
 }
Пример #4
0
        private void CreateTheme()
        {
            theme            = ScriptableObject.CreateInstance <GamestrapTheme>();
            theme.palette    = new List <ColorSet>();
            theme.effectSets = new List <EffectSet>();

            string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath("Assets/Gamestrap UI/Basic Theme.asset");

            AssetDatabase.CreateAsset(theme, assetPathAndName);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            EditorPrefs.SetString(prefsKey, assetPathAndName);
        }
Пример #5
0
 void FindTheme()
 {
     if (EditorPrefs.HasKey(prefsKey))
     {
         theme = (GamestrapTheme)AssetDatabase.LoadAssetAtPath(EditorPrefs.GetString(prefsKey), typeof(GamestrapTheme));
     }
     if (theme == null)
     {
         string[] guids = AssetDatabase.FindAssets("t:GamestrapTheme");
         if (guids.Length > 0)
         {
             string assetPathAndName = AssetDatabase.GUIDToAssetPath(guids[0]);
             theme = (GamestrapTheme)AssetDatabase.LoadAssetAtPath(assetPathAndName, typeof(GamestrapTheme));
             EditorPrefs.SetString(prefsKey, assetPathAndName);
         }
         else
         {
             CreateTheme();
         }
     }
 }
Пример #6
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();
        }