- Home /
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);
}
}
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();
}
}
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.
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
Follow this Question
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