Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Calling Custom Styles

Discussion in 'Immediate Mode GUI (IMGUI)' started by LittleRainGames, Nov 1, 2017.

  1. LittleRainGames

    LittleRainGames

    Joined:
    Apr 7, 2016
    Posts:
    97
    I guess this question would be more related to C# instead of Unity, but it kind of has to do with the Editor as well.

    So I'm made a custom class which is going to be like Unity's EditorStyles, I noticed when you call something like EditorStyles.label or EditorStyles.toolbar there is no () after the label/toolbar, where when I call my function LittleStyles.SubTitleBar() I have to use brackets.

    I'm guessing its something to do with how the function is declared, and its really not causing me problems I'm just wondering and always trying to expand my knowledge.

    So the question is, what makes that function not have to use brackets?


    Edit: Also one more question. When creating styles I sometimes like to duplicate a current one, so basically

    Code (CSharp):
    1.  
    2.  public static GUIStyle SubTitleBar()
    3.     {
    4.         GUIStyle s = new GUIStyle();
    5.         s = EditorStyles.toolbar;
    6.         s.alignment = TextAnchor.MiddleCenter;
    7.         s.fontStyle = FontStyle.Bold;
    8.         s.fontSize = 10;
    9.         return s;
    10.     }
    11.  
    But when I do that, everything that uses EditorStyle.toolbar uses the new font size. It doesn't seem to copy the style either.
    I could just set it back after I use the new one, but its kind of annoying to have to do that every time.
     
    Last edited: Nov 1, 2017
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    This is a property, not a function. Properties look like variables, but they're backed by code. Very often that code simply gets and sets a private variable, called a backing variable.

    The first line of your code creates a new, blank GUIStyle and assigns it to the variable 's'. The second line assigns the actual EditorStyles.toolbar to 's', which dumps your new, blank GUIStyle. Try this instead:
    Code (csharp):
    1. GUIStyle s = new GUIStyle(EditorStyles.toolbar);
    This creates a new GUIStyle that's a copy of EditorStyles.toolbar.
     
    frankslater and LittleRainGames like this.
  3. LittleRainGames

    LittleRainGames

    Joined:
    Apr 7, 2016
    Posts:
    97
    Ahh thank you so much.