2D Toolkit Forum
2D Toolkit => Support => Topic started by: daklab on September 30, 2013, 03:47:49 pm
-
I'm trying to create a grid using BasicButton with a tk2dUILayout attached. I also have a containerUI GameObject in my scene that has a tk2dUILayoutContainerSizer script attached. When iterating through a for loop I can instantiate my BasicButton prefabs as expected and add them to the tk2dUILayoutContainerSizer instance and all works as expected (vertical only):
//layout add
myLayout = Instantiate(prefabItem) as tk2dUILayout;
foreach (tk2dUILayoutItem item in myLayout.layoutItems)
layoutSizer.AddLayout(myLayout, item);
What I'd like to do is create a ContainerRow prefab that has its own tk2dUILayoutContainerSizer (horizontal) and instantiate a few of these and add each row to my main containerUI GameObject (which has its own tk2dUILayoutContainerSizer) that is set to vertical. This would create a grid.
I've tried this:
for(int i = 0; i < 5; i++)
{
myRowLayout = Instantiate(prefabRowToInstantiate) as GameObject;
layoutRowSizer = myRowLayout.GetComponent<tk2dUILayoutContainerSizer>() as tk2dUILayoutContainerSizer;
for(int j = 0; j < 4; j++)
{
//layout add
myLayout = Instantiate(prefabItem) as tk2dUILayout;
foreach (tk2dUILayoutItem item in myLayout.layoutItems)
layoutRowSizer.AddLayout(myLayout, item);
ctr++;
}
//TODO add each row to my main layoutSizer
}
But where my TODO comment is I haven't been able to successfully add my instantiated Row to the main layoutSizer. How do I accomplish this?
Or for that matter how can you dynamically insert nested tk2dUI layout prefabs at runtime and still have the layout container properly arrange them?
-
Hi,
This should work, though we've not tested it this way, instantiated in code, before. If you have a simple repro case for this, it would greatly reduce how long it'd take us to reproduce and attempt to fix it - I might as well ask first before spending the time to create a test case. If you do have one, please email to support at unikronsoftware.com
-
Have a similar issue with tk2dUILayoutContainerSizer, comes out with stack overflow!
What I've done:-
// Step 1 - Create blank scene
// Step 2 - Drag in "BasicButton" (with layout)
// Step 3 - Create empty game object and add tk2dUILayoutContainerSizer
// Step 4 - create a script to instantiate multiple "BasicButton" from the scene, then adding it to the tk2dUILayoutContainerSizer
// Script
public tk2dUILayout prefabItem;
void Start(){
tk2dUILayout myLayout = Instantiate<tk2dUILayout>(prefabItem);
tk2dUILayoutContainerSizer sizer = this.GetComponent<tk2dUILayoutContainerSizer>();
foreach (tk2dUILayoutItem item in myLayout.layoutItems) {
sizer.AddLayout(myLayout, item); <---- produces stack overflow
}
}
-
@Dasc / @daklab -
Please post a repro project in the private support forum.