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

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