- Home /
Add a button to toolbar/selection grid?
I am trying to make a "tab" view similar to the one in the internet browsers where you have a tab line and a "+" button to make a new button (and well, "-" to remove it), so I am trying to make a GUI.toolbar and add buttons.. But I am just confused how to do it..
toolbar and selection grids require Arrays, but it is hard to add elements to array, no? (at least I couldn't find how to do it in C#), and when I found it they said "use lists!" but toolbar/selection grid doesn't support lists.. So, how can I do it??
Also if you know a simpler way to do a tabbed menu (or an example of it) than this, it would be helpful
I use these functions to add/remove items from small arrays. Change Vector2 to whatever type your variables are.
function IncreaseArray(shoe:Vector2[]):Vector2[]{
if(shoe.length==0){
return new Vector2[1];
}
var shoeRes:Vector2[]=new Vector2[shoe.length+1];
for(var i:int=0;i<shoe.length;i++)
shoeRes[i]=shoe[i];
return shoeRes;
} //use with myArray=IncreaseArray(myArray);
function RemoveFromArray(vec:Vector2[],shoe:Vector2):Vector2[]{
var faf:Vector2[]=new Vector2[0];
for(var i:int=0;i<vec.length;i++){
if(vec[i]!=shoe){
faf=IncreaseArray(faf);
faf[faf.length-1]=vec[i];
}
}
return faf;
}// Use with myArray=RemoveFromArray(myArray,myValue);
Answer by TinkerinThinker · Dec 13, 2013 at 08:49 PM
You can simply use lists and call their ToArray()
method upon passing it to GUI.Toolbar
.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TabbedGUI : MonoBehaviour {
private int currentTab = 0;
private List<string> tabs = new List<string>();
public void AddTab(string title) {
tabs.Add(title);
}
public void RemoveTab(string title) {
tabs.Remove(title);
}
void OnGUI() {
// Notice the List<string>.ToArray() call.
currentTab = GUI.Toolbar(new Rect(0, 0, 50, Screen.width), currentTab, tabs.ToArray());
}
}
Keep in mind that this code doesn't check for duplicate tab titles. In that case the RemoveTab
method may remove the wrong tab from the list!