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 reberk · Apr 26, 2013 at 06:21 PM · javascriptguivariablesguiwindow

Dynamic variable creation for unique GUI.Window generation

I'm wanting each click of a button to generate a new draggable window containing text taken randomly from a string of items. What I can't figure out is how to generate completely new variables to store the necessary window data. Here is what I've got, though the way it's coded right now, each click of the New Item button will only refresh the one window:

 var itemList : String[] =["Item1","Item2","Item3","Item4"];
 var singleItem = "";
 var wIndex = 0;
 var showWindow = false;
 var wRect : Rect = Rect (10,40,90,90);
         
 function OnGUI(){
     if (GUI.Button(Rect(10,10,80,20),"New Item")){
         showWindow = true;
         singleItem = itemList[Random.Range(0,itemList.length -1)];
         wIndex++
     }
     
     if (showWindow){
         wRect = GUI.Window (wIndex,wRect,newWindow,singleItem);
     }
 }    
     
 function newWindow(windowID:int){
         GUI.DragWindow();
 }

Right now it's bound to wRect to define itself and showWindow to keep it displayed. I could just make a number of similar predefined variables so high that the player would never make that many windows, but I figure there's got to be a way to do it dynamically through code.

Is it possible to somehow create a new, uniquely named variable each time the button is clicked? Something like

 var "wRect"+wIndex : Rect = (10,40,90,90);

though clearly that doesn't work syntactically. I'm a little out of my depth; any help would be much appreciated.

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
Best Answer

Answer by $$anonymous$$ · Oct 10, 2013 at 01:39 AM

You can use dynamic Lists of Rects and Strings, and dynamically add items to the lists every time you click the "New Item" button.

Then, iterate over the List inside the if (showWindow) block, calling GUI.Window for each Rect and String in each list.

This should get you started, good luck!

 import System.Collections.Generic;
 ...
 var wRects : List.<Rect> = new List.<Rect>();
 var wItems : List.<String> = new List.<String>();
 ...
 function OnGUI() {
     if (GUI.Button(Rect(...
         ...
         wRects.Add(new Rect(10 * wIndex, 40, 90, 90);
         wItems.Add(singleItem);
     }
 
     if (showWindow) {
         for (int i = 0; i < wIndex; i++) {
             GUI.Window(i, wRects[i], newWindow, wItems[i]);
         }
     }
 }
 ...
Comment
Add comment · Show 1 · 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 reberk · Oct 10, 2013 at 04:23 AM 0
Share

Hey, thanks! It's been a while, but this is a project I'm actually going to return to shortly. Again, I'll play around with what you've provided and do some reading. $$anonymous$$uch appreciated.

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

12 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

Related Questions

Why does it give me an error in this small java script for sprint? 1 Answer

On Clicked, On Released GUI Button ? 2 Answers

How to access a non static variable by another static variable? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Game Over GUI Question 2 Answers


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