您现在的位置是:首页 >技术杂谈 >Unity Game FrameWork—模块使用—对象池使用网站首页技术杂谈
Unity Game FrameWork—模块使用—对象池使用
使用对象池,需继承ObjectBase。首先创建一个OPGame的类,继承于ObjectBase,我们暂且把它叫做OP对象,如下图
OP对象有两个地方可以存储成员对象或变量,一个是在OP对象内部如模型ID:m_ModelID。另一个则是对象池基类ObjectBase里的m_Target可以存储任何数据类型,需要装箱和拆箱。在创建OP对象时,我们传入自定义的类型ModelInfor,并在基类初始化时作为参数传入。
API调用
创建OP对象池:
m_OPPool = GameEntry.ObjectPool.CreateSingleSpawnObjectPool(Utility.Text.Format(“OP Pool ({0})”, name), 20, 10, 0);
创建OP对象: 创建完成后处于使用中
ModelInfor ModelInfor = new ModelInfor(i);
OPGame OPGame = OPGame.Create(“模型” + CreateNums, CreateNums, ModelInfor);
m_OPPool.Register(OPGame, true);
获取OP对象: 从对象池中获取未使用的OP对象
OPGame OPGame = m_OPPool.Spawn(“模型” + 0);
回收OP对象: 将正在使用的对象释放,回收至对象池
m_OPPool.Unspawn(OPGame);
设置OP对象是否被加锁: 加锁后,不能从对象池中销毁
m_OPPool.SetLocked(OPGame, true);
设置OP对象优先级: 没啥卵用
m_OPPool.SetPriority(OPGame, 10);
完整代码
ProcedureObjectPool:启动流程,创建ObjectPlayer对象
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对象及所存储的数据类
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对象池调用实现
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);
}
}
}
}
实现效果
如图创建三个函数,CreateItem,SpawnItem,UnspawnItem
函数调用入口如下,点击调用。调用顺序依次为CreateItem,UnspawnItem,SpawnItem
CreateItem实现了创建10个对象,第一个对象加锁,优先级设置为10
UnspawnItem实现了回收最先创建的三个对象
SpawnItem实现了从对象池中获取名称为“模型0”的对象,InUse从false变为了true
当我们不实现获取名称为“模型0”的操作时,过一会InUse状态为false的对象被对象池销毁了,而Locked设置为true的“模型0”则没有被销毁。