Unity Game FrameWork - Module Usage - Reference Pool

The reference pool is actually what we usually call the object pool. In the program, it mainly plays the role of preventing objects from being frequently created and destroyed, reducing gc, and preloading.
There are two types of pools in GF, one is called the reference pool, and the other is called the object pool. The principle of the two is the same, but the specific implementation and target objects are different. The reference pool is generally used to store ordinary C# type objects, while the object pool is generally The use of the reference pool used to store objects under UnityEngine (such as GameObject objects in Unity)
can basically exist independently from the reference pool component ReferencePoolComponent. There is one and only one attribute in the component, namely: EnableStrictCheck (whether to enable mandatory check).

API usage:
Add a specified number of references to the reference pool:
ReferencePool.Add(4);
ReferencePool.Add(typeof(StructData),4);
The added reference is marked as Unused (unused), and the reference count is +1

Acquire a reference from the reference pool:
StructData StructData1 = ReferencePool.Acquire();
When acquiring, if the reference pool is empty, create a reference. If there is a released or unused reference in the reference pool, it will be taken out from the reference pool. After taking it out, the object will not be retained in the reference pool. The obtained reference is marked as Using (in use), and the reference count is +1

Release the reference:
ReferencePool.Release(StructData1);
return the reference to the reference pool

Remove references:
ReferencePool.Remove(2);
ReferencePool.RemoveAll(typeof(StructData));
Removal is removed from the reference pool, and the storage in the reference pool is marked as Unused and Release.
The reference count can be seen from the inspector panel of the pool component.
[picture]
From the above figure, you can see the data node, finite state machine, and logging node (if there is a print log). In addition to the displayed ones, a large number of events and related components in the framework are useful. .

Paste the Demo code below:

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

public class Player : MonoBehaviour
{
    
    
    ReferencePoolInfo[] Tem;
    State state1,state2;
    StructData StructData1, StructData2, StructData3;
    void Start()
    {
    
    
        //创建状态列表(使用引用池)
        state1 = ReferencePool.Acquire<State>();
        state1.Add("游走状态");
        state2 = ReferencePool.Acquire<State>();
        state2.Add("移动状态");

        StructData1 = ReferencePool.Acquire<StructData>();
        StructData1.Add("游走状态", new Vector2Int(0, 0));
        StructData2 = ReferencePool.Acquire<StructData>();
        StructData2.Add("移动状态", new Vector2Int(1, 15));
        StructData3 = ReferencePool.Acquire<StructData>();
        StructData3.Add("跳跃状态", new Vector2Int(4, 15));
        ReferencePool.Add<StructData>(4);

        Tem = ReferencePool.GetAllReferencePoolInfos();
        foreach (var item in Tem)
        {
    
    
            Debug.LogError(item.Type + ":" + item.UsingReferenceCount);
        }
    }
    [ContextMenu("ReleaseItem")]
    public void ReleaseItem()
    {
    
    
        ReferencePool.Release(state2);
        foreach (var item in Tem)
        {
    
    
            Debug.LogError(item.Type + ":" + item.UsingReferenceCount);
        }
    }
    [ContextMenu("ReleaseList")]
    public void ReleaseList()
    {
    
    
        ReferencePool.Release(StructData1);
        ReferencePool.Release(StructData3);
        ReferencePool.RemoveAll(typeof(StructData));
        foreach (var item in Tem)
        {
    
    
            Debug.LogError(item.Type + ":" + item.UsingReferenceCount);
        }
    }
    [ContextMenu("AddItem")]
    public void AddItem()
    {
    
    
        StructData C = ReferencePool.Acquire<StructData>();
    }
}
public class State : IReference
{
    
    
    string StateName;
    public State()
    {
    
    
    }
    public void Add(string StateName)
    {
    
    
        this.StateName = StateName;
    }
    public void Clear()
    {
    
    
        StateName = null;
    }
}
public class StructData : IReference
{
    
    
    string StateName;
    Vector2Int pos;
    public StructData()
    {
    
    
    }
    public void Add(string StateName, Vector2Int pos)
    {
    
    
        this.StateName = StateName;
        this.pos = pos;
    }
    public void Clear()
    {
    
    
        StateName = null;
        pos = default(Vector2Int);
    }
}

Guess you like

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