浏览代码

Object Pool Simplification

Removed ComponentPoolSO
Modified FactorySO to be fully abstract
Removed IPoolable
Modified PoolSO and ComponentPoolSO to not constrain to IPoolable
Modified Example to use unwrapped ParticleSystem component
/main
Dave Rodriguez 4 年前
当前提交
05631f89
共有 7 个文件被更改,包括 12 次插入72 次删除
  1. 7
      UOP1_Project/Assets/Scripts/Factory/FactorySO.cs
  2. 9
      UOP1_Project/Assets/Scripts/Pool/ComponentPoolSO.cs
  3. 11
      UOP1_Project/Assets/Scripts/Pool/PoolSO.cs
  4. 11
      UOP1_Project/Assets/Scripts/Factory/ComponentFactorySO.cs.meta
  5. 22
      UOP1_Project/Assets/Scripts/Factory/ComponentFactorySO.cs
  6. 13
      UOP1_Project/Assets/Scripts/Pool/IPoolable.cs
  7. 11
      UOP1_Project/Assets/Scripts/Pool/IPoolable.cs.meta

7
UOP1_Project/Assets/Scripts/Factory/FactorySO.cs


/// Implements the IFactory interface for non-abstract types.
/// </summary>
/// <typeparam name="T">Specifies the non-abstract type to create.</typeparam>
public abstract class FactorySO<T> : ScriptableObject, IFactory<T> where T : new()
public abstract class FactorySO<T> : ScriptableObject, IFactory<T>
public virtual T Create()
{
return new T();
}
public abstract T Create();
}
}

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


/// Implements a Pool for Component types.
/// </summary>
/// <typeparam name="T">Specifies the component to pool.</typeparam>
public abstract class ComponentPoolSO<T> : PoolSO<T> where T : Component, IPoolable
public abstract class ComponentPoolSO<T> : PoolSO<T> where T : Component
{
public abstract int InitialPoolSize { get; set; }
private GameObject _poolRootObject;

{
InitializePool();
}
return base.Request();
T member = base.Request();
member.gameObject.SetActive(true);
return member;
}
public override void Return(T member)

InitializePool();
}
member.transform.SetParent(_poolRootObject.transform);
member.gameObject.SetActive(false);
base.Return(member);
}

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

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


/// A generic pool that generates members of type T on-demand via a factory.
/// </summary>
/// <typeparam name="T">Specifies the type of elements to pool.</typeparam>
public abstract class PoolSO<T> : ScriptableObject, IPool<T> where T : IPoolable
public abstract class PoolSO<T> : ScriptableObject, IPool<T>
{
protected readonly Stack<T> _available = new Stack<T>();
public abstract IFactory<T> Factory { get; set; }

public virtual T Request()
{
T member = _available.Count > 0 ? _available.Pop() : Create();
member.OnRequest();
return member;
return _available.Count > 0 ? _available.Pop() : Create();
}
public virtual IEnumerable<T> Request(int num = 1)

public virtual void Return(T member)
{
member.OnReturn(() =>
{
_available.Push(member);
});
_available.Push(member);
}
public virtual void Return(IEnumerable<T> members)

11
UOP1_Project/Assets/Scripts/Factory/ComponentFactorySO.cs.meta


fileFormatVersion: 2
guid: 1d5f471b49fdf1c43a84a8deeedc5183
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

22
UOP1_Project/Assets/Scripts/Factory/ComponentFactorySO.cs


using UnityEngine;
namespace UOP1.Factory
{
/// <summary>
/// Implements the IFactory interface for Component types.
/// </summary>
/// <typeparam name="T">Specifies the component to create.</typeparam>
public abstract class ComponentFactorySO<T> : ScriptableObject, IFactory<T> where T : Component
{
public abstract T Prefab
{
get;
set;
}
public virtual T Create()
{
return Instantiate(Prefab);
}
}
}

13
UOP1_Project/Assets/Scripts/Pool/IPoolable.cs


using System;
namespace UOP1.Pool
{
/// <summary>
/// Represents an object that can be pooled.
/// </summary>
public interface IPoolable
{
void OnRequest();
void OnReturn(Action onReturned);
}
}

11
UOP1_Project/Assets/Scripts/Pool/IPoolable.cs.meta


fileFormatVersion: 2
guid: 936084c4c3e4d6848bfa61084af46c1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存