Question by
awplays49 · Sep 25, 2015 at 05:46 PM ·
instantiateplaneplanes
Have to instantiate a 9x9 of planes and its slow. What can I do to fix this?
I have a good desktop that can handle a lot, but it takes about 3 seconds to load 81 planes. How can I fix this?
Comment
This is missing a lot of information. You say "instantiate" in the title, but "load" in the problem description. Those are substantially different concepts. Include some code and narrow down the problem for us.
@Dave Carlile this is executed by a simple custom editor.
using UnityEngine;
using System.Collections;
public class $$anonymous$$apGenerator : $$anonymous$$onoBehaviour {
public GameObject tile;
public GameObject floor;
public int mapSizeX, mapSizeY;
[Range (0, 100)]
public int tileOutlinePercentage;
public void Generate$$anonymous$$ap () {
if (transform.FindChild ("$$anonymous$$ap"))
{
DestroyImmediate (transform.FindChild ("$$anonymous$$ap").gameObject);
}
GameObject map = new GameObject ("$$anonymous$$ap");
map.transform.parent = transform;
GameObject mainTile = Instantiate (floor, Vector3.zero, Quaternion.Euler (90, 0, 0)) as GameObject;
mainTile.transform.localScale = new Vector3 (mapSizeX, mapSizeY, 0);
mainTile.transform.parent = map.transform;
for (int x = 0; x < mapSizeX; x ++)
{
for (int y = 0; y < mapSizeY; y ++)
{
float mapHalfX = mapSizeX;
mapHalfX /= 2;
float mapHalfY = mapSizeY;
mapHalfY /= 2;
Vector3 tilePos = new Vector3 (x - mapHalfX + 0.5f, 0.01f, y - mapHalfY + 0.5f);
GameObject newTile = Instantiate (tile, tilePos, Quaternion.Euler (90, 0, 0)) as GameObject;
newTile.transform.localScale -= Vector3.one * tileOutlinePercentage / 100;
newTile.transform.parent = map.transform;
}
}
}
}
Answer by GimLee · Sep 25, 2015 at 07:11 PM
Instantiating and destroying comes with relatively high cost. It is better to instantiate your Map in the Awake() function, and disabling all their renderers, so they are invisible.
When you need them, you can do a foreach to enable the renderers again.