unity 对象池Object Pool

unity内置的对象池简单使用

对象池的概念

当在Unity中创建和销毁对象时,会涉及到内存分配和释放,这可能会导致性能问题。为了避免频繁的对象创建和销毁,以及减少内存分配的开销,可以使用对象池的概念。

对象池是一种重复使用已经创建的对象的技术,而不是在需要时创建新对象。这可以显著提高性能,尤其是在需要频繁创建和销毁对象的情况下,比如游戏中的子弹、敌人或者粒子系统等。

简单例子

1.场景搭建

只需要一个地面和一个在地面之上的空对象

2.创建相关预制体和代码文件

创建一个默认的sphere加上刚体组件,并创建一个Shape(Script)给sphere

和一个internalObjectPool(Script)给空对象

3.编写代码

1.internalObjectPool的代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;

public class internalObjectPool : MonoBehaviour
{

    public Shape sphere; //用于指定在对象池中创建的对象的原型。

    public int spwanCount = 10;//用于指定每次生成对象的数量。

    private ObjectPool<Shape> pool;//对象池实例。

    public int defaultCapacity; 
    public int maxSize;//用于指定对象池的初始容量和最大容量。

    public int CountAll;//活动和非活动对象的总数。
    public int CountActive;//池已创建但当前正在使用且尚未返回的对象数。
    public int CountInactive;//池中当前可用的对象数。就是没有被激活的
    private void Start()
    {
        //在启动时,创建对象池实例 pool 固定用法
        pool = new ObjectPool<Shape>(CreateFunc, actionOnGet, actionOnRelease, actionOnDestroy,true, defaultCapacity, maxSize);
        InvokeRepeating("SpawnObject", 0.2f, 0.2f);//这里也可以用协程

    }
    private void Update()
    {
        CountAll = pool.CountAll;
        CountActive = pool.CountActive;
        CountInactive = pool.CountInactive;
    }
    private Shape CreateFunc()
    {
        return Instantiate(sphere);
    }
    private void actionOnGet(Shape shape)
    {
        shape.gameObject.SetActive(true); //将对象的 gameObject 设置为激活状态。
    }
    private void actionOnRelease(Shape shape)
    {
        shape.gameObject.SetActive(false);//将对象的 gameObject 设置为非激活状态。
    }
    private void actionOnDestroy(Shape shape)
    {
        Destroy(shape.gameObject);//调用 Destroy 方法销毁对象的 gameObject。
    }
    void SpawnObject()
    {
        for (int i = 0; i < spwanCount; i++)
        {
            Shape sphereObj = pool.Get();
            sphereObj.Init(DestroyShape);
            sphereObj.transform.position = transform.position + UnityEngine.Random.insideUnitSphere * 10f;
        }
    }

    void DestroyShape(Shape shape)
    {
        pool.Release(shape);
    }
}
2.sphere代码如下

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

public class Shape : MonoBehaviour
{
    private System.Action<Shape> onShapeDestroyed;
    
    public void Init(System.Action<Shape> onShapeDestroyed)//接收一个 System.Action<Shape> 类型的回调函数(onShapeDestroyed)作为参数。
    {
        
        SetRandomColor();//通过 SetRandomColor 方法为 Shape 设置随机颜色。
        this.onShapeDestroyed = onShapeDestroyed;// 将传递的 onShapeDestroyed 回调保存在 onShapeDestroyed 字段中。
    }

    private void SetRandomColor()
    {
        Color randomColor  = new Color(Random.value, Random.value, Random.value);

        Renderer renderer = GetComponent<Renderer>();// 获取 Renderer 组件

        if (renderer !=null)
        {
            renderer.material.color = randomColor;//将其材质的颜色设置为随机颜色。
        }
    }

    private void OnTriggerEnter(Collider other)//当与其他 Collider 碰撞时(例如,某个区域触发器),调用传递进来的 onShapeDestroyed 回调,通知外部对象池对象的销毁。
    {
        //Destroy(this.gameObject);
        onShapeDestroyed(this);// 实际是调用internalObjectPool类中的DestroyShape方法;
    }
}
 3.在inspector窗口进行赋值

4.运行测试

只是简单了解

猜你喜欢

转载自blog.csdn.net/zzexcellent27/article/details/134678083
今日推荐