Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by waterdog · Oct 21, 2013 at 11:23 PM · c#arrayinspectorguistyleguiskin

Why can't I access the Name field in the Custom Styles section of a GUISkin in the Inspector?

In working out an answer to my last question regarding creating an array of unique GUIStyles from a single GUIStyle I came up with the following code excerpt:

 public class sharkTrackRender : MonoBehaviour {
      
     public GUISkin buttonGUISkin;
      
     bool firstpass = true;
      
     void OnGUI () {
      
         if (firstpass) {
           for (int i=0; i<pttcnt; i++) {
             btnGUIArray[i] = new GUIStyle(buttonGUISkin.GetStyle(""));
           }
           firstpass = false;
         }
 
     }
 
 }

This basically does what I want. By setting up a GUISkin from the Project tab Create menu and dropping it in the public GUISkin variable slot in the script's Inspector I can set up a GUIStyle in the Custom Styles array section of the GUISkin Inspector and use that GUISkin/GUIStyle combination to set up dynamically sized arrays of unique GUIStyles. The problem is in the Inspector I cannot access the Name field of the Custom Styles array elements. The array elements have no default name, and there seems to be no way to give them a name. Clicking on the Name field does not open a text entry box. I found that using the empty string "" in the GetStyle call will access the first element in the Custom Styles array, so, for the moment I can continue coding, but, there appears to be no way to add and use another uniquely named style in the GUISkin Custom Styles array; which at some point I will probably need to do. Adding multiple GUISkins is not an option any more than would be adding multiple GUIStyles, (see my last question). Any ideas on what I'm missing would be greatly appreciated.

BTW...I'd still be happy hear from anyone on my sequential quaternion question...;^)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nicloay · Oct 22, 2013 at 08:21 AM

The problem is that you create your own instance of GUIStyle and it dosn't linked to any skins, Have a look at this code which should solve your problem.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class CustomStyles : MonoBehaviour {
     public GUISkin skin;
     
     void Start(){   // this method just for test, don't use it here, all operations with skins should be in OnGUI method
         GUIStyle buttonStyle = skin.FindStyle("button");
         buttonStyle.border = new RectOffset(20,20,20,20);//BE CAREFULL this is linked to your style and all changes is permanent
         GUIStyle customButton = new GUIStyle(buttonStyle); // now we have new instance;/
         
         GUIStyle[] customStyles=skin.customStyles;
         Array.Resize<GUIStyle>(ref customStyles,skin.customStyles.Length+1); // add one more element to the array
         skin.customStyles[skin.customStyles.Length-1]=customButton;  // see comment from Bunny83. to rebuild cache, we have to prepare array at first and then assign it back to skin
         skin.customStyles = customStyles; // assign array to the skin
     }
 }
 
 
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image waterdog · Oct 25, 2013 at 05:19 AM 0
Share

Thanks! I'll give it a look shortly.

avatar image waterdog · Oct 26, 2013 at 08:08 AM 0
Share

This sort of works. It appears to add an element since the element count changes to 2 in the inspector, but, the added element does not show up in the inspector so other than completely scripting, it isn't available.

avatar image Bunny83 · Oct 26, 2013 at 09:13 AM 0
Share

You should swap and change those two lines:

     skin.customStyles = customStyles; 
     skin.customStyles[skin.customStyles.Length-1] = customButton;

to

     customStyles[customStyles.Length-1] = customButton;
     skin.customStyles = customStyles; 

And just for those it's not obvious, you have to do this in OnGUI and not in Start. Just like the OP did.

ps: keep in $$anonymous$$d that any changes made to the GUISkin while testing in the editor will permanently change the GUISkin asset. In a built game those changes would not be saved between starts.

avatar image nicloay · Oct 26, 2013 at 09:22 AM 0
Share

I agree with you about Start. I made that just to test. Changes in skin in play mode is permanent. The same as for example if you change texture2d or mesh. In play mode And it doesn't matter when you add element to array.

avatar image Bunny83 · Oct 26, 2013 at 09:39 AM 1
Share

Yes, you're right. Actually i was pretty sure that skin.customStyles returns a temp-array like most other Unity stuff does (ParticleEmitter.particles / $$anonymous$$esh.vertices / $$anonymous$$esh.triangles / Renderer.materials / ...) but it seems to directly reference the array.

However when you do it this way it can't find the style because the internal function BuildStyleCache is only executed when you assign the array back to the customStyles property. Since the new style doesn't have a name yet (or at least not the proper name) FindStyle won't find that style.

Show more comments
avatar image
0

Answer by Bunny83 · Oct 26, 2013 at 09:55 AM

I would recommend an extension method like this to create the procedural styles:

 public static GUIStyle GetStyleOrCreate(this GUISkin aSkin, string aStyleName, GUIStyle aSource)
 {
     var S = aSkin.FindStyle(aStyleName);
     if (S == null)
     {
         S = new GUIStyle(aSource);
         S.name = aStyleName;
         GUIStyle[] customStyles = aSkin.customStyles;
         Array.Resize<GUIStyle>(ref customStyles, customStyles.Length+1); 
         customStyles[customStyles.Length-1] = S;
         aSkin.customStyles = customStyles;
     }
     return S;
 }

With this you can simply do this:

 void OnGUI()
 {
     if (first)
     {
         GUI.skin = skin;
         var S = skin.GetStyleOrCreate("MyNewStyle", "Button");
         S.border = ...

If you'll add a lot of new styles you should use a List instead and just convert the final List to an array and assign it to skin.customStyles as last step.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image waterdog · Oct 26, 2013 at 11:32 PM 0
Share

This sounds promising, but, whether I try and set up the extension method as a separate file, or as a class within the main code, I get "The name "GetStyleOrCreate" does not exist in the current context." So, there is something not clear to me about making the extension class recognizable by the main code.

avatar image Bunny83 · Oct 31, 2013 at 01:10 PM 0
Share

Extension methods have to be declared in a static class. The name of the class doesn't matter. The method of course need to be public and static as well as the class that contains it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does new GUIStyle() give "object reference not set to an instance of an object" error? 1 Answer

Array not updating in inspector 0 Answers

Multidimentional Array for Inventory style system in inspector. 1 Answer

Development of the shop? 1 Answer

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges