Using the Unity Object Pool

  In the process of game development, we often encounter that after the game is released, it is obvious that there is a stuttering phenomenon during testing. There are two reasons for this phenomenon: one is that the game optimization is not good enough or the game logic itself is designed with problems, and the other is that the mobile phone hardware is not good. Well, as programmers, we can only start from the first reason, then start to look at the performance overhead of Profiler, and then start to do various memory, special effects, and code optimization. For this kind of problem, experienced developers will make a standard design at the beginning. As far as our project is concerned, the design includes character pool, monster pool, special effect pool, experience pool, damage pool.... .. The so-called object pool is to reuse resources already resident in memory as much as possible to reduce frequent IO time-consuming operations. Using the object pool can solve the memory pressure very well, but we need to maintain the state of the objects in the pool ourselves. As far as particle effects are concerned, when the particle effects are released, we need to reset them to the initial state, so as to ensure that the playback of the effects released each time is normal. Well, without further ado, let's start with a simple small example, taking the damage and experience floating words I did in the project as an example. When it comes to this word of damage and experience, it is very common especially in MMO or ARPG games. After entering the automatic battle, the server will frequently tell the client how much blood was spent to fight monsters and how much experience was gained, so the performance of the client should be is very frequent. It is impossible for us to instantiate and destroy the corresponding floating word immediately according to the message received from the server, right? There is another problem in this way. Because the network news is too fast, if the client does not process it, it will be superimposed on one piece. For this processing, I choose to use a queue, classify the messages sent by the server into the queue, open a coroutine to process the information in the queue and control the processing interval, and prefabricate the instantiated floating characters into the experience pool. Use free objects from the pool. Simple experience pool, start coding.

1. Build a simple interface

 

2, we first create an object pool

 

It's easy to create, and then get the object of this pool.

ExpPool = PoolManager.Pools["ExpPool"];

Add to the pool the objects we want to reuse, such as particles, models, audio, , , etc. How to add it?

ExpPool = PoolManager.Pools["ExpPool"];//Get the object pool
if (!ExpPool._perPrefabPoolOptions.Contains(prefabPool))
{
    prefabPool = new PrefabPool(Resources.Load<Transform>("LabExp"));//Load local prefab
    //Initialize a Prefab by default
    prefabPool.preloadAmount = 1;
    //Enable restrictions
    prefabPool.limitInstances = true;
    //Close infinite fetch Prefab
    prefabPool.limitFIFO = false;
    //Limit the maximum number of Prefabs in the pool
    prefabPool.limitAmount = 10;
    //Enable automatic cleaning of the pool
    prefabPool.cullDespawned = true;
    //finally keep
    prefabPool.cullAbove = 10;
    // how often to clean up
    prefabPool.cullDelay = 5;
    // clean up several times at a time
    prefabPool.cullMaxPerPass = 5;
    //Initialize the memory pool
    ExpPool._perPrefabPoolOptions.Add(prefabPool);//Add to the pool
    ExpPool.CreatePrefabPool(ExpPool._perPrefabPoolOptions[ExpPool.Count]);
}
else
{
    Debug.Log("Already in prefabPool!");
}

3. The addition is completed, then it is time to go to the object pool to get the object.

Transform labExp = ExpPool.Spawn("LabExp");

Here LabExp is the name of the object added to the object pool, which is the name of my local experience prefab.

4, the last step object state initialization
IEnumerator ResetPrefab (Transform obj)
{
    yield return new WaitForSeconds(2f);
    obj.GetComponent<TweenPosition>().ResetToBeginning();
    obj.GetComponent<TweenScale>().ResetToBeginning();
    obj.GetComponent<TweenAlpha>().ResetToBeginning();
    ExpPool.Despawn(obj);
}

Restore the used object to its starting state, well, a simple object pool is ready to use!

By the way, there is an important step: remember to add a Panel to the parent node of the object pool, so that it will not affect the redrawing of other interface components.

 
 

PS: If there is an incorrect place in this article, remember @me and learn together!

Project address: https://github.com/wuzhangwuzhang/ExpPoolManager.git

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324718073&siteId=291194637