Unity Game FrameWork - Module Usage - Object Pool Analysis

Official description: Provides the function of object cache pool, avoids frequent creation and destruction of various game objects, and improves game performance. In addition to the Game Framework itself using the object pool, users can also easily create and manage their own object pools.
The figure below is the object pool used in the Demo. All entities and UIs use the object pool.
[picture]

There is already an Entity object pool in Domo, which can meet the needs of storing GameObjects. Here, it can only meet our entity usage needs. When we need to add a new object pool instead of relying on entities, because we don’t understand the principle, we can’t do it start. Let's analyze the principles and ideas of the framework through the entity object pool.

Entity Object Pool Reference Pool Contact

As shown in the figure, two scripts, Entity and Asteroid, are hung on the entity object. These two scripts do not inherit the object base class ObjectBase, and ObjectBase must be inherited to implement the entity object pool. Next, let's sort out the relevant logic of the entity and clarify the branches.
[picture]

The entity information class EntityInfo in the entity manager inherits IReference
[picture]

The creation of the entity information class utilizes the reference pool, but does not use the object pool
[picture]

The m_EntityInfos dictionary stores the entity ID and entity information.
The InternalShowEntity method fills the entity number and entity information into the m_EntityInfos dictionary. That is, new entity information will be filled in each time the entity is displayed. And each time before the entity information is filled in, the serial number m_Serial of the entity component corresponds to +1.
[picture]

[picture]

Entity object (pool) inheritance relationship

1. EntityInfo: The IReference
entity information class inherits from IReference and is a private sealed class of the entity manager EntityManager. The entity information class is created and released by the reference pool to assist in the management of entities.
Internal attributes of the entity information class:
m_Status: Entity status is used to prevent state switching errors. For example, if we call the hidden entity A, but add subentity B to entity A in another place, an error will be reported in the program to avoid Uncontrollable phenomena occur. When we call the hidden entity twice in a row, if the first time has been hidden, the second time will no longer take effect. Equivalent to a state lock, avoid calling it again when it is in a transitional state.
m_Entity, m_ParentEntity, m_ChildEntities: store references to entities, and references to lists of parent entities and child entities.
Entity information class usage:
m_EntityInfos entity information storage dictionary
The dictionary stores the number of the entity and the entity information class, and the number is obtained from the data table of the entity. The entity manager EntityManager displays the entity, fills the entity information into m_EntityInfos, and removes it from m_EntityInfos when the entity is hidden.
m_RecycleQueue Entity Information Recycling Queue
The function of m_RecycleQueue is to cooperate with m_EntityInfos to release entities. Each frame will detect whether there are entities to be recycled in the recycling queue, and if so, release the first entity in the queue. Slow release avoids stuttering caused by concentrated release.
How entity information classes manage entities:
In the method of displaying entities, InternalShowEntity, you can see that the entity helper DefaultEntityHelper.CreateEntity method adds the Entity (UnityGameFramework.Runtime) component to the loaded game object and returns IEntity. IEntity is used as a parameter to create an entity information class from a reference pool. With the state switching of the entity information class entityInfo.Status, Entity completes the entity initialization (described below), the entity joins the entity group, and the entity display.
The same is true for entity shadow hiding. In the InternalHideEntity method, as the state switching is completed, the entity is released, the entity is hidden, the entity is removed from the entity group, and then the entity information class is added to the recycling queue.

2. Entity: EntityLogic: MonoBehaviour (entity business logic component)
Entity inherits from the entity logic base class EntityLogic, and EntityLogic defines the related methods of the entity logic life cycle as an abstract class.
Each specific entity inherits from Entity. An entity triggers the entity's lifecycle methods at appropriate moments in its business logic. The business logic classes of other entities such as Asteroid and Aircraft inherit from EntityLogic.
So far, Entity's entity business logic is not complete. After Entity is implemented, business logic is only added to it. For example, when the aircraft loses blood and dies, the life cycle method of entity shadow storage only implements logic content such as triggering special effects when shadow hiding . It is not called, nor is it involved in the entity object pool, and what is defined in the inherited EntityLogic is only a virtual method, and there is no implementation of the object pool. Continue down:

3. Entity: MonoBehaviour, IEntity (entity life cycle components)
Entity entities have completed the recycling, display, polling, and additional calls to remove sub-entities in the entity life cycle. The namespace is: UnityGameFramework.Runtime.
Entity's private members contain EntityLogic members, the entity business logic components. The Entity life cycle method is mainly to trigger the related business logic of the life cycle of the entity class inherited from EntityLogic.
It has no fancy function implementation itself, but calls EntityLogic's life cycle business logic in the life cycle method. What is the meaning of its existence? Note: In the above part of how the entity information class manages entities, the InternalShowEntity method of the entity manager EntityManager adds this component to the game object, and triggers the functions of initialization, joining entity groups, and entity display. The logic of entity unbinding and hiding is triggered in the InternalHideEntity method.
The role of Entity is reflected in its communication between the entity manager EntityManager and the entity business logic. EntityLogic separates the framework code of the life cycle from the business logic code of the life cycle, and makes a layer of encapsulation for the framework and only exposes it to the outside world. The interface of the life cycle. When the life cycle method is called externally, the indirect call is realized through the manager.

Object pool usage process analysis

In the first picture, we can see that the entity is created and recycled through the object pool, but after analyzing this, the object pool is not involved at all. Instead, there is an additional reference pool of the entity information class EntityInfo to manage entity information. From the figure below, you can see that the entity-related reference pools include AttachEntityInfo, EntityInfo, EntityInstanceObject, and ShowEntityInfo. Based on the principle that the object pool is a kind of encapsulation of the reference pool, we start with the entity-related reference pool.
[picture]

By observing these classes, only EntityInstanceObject inherits from ObjectBase, and others inherit from IReference. To be sure, the content of the object pool is based on EntityInstanceObject. Other entity-related references, if you are interested, you can learn by yourself. Here we mainly look at how EntityInstanceObject implements the entity object pool.
Where it ends up is how the object pool information is displayed on the inspector panel of the object pool component.
It can be found in the object pool component inspector panel decoration class ObjectPoolComponentInspector, and the inspector panel displays the information stored in the object pool base class array ObjectPoolBase[]. The beginning (EntityInstanceObject) and the end (ObjectPoolBase[]) are found, and the rest is how to connect the whole process from the beginning to the end.
ObjectPoolBase[] is obtained through the ObjectPoolComponent.GetAllObjectPools() method, and the source is the m_ObjectPools object pool dictionary of the object pool manager ObjectPoolManager. The creation and destruction of the object pool call the InternalCreateObjectPool() and InternalDestroyObjectPool() methods. The creation of the entity object pool must be through the entity manager to call the InternalCreateObjectPool() method of the object pool manager to create the object pool. Continue to follow this line of thought. There are three places in the ObjectPoolManager that call this method, which are 406 lines of creation UI object pool, line 699 creates an entity object pool, and line 774 creates a resource object pool.
The creation of the entity object pool is called when the entity group EntityManager.EntityGroup is initialized
objectPoolManager.CreateSingleSpawnObjectPool() method, and pass in the information of the entity group to complete the creation.
[picture]

The information of the entity object pool is stored in the object pool m_InstancePool of the entity group. m_InstancePool is the object pool ObjectPool that implements the IObjectPool interface. ObjectPool implements methods such as initializing a new instance of the object pool, creating objects, recycling objects, and releasing objects, adding many object pool functions.
m_Objects of ObjectPool stores the key-value pairs of object names and object classes, and m_ObjectMap stores the key-value pairs of object ontology and object classes.
Let's take a look at where the object is created (registered) in the object pool ObjectPool. Through source tracing, we can find the EntityManager.ShowEntity() method. The user calls this method to display the entity. Before the display, the m_ResourceManager.LoadAsset() is loaded first, and the loading is successful. In the callback event of EntityManager.LoadAssetSuccessCallback(), EntityInstanceObject.Create() is called to create an object instance, and the create method completes obtaining a reference from the reference pool. After the instance object is created, call the RegisterEntityInstanceObject method of the corresponding entity group, RegisterEntityInstanceObject calls the Register method of ObjectPool, and finally add the object instance into the object pool.
The process of taking out from the object pool, if you are interested, you can go through the process and analyze it.
It can be seen from the whole process that the entity object pool depends on the existence of the entity group, and the corresponding entity object (EntityInstanceObject) is created in the entity group. When an object is created, it is first added to the reference pool and then added to the entity group's object pool. The release of objects in the entity object pool is performed according to the interval seconds defined by the entity group, first released from the object pool, and then released from the reference pool.
Demo implements entity object pool, UI object pool, and resource object pool. Later, we implement our own object pool to deepen the use and understanding of object pool.

Guess you like

Origin blog.csdn.net/qq_37619255/article/details/130113822
Recommended