Performance: Prefabs and reusable objects
This concept affects the initial load time and runtime performance by managing how game objects are instantiated and reused in the scene.
public GameObject enemyPrefab; for (int i = 0; i < 100; i++) { GameObject obj = Instantiate(enemyPrefab); // Modify instance properties if needed }
for (int i = 0; i < 100; i++) { GameObject obj = new GameObject("Enemy"); obj.AddComponent<EnemyBehavior>(); // Set properties manually }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual new GameObject creation | High (100+ new objects) | N/A | High CPU and memory usage | [X] Bad |
| Instantiate from prefab | Low (reuses template) | N/A | Lower CPU and memory usage | [OK] Good |
public GameObject prefab;
void Start() {
GameObject obj = Instantiate(prefab);
Debug.Log(obj.name);
}public GameObject prefab;
void Start() {
GameObject obj = Instantiate(prefab, new Vector3(0, 0, 0));
}Pass the test and this pattern moves unknown