浏览代码

Simplified parenting

Simplified parenting logic
Added some comment docs
/main
Dave Rodriguez 4 年前
当前提交
7c6961f0
共有 3 个文件被更改,包括 37 次插入40 次删除
  1. 45
      UOP1_Project/Assets/Scripts/Pool/ComponentPoolSO.cs
  2. 8
      UOP1_Project/Assets/Scripts/Pool/Example/LocalPoolTester.cs
  3. 24
      UOP1_Project/Assets/Scripts/Pool/PoolSO.cs

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


using UnityEngine;
using UnityEngine;
namespace UOP1.Pool
{

if (_poolRoot == null)
{
_poolRoot = new GameObject(name).transform;
_poolRoot.SetParent(_parent);
}
return _poolRoot;
}

private Transform Parent
{
get
{
if (_parent == null)
{
_parent = PoolRoot;
}
return _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>
Transform newParent = t != null ? t : PoolRoot;
if (_parent != null && _parent != newParent)
{
foreach (T member in Available)
{
member.transform.SetParent(newParent);
}
CheckCleanPoolRoot();
}
_parent = newParent;
}
void CheckCleanPoolRoot()
{
if (_poolRoot != null && _poolRoot != _parent && _poolRoot.childCount == 0)
{
Destroy(_poolRoot.gameObject);
}
_parent = t;
PoolRoot.SetParent(_parent);
}
public override T Request()

public override void Return(T member)
{
member.transform.SetParent(Parent.transform);
member.transform.SetParent(PoolRoot.transform);
CheckCleanPoolRoot();
base.Return(member);
}

newMember.transform.SetParent(Parent.transform);
newMember.transform.SetParent(PoolRoot.transform);
newMember.gameObject.SetActive(false);
return newMember;
}

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


_pool = ScriptableObject.CreateInstance<ParticlePoolSO>();
_pool.name = gameObject.name;
_pool.Factory = _factory;
// Set the pool parent to this object. The pool's lifetime is now tied to this object. This method can be called at any time,
// But if you do not set the parent before the first request/return is made, the pool members will be parented to a generated object.
//_pool.SetParent(this.transform);
_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);
// You can set the parent to null at any time and the pool members will be re-parented to a generated object.
// Objects that are currently in use will now target the generated object for parenting when Returned.
// And back to this object.
}
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)

正在加载...
取消
保存