Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to Access Unity Default Skin

Discussion in 'Immediate Mode GUI (IMGUI)' started by pogoli, Aug 18, 2008.

  1. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    Is there a way I can get Unity's default skin?
    I want what GUI.skin returns if not set, or set to null. I would just use GUI.skin, but I want to access it outside of OnGui() calls. I want to set up custom GUIStyles in Start() rather than recreating them several times every frame in OnGUI().

    Here is my C# property.

    Code (csharp):
    1.  
    2. public GUISkin m_skin;
    3. public GUISkin skin {
    4.     get {
    5.         if (m_skin != null) {
    6.             return m_skin;
    7.         } else {
    8.             //return GUI.skin;
    9.             return null; //TODO return the default unity skin -- where do I get that from?
    10.         }
    11.     }
    12. }
    13.  
    So... How do I get the default skin?

    Thanks!
     
  2. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Create a GUISkin variable that is public to the class and in the OnGUI function assign that variable.

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4. if(defaultSkin == null)
    5. defaultSkin = GUI.skin;
    6. }
    7.  
    Im not sure why you want it though if you just want to create a GUISkin at start up and assign it to the gui just do that. I doubt a simple call as shown below is gonna be your performance bottleneck, if that is what you worry about.

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.    GUI.skin = mySkinInitedInStart;
    5. }
    6.  
     
  3. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    I am providing an editor-exposed-serialized skin reference called m_skin in a GUI thing.

    I want it so using the property "skin" that I provided code for above, will never return null. Then, in the case that a designer forgot to specify a skin-reference, the GUI thing will just look odd, rather than cause a crash. I know there are many many many other ways to avoid a crash here, but as I wrote this (and read what GUI.skin returns when uninitialized) I thought... "Hmmm... a default unity skin? I'd like to just return that if one isn't specified... Oh, I need to be in an OnGUI() scope? Well gosh, there must be another way to get at that default unity skin that GUI.skin returns when one isn't yet specified." and then I posted this forum topic.

    So... I am asking out of curiosity. It just feels like something I should be able to get at, and often these things are obtainable in a different way.

    Does anyone know that way?

    ... I don't really want a hack where I save what an uninitialized GUI.skin returns as a local variable. I want something that just gives me this default skin.


    Thanks!
     
    LinkiraStudios likes this.
  4. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    If you dont set GUI.skin each OnGUI function will be called with Unity's default skin. Are you trying to override the default skin?

    And you probably cant call GUI.skin outside (or should) as things might not have been initialized by the engine yet.

    Oh and if you read the documentation for GUI.skin you will see that simply setting it to null will revert to unity's default skin.
     
  5. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    to expand on what I am doing, here is some more code.

    Code (csharp):
    1. [System.Serializable]
    2. class GUIThingWindow : MonoBehaviour {
    3.     private GUIStyle m_specialStyle;
    4.     public Color m_textColor; //exposed to and specified via editor
    5.     public GUISkin m_skin; //exposed to and specified via editor
    6.     public GUISkin skin {
    7.         get {
    8.             if (m_skin != null) {
    9.                 return m_skin;
    10.             } else {
    11.                 return null; //TODO return the default skin
    12.             }  
    13.         }  
    14.     }
    15.    
    16.     private void SetupGuiStyles() {
    17.         m_specialStyle = new GUIStyle(skin.GetStyle("BaseCustomStyle")); //uses the property, which may be null (which is what I am trying to avoid)
    18.         m_specialStyle.normal.textColor = m_textColor;
    19.     }
    20.    
    21.     private void Start() {
    22.         SetupGuiStyles(); // My Preferred way to do this so I don't call "new GUIStyle()" several times a frame
    23.     }
    24.    
    25.     private void OnGUI() {
    26.         GUI.skin = m_skin;
    27.         //I don't want to call SetupGuiStyles() here because OnGUI() is executed
    28.         //several times each frame, and these styles will never change
    29.         //after their initial set up.
    30.         // and no... I'd rather not use a "doOnce" boolean.
    31.     }
    32. }
     
  6. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    You could do something like

    Code (csharp):
    1.  
    2. if(skin != null)
    3. {
    4.    // Call your setup code
    5. }
    6. else
    7. {
    8.    // Create a default GUISkin from code
    9. }
    10.  
     
  7. pogoli

    pogoli

    Joined:
    May 30, 2008
    Posts:
    41
    I have explored the option of creating a new skin using GUISkin.Instantiate(). But I'd rather have a way to get at the default unity skin. The one it uses when you just say GUI.Button(). Where is that and how do I get at it?

    Is it just plain not accessible? Is it something that is generated by unity each frame before each OnGUI() call?
     
  8. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Assign null to GUI.skin, then read it back :)

    You probably have to do it from OnGUI, though - so I'd just have a piece of code that goes something like this:
    Code (csharp):
    1.  
    2. void OnGUI () {
    3.     if (m_MyStyle == null) {
    4.        Initialize ();
    5.     }
    6. }
    7.  
    8. GUIStyle m_MyStyle;
    9. void Initialize () {
    10.     GUI.skin = NULL;
    11.     m_MyStyle = GUI.skin.GetStyle ("Button");
    12. }
    13.