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 Nothke · Nov 20, 2013 at 09:00 AM · guitoolbarselectiongrid

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

Comment
Add comment · Show 1
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 Professor Snake · Nov 20, 2013 at 09:54 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · 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

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

19 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 avatar image avatar image avatar image

Related Questions

GUI.Button To Change Weapons On Screen 2 Answers

Creating SelectionGrid with each button tinted a different color 2 Answers

Selection grid not working? 1 Answer

How can I change Toolbar/SelectionGrid selected item's text color? 1 Answer

Selectiongrid formatting with guistyle.wordwrap 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