Search code examples
c#user-interfaceunity-game-engineunity-editor

Is possible to use EditorStyle on Unity in-game to do Horizontal Groups of Buttons?


I want to use EditorStyles on an in-game situation. I inspected EditorStyles class, and I realize that everything comes from:

        private GUIStyle GetStyle(string styleName)
        {
            GUIStyle s = GUI.skin.FindStyle(styleName) ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle(styleName);
            if (s == null)
            {
                Debug.LogError("Missing built-in guistyle " + styleName);
                s = GUISkin.error;
            }
            return s;
        }

Copied from: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/EditorStyles.cs#L425

The reference call is here: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/EditorStyles.cs#L333

And this is what I use (on Editor): https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/GUI/EditorStyles.cs#L71

All this to do the following:

...

I have to do the following:

 EditorGUILayout.BeginHorizontal ();
 if (GUILayout.Button ("Select", EditorStyles.miniButtonLeft)) {
 }
 if (GUILayout.Button ("Revert", EditorStyles.miniButton)) {
 }
 if (GUILayout.Button ("Apply", EditorStyles.miniButtonRight)) {
 }
 EditorGUILayout.EndHorizontal ();

But, I want to know if there is any possible way to do this in-game. Is this possible?


Solution

  • I forgot that there were a way of doing this.

    With an Editor script, we will save on out disk Editor guiskins:

            bool saveAssets = false;
            foreach (var editorSkin in Enum.GetValues(typeof(EditorSkin)).Cast<EditorSkin>())
            {
                string file = Path.Combine("Assets", "StreamingAssets", $"{editorSkin.ToString()}.guiskin");
    
                if (!File.Exists(file))
                {
                    AssetDatabase.CreateAsset(Instantiate(EditorGUIUtility.GetBuiltinSkin(editorSkin)), file);
                    saveAssets = true;
                }
            }
    
            if (saveAssets)
                AssetDatabase.SaveAssets();
    

    And then, we could just save then on Resources folder, and load it with Resources.Load:

    ...

    //
    // Summary:
    //     Enum that selects which skin to return from EditorGUIUtility.GetBuiltinSkin.
    public enum GlobalSkin
    {
        //
        // Summary:
        //     The skin used for game views.
        Game = 0,
    
        //
        // Summary:
        //     The skin used for inspectors.
        Inspector = 1,
    
        //
        // Summary:
        //     The skin used for Scene views.
        Scene = 2
    }
    

    And then, just create a method to return them:

        // Get one of the built-in GUI skins, which can be the game view, inspector or scene view skin as chosen by the parameter.
        public static GUISkin GetBuiltinSkin(GlobalSkin skin)
        {
            return Resources.Load<GUISkin>($"Skins/{skin.ToString()}");
        }
    

    Everything else is done by using GUISkin.FindStyle("whatever").