- Home /
Instantiate large amount of objects in edit mode
Hi, want to generate 100*100 objects in my scene, in the editor.
I have my code to generate them at runtime, but its done using coroutine, and coroutine won't work in edit mode.
//So I have something like that for runtime
IEnumerator Generate()
{
for(int x = 0 ; x < 100 ; x ++)
{
for(int y = 0 ; y < 100 ; y ++)
{
GameObject o = new GameObject("object");
o.transform.position = new Vector3(x,y,0);
}
yield return null;
}
}
Did someone already face the same problem and have a solution ? How can I generate my objects not in one big frame that'll freeze my unity ?
Thanks
Answer by Perfecter · Oct 19, 2014 at 02:38 PM
hmmm, just make it function, not coroutine. For example :
public class Generator : MonoBehaviour
{
public void Generate()
{
for(int x = 0 ; x < 100 ; x ++)
{
for(int y = 0 ; y < 100 ; y ++)
{
GameObject o = new GameObject("object");
o.transform.position = new Vector3(x,y,0);
}
}
}
}
[CustomEditor(typeof(Generator ))]
public class GeneratorEditor: Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("CREATE OBJECTS"))
{
((Generator)target).Generate();
}
}
}
Well my problem with this solution is that it'll be generated in one frame, so unity will freeze for a long time, and sometimes crash. I use coroutine to do that at runtime because it spread the generation on multiple frames.
sry, didnt read your message entirely. Think about some tile system (like tk2dUITile$$anonymous$$ap).