Hello Guest

Author Topic: UI Grid Creation at Runtime (tk2dUI AddLayout at runtime)  (Read 4546 times)

daklab

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Daklab - Experiments, Projects, Designs, Tips, and Tutorials Related to Creative Coding
UI Grid Creation at Runtime (tk2dUI AddLayout at runtime)
« 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):

Code: [Select]
//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:

Code: [Select]
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?
Braindrop Labs - Games. Apps. Coded Art. www.braindroplabs.com

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: UI Grid Creation at Runtime (tk2dUI AddLayout at runtime)
« Reply #1 on: September 30, 2013, 05:28:18 pm »
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

Dasc

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: UI Grid Creation at Runtime (tk2dUI AddLayout at runtime)
« Reply #2 on: May 11, 2015, 05:40:19 pm »
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
  }
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: UI Grid Creation at Runtime (tk2dUI AddLayout at runtime)
« Reply #3 on: May 14, 2015, 05:31:33 pm »
@Dasc / @daklab -
Please post a repro project in the private support forum.