当前位置: 首页 > news >正文

unity自己对象池的使用

unity出了自己的对象池 这里记录一下用法

  • 命名空间就是这个
    在这里插入图片描述

  • 一般有两种用法,第一种是在using里面获取,脱离这个域就释放。第二种是在Get和Release配合使用

 // This version will only be returned to the pool if we call Release on it.
 //只有使用Release才会释放
        var simplifiedPoints = ListPool<Vector2>.Get();
//这种只要脱离了Scope就会自动释放
        // Copy the points into a temp list so we can pass them into the Simplify method
        // When the pooled object leaves the scope it will be Disposed and returned to the pool automatically.
        // This version is ideal for working with temporary lists.
        using (var pooledObject = ListPool<Vector2>.Get(out List<Vector2> tempList))
        {
            for (int i = 0; i < points.Length; ++i)
            {
                tempList.Add(points[i]);
            }

            LineUtility.Simplify(tempList, 1.5f, simplifiedPoints);
        }
        return simplifiedPoints;
  • 一般比较常用的就几个集合类和下面这个,可以对自己的类进行一个池子回收。
using UnityEngine.Pool;

public class GenericPoolExample
{
    class MyClass
    {
        public int someValue;
        public string someString;
    }

    void GetPooled()
    {
        // Get an instance
        var instance = GenericPool<MyClass>.Get();

        // Return the instance
        GenericPool<MyClass>.Release(instance);
    }
}
  • 对于一些unity中的GameObject对象可以使用ObjectPool

相关文章:

  • CHAPTER 10: DESIGN A NOTIFICATION SYSTEM
  • C/C++统计满足条件的4位数个数 2023年5月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
  • python装13的一些写法
  • flume安装及实战
  • 数据库选型参考
  • 数据分享|R语言生态学种群空间点格局分析:聚类泊松点过程对植物、蚂蚁巢穴分布数据可视化...
  • 【Linux】Linux环境基础开发工具使用
  • 多线程带来的的风险-线程安全
  • MySQL集群高可用架构之MMM
  • 设计模式-代理模式
  • 【python爬虫】—星巴克产品
  • 【C语言】指针经典笔试题(上)
  • [Linux入门]---git命令行的基本使用
  • pytorch生成CAM热力图-单张图像
  • Python实现简单的爬虫功能
  • CListCtrl设置只显示单列
  • RabbitMQ配置文件_修改RabbitMQ MQTT的1883端口
  • OJ练习第180题——颠倒二进制位
  • 我的创作纪念日
  • 多维时序 | MATLAB实现WOA-CNN-GRU-Attention多变量时间序列预测(SE注意力机制)
  • 普京签署有关批准俄罗斯与伊朗全面战略伙伴关系条约的联邦法令
  • “小时光:地铁里的阅读”摄影展开幕,嘉宾共话日常生活与阅读
  • 美方因涉港问题对中国官员滥施非法单边制裁,外交部:强烈谴责,对等反制
  • 中国房地产报:以改促治实现楼市多难并解
  • 全球南方声势卓然壮大的历史逻辑——写在万隆会议召开70周年之际
  • “30小时不够”,泽连斯基建议延长停火至30天