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 17, 2013 at 07:44 AM · c#arraycolorguistyle

How do I construct an array of unique GUIStyles from the values in a single GUIStyle when apparently GUIStyles are passed by reference, not value.

I have written C# code to dynamically generate a menu in onGUI() at run time to control the display of a variable number of track lines drawn though my 3D space. It works fine but for one issue. I made a colorpicker to change the color of a track line selected by clicking its associated 'Set Color' button in the menu. When I use the colorpicker I want the label of the button to change color as well as change the color of the track line with the color pick. The color of the selected line changes just like I want it to. The selected button label changes as well, as do ALL the other button labels at the same time.

I have a GUIStyle for the 'Set Color' buttons set up in the Inspector, and I have been trying to create a GUIStyle array with an array element for each 'Set Color' button constructed from the values in the GUIStyle in the Inspector, because I anticipated just this kind of problem while writing the menu code if I used a single GUIStyle for all buttons.

It appears the even building the array elements by selecting the individual components from the Inspector GUIStyle still is a by reference operation so the "values" all refer back to the Inspector GUIStyle. By the nature of my data this has to be a run time operation, so even if I wanted to manually set up 40 some odd GUIStyles in the Inspector, I couldn't do it. And, I really don't want to do that...;^)

I suspect the answer lies in setting up a GUI.skin that references the Inspector GUIStyle then extracting the elements of the Inspector GUIStyle in to a new GUIStyle with something like:

 btnGUIArray[i] = new GUIStyle(someGUISkin.GetStyle("btnGUIStyle"));

But, I haven't quite figured out how the make that work yet, either. So, pointers in either direction would be appreciated. A skeleton of my very near functioning code just attempting to use GUIStyles directly is provided below.

 public class sharkTrackRender : MonoBehaviour {
     
     public GUIStyle btnGUIStyle;     
     private GUIStyle[] btnGUIArray;
     private int selectedTrack;
 
 
     void Start () {
 
           // pttcnt is generated and all the other parameters below are initialized here at run time
 
           btnGUIArray = new GUIStyle[pttcnt];
         
         for (int i=0; i<pttcnt; i++) {            
             btnGUIArray[i] = btnGUIStyle;  // btnGUIStyle is set up in Inspector
           }                                // I've tried individual component access as well
 
         }
 
     void OnGUI () {
 
       if (GUI.RepeatButton(new Rect(rightedge+(bkgwth-barwth)/2,topedge+30,barwth,barhgt),colorbar,lblGUIStyle)) {
             
 
              // do a bunch of scale fiddling to map screen coordinate picks from a stretched
              // colorpicker image back to real size image coordinates for GetPixel
 
 
              Color pixclr = colorbar.GetPixel((int)xshift,(int)yshift); // get color from image
     
          srfclrupdate[selectedTrack] = pixclr;    
 
              btnGUIArray[selectedTrack].normal.textColor = pixclr;
          btnGUIArray[selectedTrack].hover.textColor = pixclr;
              btnGUIArray[selectedTrack].onNormal.textColor = pixclr;
             
          // update all LineRenderer colors for selected track:
             
              for (int j=0; j<reccnts[selectedTrack,srf]-1; j++) {  
            srfclrs[selectedTrack,j,0] = srfclrupdate[selectedTrack];
            srfclrs[selectedTrack,j,1] = srfclrupdate[selectedTrack];
          }        
             
            }
 
        if (menuUp) {
             
          GUI.Box(new Rect(midleftedge,topedge,mnuwth,mnuhgt), "TRACK DISPLAY:",boxGUIStyle);
             
          for (int i=0; i<pttcnt; i++) {    
 
            GUI.Label(new Rect(midleftedge+15,topedge+25+i*btnhgt*3,txtwth,btnhgt),ptts[i] + ":",lblGUIStyle);
     
            trackparams[i,pos_on] = GUI.Toggle (new Rect (midleftedge+10+txtwth,topedge+25+i*btnhgt*3,btnwth,btnhgt),trackparams[i,pos_on],"Profile",posGUIStyle);
     
            trackparams[i,srf_on] = GUI.Toggle (new Rect (midleftedge+17+txtwth*2,topedge+25+i*btnhgt*3,btnwth,btnhgt),trackparams[i,srf_on],"Surface",srfGUIStyle);
 
            if (GUI.Button(new Rect(midleftedge+27+txtwth*3,topedge+32+i*btnhgt*3,btnwth*5+10,btnhgt+4),"Set Color",btnGUIArray[i])) {
                   selectedTrack = i;        
            }
                     
 
            trackparams[i,anim_pos] = GUI.Toggle (new Rect (midleftedge+10+txtwth,topedge+40+i*btnhgt*3,btnwth,btnhgt),trackparams[i,anim_pos],"Animate",apoGUIStyle);
          
            trackparams[i,anim_srf] = GUI.Toggle (new Rect (midleftedge+17+txtwth*2,topedge+40+i*btnhgt*3,btnwth,btnhgt),trackparams[i,anim_srf],"Animate",asfGUIStyle);
         
            if (trackparams[i,anim_pos]) {
               trackparams[i,pos_on] = false; // anim_pos forces static pos off
            }
 
            if (trackparams[i,anim_srf]) {
           trackparams[i,srf_on] = false; // anim_srf forces static srf off
            }
           
          }  
             
        } 
         
     }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by waterdog · Oct 21, 2013 at 10:06 PM

Answering myself once more. I have made this work, but, there are still a couple of issues. Creating a GUISkin in the Project tab create menu, adding a public variable for the skin in the script to make it available in the Inspector, dragging the just created GUISkin into the Inspector GUISkin slot, and filling in a Custom Style array element in the GUISkin in its Inspector section (at the bottom of the Inspector) with the desired button elements allows the GUIStyle to be used to create new, independent GUIStyles even though GUIStyles appear to be passed by reference. The added script code looks like:

 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;
         }
 
     }
 
 }

Note the use of a firstpass bool variable to stop updating the GUIStyle array with each pass through OnGUI. If you don't do something like this then the label color changes made through the colorpicker will be reset to the default GUIStyle color. The main issue now is the requirement to us "" rather than a GUIStyle name in the call to set up the GUIStyle array. As this question is essentially solved, I'll start a new question for that.

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 Benproductions1 · Oct 21, 2013 at 10:07 PM 0
Share

Ins$$anonymous$$d of your "firstPass" idea, you can just do that logic in Start. None of that code has to be in OnGUI :)

avatar image waterdog · Oct 21, 2013 at 10:39 PM 0
Share

I wondered about that, and tried it from Start, but got warnings about not doing it from OnGui. So, I figured better to make it happy at least until the "" issue gets sorted it out ;^)

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

15 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

Related Questions

Change color of all buttons listed in array 3 Answers

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

Creating an array of gui styles and using them 1 Answer

Why does new GUIStyle() give "object reference not set to an instance of an object" error? 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