Optimization Technology --- Object pool optimization project, --- Object pool project
Another year! Today I am going to explain to you a very common optimization technology-using the object pool to optimize the project.
I don't need to talk much about it. This article can be used together with the memory optimization mentioned in my previous blog!
First, let's explain what the object pool is. We all know that all game objects in unity can be collectively called objects. As the name suggests, an object pool is a container that can store many objects!
In our actual development project, we may encounter such a problem. We are constantly creating new game objects, and then we will use Destroy () when we do not use this object () method to destroy this useless object. Many new users will adopt this approach! If you want an object to be instantiated or new, destroy it when you don't want it. We don't know this, it will eat our memory! Seriously, it will affect the quality of our products.
So what should we do with the repeated use of resources? Let's think about it. If we don't delete these objects, but set their gameobject attribute to false, will these objects be invisible? Set the attribute to true when necessary, and set their location and other attributes to solve the problem! Because of this, the object pool optimization technology helps us solve this problem.
The following uses the use and destruction of bullets in the first-person role shooting to demonstrate the use of Object pool technology!
First, create an object pool class:
Using UnityEngine; using System. collections; using System. collections. generic; public class pool: MonoBehaviour {// declare a singleton object public static pool instance; // create a Dictionary dic public static Dictionary
Dic = new Dictionary
(); // Use this for initialization void Start () {// The initialization instance of a single instance = this ;} // provide a public static GameObject Get (string key, Vector3 position, Quaternion rotation) method for obtaining objects {// create the returned object GameObject go; // define the key name of dic, because all the gameobjects generated by instantiate are automatically named as gameobject (Clone). Here, we use the following return method to name the key to match string GameObjectName = key + "(Clone )"; // If the dictionary contains the key "gameobjectname" and the array corresponding to the key is not empty (with this type of bullets, and there are <already created> (not activated) Bullet gameobject) if (dic. containsKey (GameObjectName) & dic [GameObjectName]. count> 0) {// retrieve the array ArrayList = dic [gameobjectname] from the key location of GameObjectName; // retrieve the first-digit bullet // forcibly convert the object type to the gameobject type; go = (GameObject) list [0]; // remove this bullet from the List (used out) // remove the element from the list so that the element can be retrieved again next time. List. removeAt (0); // activate the gameobject attribute of the bullet go. setActive (true); // activated when used // set the location go. transform. position = position; // set the rotation go. transform. rotation = rotation;} // if it is not the first time (there is no object in the pool, then we Instantiate the object) else {go = Instantiate (Resources. load (key), position, rotation) as GameObject;} // return the created object return go;} // function: unactivates the object to be deactivated. Public static GameObject Return (GameObject g) {string key = g. name; // if the dictionary contains this key if (dic. containsKey (key) {dic [key]. add (g) ;}// if this key else does not exist {// create an arraylist for this key and Add g to dic [key] = new ArrayList () {g };}// Cancel activation g instead of destruction. setActive (false); // return g ;}}
Using System. collections; using System. collections. generic; using UnityEngine; // This script is mounted to any geamobject public class CreatBullet: MonoBehaviour {// drag the Bullet preset public GameObject Bullet; // drag it to the starting position of the Bullet, place an empty object and drag it into the public Transform startPlace; private float speed = 500; void Update () {// obtain the Space key Input (emission key) if (Input. getKeyDown (KeyCode. mouse0) {// call the get method of the pool to create a bullet GameObject bullet = pool. get (Bullet. name, star TPlace. position, Quaternion. identity); // Add a force (emission) to the bullet's rigid body // Rigidbody. AddForce: // AddForce applies only to the activated rigid body. If the game object is not activated, adding force will not work. // ForceMode. Impulse: This method uses instantaneous force. Bullet. GetComponent
(). AddForce (Vector3.forward * speed, ForceMode. impulse) ;}} using System. collections; using System. collections. generic; using UnityEngine; public class DestroyBullet: MonoBehaviour {// This script is mounted to the bullet, so that the bullet is automatically destroyed n seconds after activation (enters the inactive state) // create a collaborative latency operation destroys (); void Update () {// keep checking in update; // execute the delayed operation StartCoroutine (destroys ());} IEnumerator destroys () {// yield return new WaitForSeconds (3f); // call the Cancel activation method to cancel the activation of the pool. return (this. transform. gameObject );}}
Well, the above is the compilation of the Object pool code. Of course, this writing method is just one. You can not only write the data structure like dictionary, but also use the list set to write the object pool. There are many other ways to write. I personally think this writing method is relatively simple and easy to understand. The dictionary is more convenient to use. What's wrong with writing!