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 that references the Inspector GUIStyle then extracting the elements of the Inspector GUIStyle in to a new GUIStyle
with something like:

btnGUIArray *= 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 = 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+ibtnhgt3,txtwth,btnhgt),ptts + “:”,lblGUIStyle);_

trackparams[i,pos_on] = GUI.Toggle (new Rect (midleftedge+10+txtwth,topedge+25+ibtnhgt3,btnwth,btnhgt),trackparams[i,pos_on],“Profile”,posGUIStyle);

trackparams[i,srf_on] = GUI.Toggle (new Rect (midleftedge+17+txtwth2,topedge+25+ibtnhgt*3,btnwth,btnhgt),trackparams[i,srf_on],“Surface”,srfGUIStyle);

_ if (GUI.Button(new Rect(midleftedge+27+txtwth3,topedge+32+ibtnhgt3,btnwth5+10,btnhgt+4),“Set Color”,btnGUIArray*)) {
selectedTrack = i;

trackparams[i,anim_pos] = GUI.Toggle (new Rect (midleftedge+10+txtwth,topedge+40+ibtnhgt3,btnwth,btnhgt),trackparams[i,anim_pos],“Animate”,apoGUIStyle);

trackparams[i,anim_srf] = GUI.Toggle (new Rect (midleftedge+17+txtwth2,topedge+40+ibtnhgt*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*

* }*

* }*

* }*

* }*

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 *= 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.