浏览代码

Merge remote-tracking branch 'davejrodriguez/fix/object-pool' into pr-bashing

# Conflicts:
#	UOP1_Project/Assets/Scripts/Audio/AudioManager.cs
/main
Ciro Continisio 4 年前
当前提交
6f5f996f
共有 7 个文件被更改,包括 40 次插入54 次删除
  1. 2
      UOP1_Project/Assets/Scripts/Audio/AudioManager.cs
  2. 14
      UOP1_Project/Assets/Scripts/Audio/SoundEmitters/SoundEmitterPoolSO.cs
  3. 36
      UOP1_Project/Assets/Scripts/Pool/ComponentPoolSO.cs
  4. 4
      UOP1_Project/Assets/Scripts/Pool/Example/LocalPoolTester.cs
  5. 14
      UOP1_Project/Assets/Scripts/Pool/Example/ParticlePoolSO.cs
  6. 1
      UOP1_Project/Assets/Scripts/Pool/IPool.cs
  7. 23
      UOP1_Project/Assets/Scripts/Pool/PoolSO.cs

2
UOP1_Project/Assets/Scripts/Audio/AudioManager.cs


[Header("SoundEmitters pool")]
[SerializeField] private SoundEmitterFactorySO _factory;
[SerializeField] private SoundEmitterPoolSO _pool;
[SerializeField] private int _initialSize = 10;
[Header("Listening on channels")]
[Tooltip("The SoundManager listens to this event, fired by objects in any scene, to play SFXs")]

private void Awake()
{
_SFXEventChannel.OnAudioCueRequested += PlayAudioCue;
Pool.Prewarm(_initialSize);
}
public static bool SetGroupVolume(AudioMixerGroup group, float volume)

14
UOP1_Project/Assets/Scripts/Audio/SoundEmitters/SoundEmitterPoolSO.cs


{
[SerializeField]
private SoundEmitterFactorySO _factory;
[SerializeField]
private int _initialPoolSize;
public override IFactory<SoundEmitter> Factory
{

set
{
_factory = value as SoundEmitterFactorySO;
}
}
public override int InitialPoolSize
{
get
{
return _initialPoolSize;
}
set
{
_initialPoolSize = value;
}
}
}

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


/// <typeparam name="T">Specifies the component to pool.</typeparam>
public abstract class ComponentPoolSO<T> : PoolSO<T> where T : Component
{
public abstract int InitialPoolSize { get; set; }
private void InitializePool()
{
_poolRootObject = new GameObject(name);
DontDestroyOnLoad(_poolRootObject);
for (int i = 0; i < InitialPoolSize; i++)
private GameObject PoolRootObject {
get
_available.Push(Create());
if (!Application.isPlaying)
{
return null;
}
if (_poolRootObject == null)
{
_poolRootObject = new GameObject(name);
DontDestroyOnLoad(_poolRootObject);
}
return _poolRootObject;
if (_poolRootObject == null)
{
InitializePool();
}
T member = base.Request();
member.gameObject.SetActive(true);
return member;

{
if (_poolRootObject == null)
{
InitializePool();
}
member.transform.SetParent(_poolRootObject.transform);
member.transform.SetParent(PoolRootObject.transform);
member.gameObject.SetActive(false);
base.Return(member);
}

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

base.OnDisable();
#if UNITY_EDITOR
DestroyImmediate(_poolRootObject);
DestroyImmediate(PoolRootObject);
Destroy(_poolRootObject);
Destroy(PoolRootObject);
#endif
}
}

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


_pool = ScriptableObject.CreateInstance<ParticlePoolSO>();
_pool.name = gameObject.name;
_pool.Factory = _factory;
_pool.InitialPoolSize = _initialPoolSize;
List<ParticleSystem> particles = _pool.Request(10) as List<ParticleSystem>;
_pool.Prewarm(_initialPoolSize);
List<ParticleSystem> particles = _pool.Request(2) as List<ParticleSystem>;
foreach (ParticleSystem particle in particles)
{
StartCoroutine(DoParticleBehaviour(particle));

14
UOP1_Project/Assets/Scripts/Pool/Example/ParticlePoolSO.cs


{
[SerializeField]
private ParticleFactorySO _factory;
[SerializeField]
private int _initialPoolSize;
public override IFactory<ParticleSystem> Factory
{

set
{
_factory = value as ParticleFactorySO;
}
}
public override int InitialPoolSize
{
get
{
return _initialPoolSize;
}
set
{
_initialPoolSize = value;
}
}
}

1
UOP1_Project/Assets/Scripts/Pool/IPool.cs


/// <typeparam name="T">Specifies the type of elements in the pool.</typeparam>
public interface IPool<T>
{
void Prewarm(int num);
T Request();
void Return(T member);
}

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


/// <typeparam name="T">Specifies the type of elements to pool.</typeparam>
public abstract class PoolSO<T> : ScriptableObject, IPool<T>
{
protected readonly Stack<T> _available = new Stack<T>();
protected readonly Stack<T> Available = new Stack<T>();
protected bool HasBeenPrewarmed { get; set; }
protected virtual T Create()
{

public virtual void Prewarm(int num)
{
if (HasBeenPrewarmed)
{
Debug.LogWarning($"Pool {name} has already been prewarmed.");
return;
}
for (int i = 0; i < num; i++)
{
Available.Push(Create());
}
HasBeenPrewarmed = true;
}
return _available.Count > 0 ? _available.Pop() : Create();
return Available.Count > 0 ? Available.Pop() : Create();
}
public virtual IEnumerable<T> Request(int num = 1)

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

public virtual void OnDisable()
{
_available.Clear();
Available.Clear();
}
}
}
正在加载...
取消
保存