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 ezlockwo · Apr 14, 2013 at 01:27 AM · listvariablewindowgui.boxenumerate

Enumerating GUI.Box within a window

So what I'm trying to do is create a variable number of GUI.Box with some simple data in them and have them appear within a window. Currently the way I'm accomplishing this is by creating an ArrayList of an object with the code to make the GUI.Box in the OnGUI function of the created class. The problem with this is that I can't hide those boxes within the window or prevent the boxes from extending down beyond the bounds of the window.

I've also tried a foreach loop, but the problem there is that it only shows one box at a time when called from the window.

If anyone knows how to accomplish enumerating Boxes within a window, I would greatly appreciate the help. I apologize if my description of what I'm trying to achieve is confusing, I'm a little stuck on how to describe it as well.

Old Code:

 public class FPSMenuScript : MonoBehaviour {
     private string name;
     public ArrayList actors;
     private Rect sideBarWindow;
     
     void Start () {
         actors = new ArrayList();
         i = 0;
         sideBarWindow = new Rect((Screen.width - 160f),10f,150f, Screen.height - 170f);
     }
     void OnGUI()
     {
         GUI.Window (0,sideBarWindow,ActorWindow,"Actor Window");
     }
     
     void ActorWindow(int windowID) {
         if(GUI.Button (new Rect(5f,5f,100f,30f),"Add Actor")){
             actors.Add();
         }
     }
 }
 
 public class ActorGUI : MonoBehaviour {
     public Rect actorRect;
     public int actorNumber;
     public string actorName;
     
     void Start () {
         actorRect = new Rect(Screen.width - 150f, 50f + 55f*actorNumber, 140f, 50f);
     }
     void OnGUI () {
         GUI.depth = 0;
         GUI.Box (actorRect, actorName);
     }
 }
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
1
Best Answer

Answer by Bunny83 · Apr 14, 2013 at 01:42 AM

Uhm, any content drawn inside the window is clipped to the window bounds. It seems you're doing something really strange here. It would be way easier to pin down your problem if you had provided some code.

Anyways, for dynamic content generation i would recommend to use GUILayout instead of GUI. Instead of an ArrayList, which is untyped and requires boxing, i would use a generic List.

Since we don't know what language you use i will post my example in my preferred language C#:

 //C#
 Rect windowPos = new Rect(10,10,300,300);
 List< Item > items;
 
 void OnGUI()
 {
     windowPos = GUI.Window(0, windowPos, DrawWindow, "MyTitle");
 }
 
 void DrawWindow(int id)
 {
     GUILayout.BeginHorizontal();
     foreach(var item in items)
         item.Draw();
     GUILayout.EndHorizontal();
 }
 
 // Item class
 public class Item
 {
     public string someText;
     public void Draw()
     {
         GUILayout.BeginVertical("box");
         GUILayout.Label(someText);
         GUILayout.EndVertical();
     }
 }
Comment
Add comment · Show 2 · 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 Bunny83 · Apr 14, 2013 at 01:43 AM 0
Share

ps. i wrote that from scratch, so it's not tested and only a code snippet. The filling of the items List is up to you. ;)

pps: The generic List is in the System.Collections.Generic namespace so you need a "using" at the top.

avatar image ezlockwo · Apr 14, 2013 at 06:50 AM 0
Share

Sorry about the lack of code, I felt it was probably easier to explain what I was trying to accomplish, since I was fairly sure I was on the wrong track on how to do this. This code works though (well the parts I knew I needed anyway), the only change needed is:

 foreach(Item _item in items)
 {
 item.Draw();
 }

other than that, it functions well. Thanks for putting in C#, that's what my entire project is in.

I feel like I was actually pretty close, though I haven't been using GUILayout, I'll edit my original post to give you some idea what I was trying to do previously.

But anyway, thank you so much for your help, I'd been working on this problem for about a week now.

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

A node in a childnode? 1 Answer

class List adding to variables are assigning not adding 1 Answer

Enumerations Varibles 1 Answer

List array in GUI.Box 0 Answers

Making a list selection variable 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