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 Raymond 2 · Jan 16, 2012 at 03:31 PM · buttonarraylistfrom

Button list from array

I want to create a list of buttons from an array, with for each button a texture, but how can i place buttons, with each a specific position and it's own texture and values from an array. The whole idea behind this is to create some kind of app drawer for windows, so each button has to open a specific program on the computer. If anyone has an idea about this. please let me know

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kroltan · Jan 16, 2012 at 03:53 PM

If you're generating buttons in a pattern, you can use for loops. Example:

function OnGUI(){
    for (var 1 : int = 0; i <= yourArray.length; i++){
        yourArray[i] = GUI.Button(Rect(10*i,0,10,10),image[i]);
        if (yourAray[i]){
            //Your code here
        }
    }
}

This will mass create and check buttons in each array item. It's important to say that yourArray will hold the buttons (boolean value), not their content. Just put your code in the defined spade and you're done. P.S.: This example will create a row of buttons. It's recommendable that you use a grid generator intead if you plan to display multiple rows (that's very easy to find).

image is the array of icons for your buttons. You can also use text if youd efine as a String array.

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

Answer by SkaredCreations · Jan 16, 2012 at 03:47 PM

Just create a struct with two public members ("Rect buttonRect" and "Texture buttonTexture"), finally declare the list object as array of your struct

Comment
Add comment · Show 19 · 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 Raymond 2 · Jan 16, 2012 at 03:52 PM 0
Share

but how can it place the buttons then? :S still a noob in GUI scripting:P

avatar image Raymond 2 · Jan 16, 2012 at 03:52 PM 0
Share

sorry i put it as answer, always forget that i can comment

avatar image Raymond 2 · Jan 16, 2012 at 03:54 PM 0
Share

This is my current script, how could i edit this so that the buttons link to the different applications, then i would be happy already if i know that.

import System.Diagnostics;

var stringPath = "../";

//All icon textures

var btnTexturePS : Texture;

//All application links

var appLink : String[];

var selGridInt : int = 0;

var selStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];

function OnGUI () {

 selGridInt = GUI.SelectionGrid (Rect (25, 25, 200, 50), selGridInt, selStrings, 2);

}

avatar image Kroltan · Jan 16, 2012 at 04:02 PM 0
Share

Excuse me, but are you using C# or JS? You imported something, that's from C#, but the variable definitons look like JS.

avatar image Raymond 2 · Jan 16, 2012 at 04:05 PM 0
Share

i write in JS, but the cde works fine so it seems it's JS, and as far as i see, it's written as JS

Show more comments
avatar image
0

Answer by Larry-Dietz · Jan 16, 2012 at 08:19 PM

I just gave someone else an example of creating a grid of buttons from a multidimentional array on this question... Infect the blank cubes

Take a look at it. It might me of some help to you as well.

-Larry

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

Answer by frogsbo · Jun 09, 2014 at 07:35 AM

Problem with that, is it uses 1 draw call per button, very slow, for like 10x10 buttons is 100 draw calls. here is a button using whatever texture you want, it returns number of button clicked on your texture:

   function OnGUI(){
             var gridpixels = 20;//pixels per grid square
             var gidxsquares = 12;//num squares in x direction
             if (GUI.Button(Rect(320,10,240,60),fctbutton,GUIStyle.none))
     {
             var xpos = Input.mousePosition.x - 320 ;
             var ypos = Screen.height - Input.mousePosition.y -10;
             
             var result =  Mathf.Floor(xpos / gridpixels) + Mathf.Floor(ypos / gridpixels)*gidxsquares + 1;//plus 1 at end for not zero first square
             //
             print (result);
     }    

}

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

8 People are following this question.

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

Related Questions

Looping through Button Array and checking for OnClick 0 Answers

Create a button from an int, remove button when clicked, and never load it again 1 Answer

A node in a childnode? 1 Answer

How to make a or array of GUI.Button's? 1 Answer

Button showing when array is full 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