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

Issue with applying GUI skin to existing GUI elements

Discussion in 'Immediate Mode GUI (IMGUI)' started by weneverfail, Sep 19, 2014.

  1. weneverfail

    weneverfail

    Joined:
    Sep 10, 2014
    Posts:
    3
    Hi guys,

    I've spent the last hour or two on Google trying to figure out why my GUI skin wasn't modifying any existing objects. (objects which I added pre-runtime)

    Here's my code which (as far as I've understood) applies a GUI skin globally.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GUISkinHelper : MonoBehaviour {
    6.  
    7.     public GUISkin skin;
    8.  
    9.     void OnGUI () {
    10.         GUI.skin = skin;
    11.  
    12.         //This appears to be the only way to display a UI element with my skin.
    13.         Rect buttonRect = new Rect (10, 10, 150, 20);
    14.         GUI.Button (buttonRect, "Skinned Button");
    15.  
    16.  
    17.     }
    18.  
    19. }
    20.  
    I've added some code that generates a GUI button for testing purposes, and this renders just fine with my new GUI skin. Existing elements however remain to be displayed with the default skin.

    I'm sure this is just a silly question and I must be missing something, but I just can't find out what it is.

    I hope you can help me, thanks :)
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    What about adding some Debug.Log() calls to verify that the skin is actually getting set. For example, in the OnGUI() method for your pre-runtime object, add something like:
    Code (csharp):
    1. GUI.skin = skin;
    2. Debug.Log("Using GUI skin: " + GUI.skin.name);
     
  3. weneverfail

    weneverfail

    Joined:
    Sep 10, 2014
    Posts:
    3
    Hi Tony, thanks for your response.

    I know that the skin is actually being set, because if I create a new instance of (in my example) a button at runtime, it renders properly with the my custom GUI skin.

    I tried out your example either way and the debug log got filled up very quickly due to the OnGUI method being called every frame. It confirmed that my custom GUI skin was being used.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    It's even worse than that -- once for every UI event, which often means multiple times per frame.

    Are you creating the skin at runtime (in script) or pre-runtime (in the editor)? If it's at runtime, are you sure that the 'button' style is defined at the time that OnGUI() is being called? Sorry, that's all I got. Can you post your OnGUI() code?
     
  5. weneverfail

    weneverfail

    Joined:
    Sep 10, 2014
    Posts:
    3
    I'm creating the skin pre-runtime, I saved it as an asset. (I think this is the way GUI skins work, wasn't aware of the fact that they could be generated at runtime) I also forgot to mention that I'm working with the open beta (4.6.0b18), perhaps some things have changed regarding GUI skins? I was unable to find a changelog or any beta tutorial regarding GUI skins.