版本: Unity 6 (6000.0)
语言English
  • C#

ObjectPool<T0>

UnityEngine.Pool 中的类

/

实现于:UnityEngine.CoreModule


实现接口:IObjectPool<T0>

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们会阅读用户提出的每个建议更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

描述

一个基于栈的 IObjectPool<T0>

对象池是一种优化项目并降低 CPU 在快速创建和销毁新对象时负担的方法。这是一个值得牢记的良好实践和设计模式,它有助于减轻 CPU 的处理能力,使其处理更重要的任务,而不是被重复的创建和销毁调用所淹没。ObjectPool 使用栈来保存要重用的对象实例的集合,并且不是线程安全的。

using System.Text;
using UnityEngine;
using UnityEngine.Pool;

// This component returns the particle system to the pool when the OnParticleSystemStopped event is received. [RequireComponent(typeof(ParticleSystem))] public class ReturnToPool : MonoBehaviour { public ParticleSystem system; public IObjectPool<ParticleSystem> pool;

void Start() { system = GetComponent<ParticleSystem>(); var main = system.main; main.stopAction = ParticleSystemStopAction.Callback; }

void OnParticleSystemStopped() { // Return to the pool pool.Release(system); } }

// This example spans a random number of ParticleSystems using a pool so that old systems can be reused. public class PoolExample : MonoBehaviour { public enum PoolType { Stack, LinkedList }

public PoolType poolType;

// Collection checks will throw errors if we try to release an item that is already in the pool. public bool collectionChecks = true; public int maxPoolSize = 10;

IObjectPool<ParticleSystem> m_Pool;

public IObjectPool<ParticleSystem> Pool { get { if (m_Pool == null) { if (poolType == PoolType.Stack) m_Pool = new ObjectPool<ParticleSystem>(CreatePooledItem, OnTakeFromPool, OnReturnedToPool, OnDestroyPoolObject, collectionChecks, 10, maxPoolSize); else m_Pool = new LinkedPool<ParticleSystem>(CreatePooledItem, OnTakeFromPool, OnReturnedToPool, OnDestroyPoolObject, collectionChecks, maxPoolSize); } return m_Pool; } }

ParticleSystem CreatePooledItem() { var go = new GameObject("Pooled Particle System"); var ps = go.AddComponent<ParticleSystem>(); ps.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);

var main = ps.main; main.duration = 1; main.startLifetime = 1; main.loop = false;

// This is used to return ParticleSystems to the pool when they have stopped. var returnToPool = go.AddComponent<ReturnToPool>(); returnToPool.pool = Pool;

return ps; }

// Called when an item is returned to the pool using Release void OnReturnedToPool(ParticleSystem system) { system.gameObject.SetActive(false); }

// Called when an item is taken from the pool using Get void OnTakeFromPool(ParticleSystem system) { system.gameObject.SetActive(true); }

// If the pool capacity is reached then any items returned will be destroyed. // We can control what the destroy behavior does, here we destroy the GameObject. void OnDestroyPoolObject(ParticleSystem system) { Destroy(system.gameObject); }

void OnGUI() { GUILayout.Label("Pool size: " + Pool.CountInactive); if (GUILayout.Button("Create Particles")) { var amount = Random.Range(1, 10); for (int i = 0; i < amount; ++i) { var ps = Pool.Get(); ps.transform.position = Random.insideUnitSphere * 10; ps.Play(); } } } }

属性

CountActive池已创建但当前正在使用且尚未返回的对象数量。
CountAll活动和非活动对象的总数。
CountInactive当前在池中可用的对象数量。

构造函数

ObjectPool_1创建一个新的 ObjectPool 实例。

公共方法

Clear移除所有池化项。如果池包含销毁回调,则会对池中的每个项调用它。
Dispose移除所有池化项。如果池包含销毁回调,则会对池中的每个项调用它。
Get从池中获取一个实例。如果池为空,则会创建一个新的实例。
Release将实例返回到池中。将实例返回到已满的池会导致该实例被销毁。