浏览代码

Merge branch 'fix/object-pool' of https://github.com/davejrodriguez/open-project-1 into pr-bash

/main
Ciro Continisio 4 年前
当前提交
abcd1b32
共有 3 个文件被更改,包括 63 次插入16 次删除
  1. 43
      UOP1_Project/Assets/Scripts/Pool/ComponentPoolSO.cs
  2. 12
      UOP1_Project/Assets/Scripts/Pool/Example/LocalPoolTester.cs
  3. 24
      UOP1_Project/Assets/Scripts/Pool/PoolSO.cs

43
UOP1_Project/Assets/Scripts/Pool/ComponentPoolSO.cs


using UnityEngine;
using UnityEngine;
namespace UOP1.Pool
{

/// <typeparam name="T">Specifies the component to pool.</typeparam>
public abstract class ComponentPoolSO<T> : PoolSO<T> where T : Component
{
private GameObject _poolRootObject;
private GameObject PoolRootObject
private Transform _poolRoot;
private Transform PoolRoot
if (!Application.isPlaying)
{
return null;
}
if (_poolRootObject == null)
if (_poolRoot == null)
_poolRootObject = new GameObject(name);
//DontDestroyOnLoad(_poolRootObject);
_poolRoot = new GameObject(name).transform;
_poolRoot.SetParent(_parent);
return _poolRootObject;
return _poolRoot;
private Transform _parent;
/// <summary>
/// Parents the pool root transform to <paramref name="t"/>.
/// </summary>
/// <param name="t">The Transform to which this pool should become a child.</param>
/// <remarks>NOTE: Setting the parent to an object marked DontDestroyOnLoad will effectively make this pool DontDestroyOnLoad.<br/>
/// This can only be circumvented by manually destroying the object or its parent or by setting the parent to an object not marked DontDestroyOnLoad.</remarks>
public void SetParent(Transform t)
{
_parent = t;
PoolRoot.SetParent(_parent);
}
public override T Request()
{
T member = base.Request();

public override void Return(T member)
{
member.transform.SetParent(PoolRootObject.transform);
member.transform.SetParent(PoolRoot.transform);
member.gameObject.SetActive(false);
base.Return(member);
}

T newMember = base.Create();
newMember.transform.SetParent(PoolRootObject.transform);
newMember.transform.SetParent(PoolRoot.transform);
newMember.gameObject.SetActive(false);
return newMember;
}

base.OnDisable();
if (_poolRoot != null)
{
DestroyImmediate(PoolRootObject);
DestroyImmediate(_poolRoot.gameObject);
Destroy(PoolRootObject);
Destroy(_poolRoot.gameObject);
}
}
}
}

12
UOP1_Project/Assets/Scripts/Pool/Example/LocalPoolTester.cs


private ParticlePoolSO _pool;
private ParticleFactorySO _factory;
private void Start()
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
private IEnumerator Start()
_pool.SetParent(this.transform);
_pool.Prewarm(_initialPoolSize);
List<ParticleSystem> particles = _pool.Request(2) as List<ParticleSystem>;
foreach (ParticleSystem particle in particles)

yield return new WaitForSeconds(2);
_pool.SetParent(null);
yield return new WaitForSeconds(2);
_pool.SetParent(this.transform);
}
private IEnumerator DoParticleBehaviour(ParticleSystem particle)

24
UOP1_Project/Assets/Scripts/Pool/PoolSO.cs


public abstract class PoolSO<T> : ScriptableObject, IPool<T>
{
protected readonly Stack<T> Available = new Stack<T>();
/// <summary>
/// The factory which will be used to create <typeparamref name="T"/> on demand.
/// </summary>
public abstract IFactory<T> Factory { get; set; }
protected bool HasBeenPrewarmed { get; set; }

}
/// <summary>
/// Prewarms the pool with a <paramref name="num"/> of <typeparamref name="T"/>.
/// </summary>
/// <param name="num">The number of members to create as a part of this pool.</param>
/// <remarks>NOTE: This method can be called at any time, but only once for the lifetime of the pool.</remarks>
public virtual void Prewarm(int num)
{
if (HasBeenPrewarmed)

HasBeenPrewarmed = true;
}
/// <summary>
/// Requests a <typeparamref name="T"/> from this pool.
/// </summary>
/// <returns>The requested <typeparamref name="T"/>.</returns>
/// <summary>
/// Batch requests a <typeparamref name="T"/> collection from this pool.
/// </summary>
/// <returns>A <typeparamref name="T"/> collection.</returns>
public virtual IEnumerable<T> Request(int num = 1)
{
List<T> members = new List<T>(num);

return members;
}
/// <summary>
/// Returns a <typeparamref name="T"/> to the pool.
/// </summary>
/// <param name="member">The <typeparamref name="T"/> to return.</param>
/// <summary>
/// Returns a <typeparamref name="T"/> collection to the pool.
/// </summary>
/// <param name="members">The <typeparamref name="T"/> collection to return.</param>
public virtual void Return(IEnumerable<T> members)
{
foreach (T member in members)

正在加载...
取消
保存