Rendering of 2D Map very slow, what am I doing wrong?
I have a map controller script (c#), Generation of the map in setup():
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
GameObject go = (GameObject)Instantiate (prefab);
go.transform.position = new Vector2 (j, i);
go.isStatic = true;
go.SetActive (false);
gos.Add (go);
}
}
Calculating if I should activate a tile or not in update():
foreach (GameObject go in gos) {
if (go.transform.position.x > left-treshold && go.transform.position.x < right+treshold && go.transform.position.y < top+treshold && go.transform.position.y > bottom-treshold) {
//Debug.Log ("I am visible!");
if(!go.activeSelf){go.SetActive (true);}
} else {
if(go.activeSelf){go.SetActive (false);}
//Debug.Log ("I am NOT visible!");
}
}
With a width and height of 200, I only get ~30fps. What can I do to optimize my program? I'm very new to c# and I'd like to learn as much as possible. Thanks :)
Answer by imaginaryhuman · Jan 27, 2017 at 12:40 AM
@theeisbaer You created 40,000 game objects which probably will be a really huge amount of time taken for 'culling' and figuring out what's visible etc. Every one of those transforms (for those that are active) have to be calculated in terms of a rotation matrix. There's a lot of calculations going on there. I imagine Unity is trying to optimize what it displays based on which tiles are visible so when you make changes it has to do a lot of recalculations?
Your answer
Follow this Question
Related Questions
2D Optimization -> 3D CUBE flattened by ortographic camera or stretched pixel? 0 Answers
Is there any way to prevent Polygon collider auto generation in Unity 5.3? 0 Answers
Grid based movement, more tiles per round 0 Answers
How to stop an object from following its parent? 3 Answers
How to create two invisible joysticks 0 Answers