Search Unity

Object Pooling

Discussion in 'Scripting' started by Zakkary19, Sep 15, 2019.

  1. Zakkary19

    Zakkary19

    Joined:
    Jan 4, 2016
    Posts:
    5
    Hi there! I am really quite stuck on how to properly do object pooling for my 3D endless runner game. I have this TileManager script that works perfectly but it causes lag on mobile devices because it has to constantly instantiate and destroy objects. I am just getting really confused on how to change the script to use object pooling.

    I understand I need to instantiate the objects right off the start then store them in a pool. Then from that pool, I would need to move the objects position. I do not know how to go about all of this. If anyone could help by showing me what I need to do to the script that would be greatly appreciated! Thank you!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TileManager : MonoBehaviour {
    6.  
    7.     //Tile Variables
    8.     public GameObject[] tilePrefabs;
    9.  
    10.     public static Transform playerTransform;
    11.     private float spawnZ = -13.0f;
    12.     private float tileLength = 13.0f;
    13.     private float safeZone = 12.0f;
    14.     private int amnTilesOnScreen = 4;
    15.     private int lastPrefabIndex = 0;
    16.  
    17.     public List<GameObject> activeTiles;
    18.  
    19.     // Use this for initialization
    20.     private void Start () {
    21.         activeTiles = new List<GameObject>();
    22.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    23.  
    24.  
    25.         for (int i = 0; i < amnTilesOnScreen; i++)
    26.         {
    27.             if (i < 2)
    28.                 SpawnTile(0);
    29.             else
    30.                 SpawnTile();
    31.         }
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     private void Update () {
    36.         if (playerTransform.position.z - safeZone > (spawnZ - amnTilesOnScreen * tileLength))
    37.         {
    38.             SpawnTile();
    39.             DeleteTile();
    40.         }
    41.     }
    42.  
    43.     private void SpawnTile(int prefabIndex = -1)
    44.     {
    45.         GameObject go;
    46.         if (prefabIndex == -1)
    47.             go = Instantiate(tilePrefabs[RandomTilePrefabIndex()]) as GameObject;
    48.         else
    49.             go = Instantiate(tilePrefabs[prefabIndex])as GameObject;
    50.  
    51.         go.transform.SetParent(transform);
    52.         go.transform.position = Vector3.forward * spawnZ;
    53.         spawnZ += tileLength;
    54.         activeTiles.Add(go);
    55.     }
    56.  
    57.     private void DeleteTile()
    58.     {
    59.         Destroy(activeTiles[0]);
    60.         activeTiles.RemoveAt(0);
    61.     }
    62.  
    63.     private int RandomTilePrefabIndex()
    64.     {
    65.         if (tilePrefabs.Length <= 1)
    66.             return 0;
    67.  
    68.         int randomIndex = lastPrefabIndex;
    69.         while (randomIndex == lastPrefabIndex)
    70.         {
    71.             randomIndex = Random.Range(0, tilePrefabs.Length);
    72.         }
    73.  
    74.         lastPrefabIndex = randomIndex;
    75.         return randomIndex;
    76.     }
    77.  
    78.    
    79. }
     
  2. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    The point of object pooling is basically to never need to use instantiate ( after start of game or level ) or destroy.
    How many tiles are maximum seen on the screen ever? Make activeTiles an array of that size ( arrays are slightly faster than Lists due to fixed data size ) Spawn your tiles at start and fill the activeTiles with those. Then just set activeTiles[youChoose].transform.position and activeTiles[youChoose].setActive() etc.

    If you want faster loading times it might be helpful to have all those tile GameObjects in your first scene already linked to activeTiles in the editor then move them with the pooling scripts through all the scenes in a singleton.

    EDIT:
    You'll need an activeTiles for every different prefab you got after reading you're doing an endless runner with probably lots of different tilePrefabs?
     
    Last edited: Sep 15, 2019
  3. Zakkary19

    Zakkary19

    Joined:
    Jan 4, 2016
    Posts:
    5
    I will do some trial and error and see if I can get this to work. Thank you for guiding me in the right direction!