u3d 缓冲池

  主要参考 http://catlikecoding.com/unity/tutorials/  教程:

Pool类和Object类;

Pool.cs 负责管理一个ObjectList    行为: GetPool获取static Pool  GetObject AddObject     

Object.cs   给外部 GetObject   和AddObjectToPool

项目中设置pool,会进行计算使用次数,一定时间后,如果没有使用,则清除。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using UnityEngine;
using System.Collections.Generic;
 
public class ObjectPool : MonoBehaviour
{
    private List<PooledObject> mObjectList = new List<PooledObject>();
    public static ObjectPool GetPool(PooledObject fobj)
    {
        string tempPoolName = fobj.name + "Pool";
        ObjectPool tempPool = null;
        GameObject tempPoolGameobj = GameObject.Find(tempPoolName);
        if (tempPoolGameobj == null)
        {
            tempPoolGameobj = new GameObject(tempPoolName);
        }
        tempPool = tempPoolGameobj.GetComponent<ObjectPool>();
        if (tempPool == null)
        {
            tempPool = tempPoolGameobj.AddComponent<ObjectPool>();
        }
        return tempPool;
    }
 
    /// <summary>
    /// 获取缓冲区内的数据
    /// </summary>
    /// <returns></returns>
    public PooledObject GetPoolObj(PooledObject fobj)
    {
        int tempIndex = mObjectList.Count - 1;
        PooledObject tempObj = null;
        if (tempIndex >= 0)
        {
            tempObj = mObjectList[tempIndex];
            tempObj.gameObject.SetActive(true);
            mObjectList.RemoveAt(tempIndex);
        }
        else
        {
            tempObj = Instantiate<PooledObject>(fobj);
            tempObj.transform.SetParent(transform, false);
            tempObj.mPool = this;
        }
        return tempObj;
    }
    /// <summary>
    /// 向缓冲区内添加数据
    /// </summary>
    public void AddObjToPool(PooledObject fpoolObj)
    {
        fpoolObj.gameObject.SetActive(false);
        mObjectList.Add(fpoolObj);
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using UnityEngine;
 
public class PooledObject : MonoBehaviour
{
    [System.NonSerialized]
    public ObjectPool mPool = null;
 
 
    public int testInt = 1;
    //获取池中object
    public T GetObject<T>() where T : PooledObject
    {
        if (mPool == null)
        {
            mPool = ObjectPool.GetPool(this);
        }
        testInt = 2;
        return (T)mPool.GetPoolObj(this);
    }
 
    //把此object加入池中
 
    public void AddObjToPool()
    {
        Debug.LogError("testInt :" + testInt);
        if (mPool == null)
        {
            Debug.LogError("is null!!!!");
        }
        if (mPool != null)
        {
            mPool.AddObjToPool(this);
        }
    }
}

  

posted @   sun_dust_shadow  阅读(348)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示