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

LinkedPool<T0> 构造函数

建议修改

成功!

感谢您帮助我们提高 Unity 文档质量。虽然我们无法接受所有意见,但我们还是会阅读用户提出的每条建议修改并酌情进行更新。

关闭

提交失败

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

关闭

取消

声明

public LinkedPool<T0>(Func<T> createFunc, Action<T> actionOnGet, Action<T> actionOnRelease, Action<T> actionOnDestroy, bool collectionCheck, int maxSize);

参数

createFunc 在池为空时用于创建一个新实例。在大多数情况下,这将只是 `() = new T()`。
actionOnGet 在实例从池中取出时调用。
actionOnRelease 在实例被放回池中时调用。它可用于清理或禁用实例。
actionOnDestroy 在无法将元素放回池中(由于池已达到最大容量)时调用。
collectionCheck 将在实例返回到池时执行集合检查。如果实例已经在池中,则会引发异常。集合检查仅在编辑器中执行。
maxSize 池的最大容量。当池达到最大容量时,则会销毁返回到池的任何其他实例,并进行垃圾收集。它可用于防止池增长到非常大的容量。

描述

创建一个新的 LinkedPool 实例。

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(); } } } }