Unity Game FrameWork - module usage - object pool usage

To use an object pool, ObjectBase must be inherited. First create an OPGame class, inherited from ObjectBase, we call it the OP object for the time being, as shown below
[picture]

The OP object has two places where member objects or variables can be stored, one is inside the OP object such as the model ID: m_ModelID. The other is that the m_Target in the object pool base class ObjectBase can store any data type, which requires boxing and unboxing. When creating the OP object, we pass in the custom type ModelInfor and pass it in as a parameter when the base class is initialized.

API calls

Create an OP object pool:
m_OPPool = GameEntry.ObjectPool.CreateSingleSpawnObjectPool(Utility.Text.Format(“OP Pool ({0})”, name), 20, 10, 0);
Create an OP object: After creation, it is in use
ModelInfor ModelInfor = new ModelInfor(i);
OPGame OPGame = OPGame.Create("Model" + CreateNums, CreateNums, ModelInfor);
m_OPPool.Register(OPGame, true);
Get OP object: get unused OP object
OPGame from object pool OPGame = m_OPPool.Spawn("Model" + 0);
Recycle OP object: Release the object in use and recycle it to the object pool
m_OPPool.Unspawn(OPGame);
Set whether the OP object is locked: After locking, the object cannot be Destroy in the pool
m_OPPool.SetLocked(OPGame, true);
set the OP object priority: use
m_OPPool.SetPriority(OPGame, 10);

full code

ProcedureObjectPool: Start the process and create ObjectPlayer objects

using UnityEngine;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
namespace StarForce
{
    
    
    public class ProcedureObjectPool : ProcedureBase
    {
    
    
        public override bool UseNativeDialog
        {
    
    
            get
            {
    
    
                return true;
            }
        }

        public override void OnEnter(ProcedureOwner procedureOwner)
        {
    
    
            base.OnEnter(procedureOwner);
            GameObject TT = new GameObject("Player");
            TT.AddComponent<ObjectPlayer>();
        }
    }
}

OPGame: OP object and stored data class

using GameFramework;
using GameFramework.ObjectPool;
using UnityEngine;

public class OPGame : ObjectBase
{
    
    
    private int m_ModelID;

    public OPGame()
    {
    
    
    }

    public static OPGame Create(string name,int ID, ModelInfor Model)
    {
    
    
        OPGame InstanceOP = ReferencePool.Acquire<OPGame>();
        InstanceOP.Initialize(name, Model);
        InstanceOP.m_ModelID = ID;
        return InstanceOP;
    }

    public override void Clear()
    {
    
    
        base.Clear();
        m_ModelID = -1;
    }
    public override void Release(bool isShutdown)
    {
    
    
        
    }
}
public class ModelInfor
{
    
    
    private int ID;
    private Mesh Mesh;
    private Texture2D Texture2D;
    public ModelInfor(int ID)
    {
    
    
        this.ID = ID;
        Mesh = null;
        Texture2D = null;
    }
    public ModelInfor()
    {
    
    
        this.ID = -1;
        Mesh = null;
        Texture2D = null;
    }
    public Mesh GetMesh()
    {
    
    
        return Mesh;
    }
    public Texture2D GetTexture2D()
    {
    
    
        return Texture2D;
    }
    public int GetID()
    {
    
    
        return ID;
    }
}

ObjectPlayer: OP object pool call implementation

using GameFramework;
using GameFramework.ObjectPool;
using StarForce;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPlayer : MonoBehaviour
{
    
    
    //创建对象名称编号
    int CreateNums = 0;
    private IObjectPool<OPGame> m_OPPool;
    List<OPGame> UsingOPGames;
    void Start()
    {
    
    
        m_OPPool = GameEntry.ObjectPool.CreateSingleSpawnObjectPool<OPGame>(Utility.Text.Format("OP Pool ({0})", name), 20, 10, 0);
        m_OPPool.AutoReleaseInterval = 5;
        UsingOPGames = new List<OPGame>();
    }
    [ContextMenu("CreateItem")]
    public void CreateItem()
    {
    
    
        for (int i = 0; i < 10; i++)
        {
    
    
            ModelInfor ModelInfor = new ModelInfor(i);
            OPGame OPGame = OPGame.Create("模型" + CreateNums, CreateNums, ModelInfor);
            //创建对象
            m_OPPool.Register(OPGame, true);
            UsingOPGames.Add(OPGame);
            if (CreateNums == 2)
            {
    
    
                CreateNums = 0;
            }
            else
            {
    
    
                CreateNums++;
            }
            if (i==0)
            {
    
    
                m_OPPool.SetLocked(OPGame, true);
                m_OPPool.SetPriority(OPGame, 10);
            }
            ModelInfor Model = (ModelInfor)OPGame.Target;
            Debug.LogError(Model.GetID());
        }
        
    }
    [ContextMenu("SpawnItem")]
    public void SpawnItem()
    {
    
    
        //获取对象
        OPGame OPGame = m_OPPool.Spawn("模型" + 0);
        UsingOPGames.Add(OPGame);
        Debug.LogError("获取的对象:"+OPGame.Name);
    }
    [ContextMenu("UnspawnItem")]
    public void UnspawnItem()
    {
    
    
        if (UsingOPGames.Count>3)
        {
    
    
            for (int i = 2; i >= 0; i--)
            {
    
    
                OPGame OPGame = UsingOPGames[i];
                //回收对象
                m_OPPool.Unspawn(OPGame);
                UsingOPGames.Remove(OPGame);
                Debug.LogError("释放的对象:" + OPGame.Name);
            }
        }
    }
}

achieve effect

Create three functions as shown in the figure, CreateItem, SpawnItem, UnspawnItem
[picture]

The function call entry is as follows, click to call. The calling order is CreateItem, UnspawnItem, SpawnItem
[picture]

CreateItem realizes the creation of 10 objects, the first object is locked, and the priority is set to 10
[picture]

UnspawnItem realizes the recovery of the first three objects created.
[picture]
SpawnItem realizes the acquisition of the object named "Model 0" from the object pool, and InUse changes from false to true.
[picture]
When we do not realize the operation of obtaining the name "Model 0", it is too After a while, the object whose InUse status is false is destroyed by the object pool, but the "model 0" whose Locked is set to true is not destroyed.
[picture]

Guess you like

Origin blog.csdn.net/qq_37619255/article/details/130129751