浏览代码

Modified Example Scene

Modified Example Scene to use unwrapped Particle System
/main
Dave Rodriguez 4 年前
当前提交
395ad900
共有 9 个文件被更改,包括 33 次插入1090 次删除
  1. 2
      UOP1_Project/Assets/Scripts/Pool/Example/Assets/Example.unity
  2. 24
      UOP1_Project/Assets/Scripts/Pool/Example/LocalPoolTester.cs
  3. 16
      UOP1_Project/Assets/Scripts/Pool/Example/ParticleFactorySO.cs
  4. 4
      UOP1_Project/Assets/Scripts/Pool/Example/ParticlePoolSO.cs
  5. 21
      UOP1_Project/Assets/Scripts/Pool/Example/PoolTester.cs
  6. 1001
      UOP1_Project/Assets/Scripts/Pool/Example/Assets/Particles.prefab
  7. 7
      UOP1_Project/Assets/Scripts/Pool/Example/Assets/Particles.prefab.meta
  8. 37
      UOP1_Project/Assets/Scripts/Pool/Example/PoolableParticle.cs
  9. 11
      UOP1_Project/Assets/Scripts/Pool/Example/PoolableParticle.cs.meta

2
UOP1_Project/Assets/Scripts/Pool/Example/Assets/Example.unity


m_Script: {fileID: 11500000, guid: ff0fb790d778a05429eca3a001ab6be1, type: 3}
m_Name:
m_EditorClassIdentifier:
_prefab: {fileID: -6757246126632128155, guid: 13e8a2294914b7744a0b9fe47b807ead,
type: 3}
_initialPoolSize: 5
--- !u!4 &648587143
Transform:

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


public class LocalPoolTester : MonoBehaviour
{
[SerializeField]
private PoolableParticle _prefab = default;
[SerializeField]
private IEnumerator Start()
private void Start()
_factory.Prefab = _prefab;
List<PoolableParticle> particles = _pool.Request(10) as List<PoolableParticle>;
foreach (PoolableParticle particle in particles)
List<ParticleSystem> particles = _pool.Request(10) as List<ParticleSystem>;
foreach (ParticleSystem particle in particles)
particle.transform.position = Random.insideUnitSphere * 5f;
particle.Play();
StartCoroutine(DoParticleBehaviour(particle));
yield return new WaitForSecondsRealtime(5f);
_pool.Return(particles);
}
private IEnumerator DoParticleBehaviour(ParticleSystem particle)
{
particle.transform.position = Random.insideUnitSphere * 5f;
particle.Play();
yield return new WaitForSeconds(particle.main.duration);
particle.Stop(true, ParticleSystemStopBehavior.StopEmitting);
yield return new WaitUntil(()=>particle.particleCount==0);
_pool.Return(particle);
}
}

16
UOP1_Project/Assets/Scripts/Pool/Example/ParticleFactorySO.cs


using UOP1.Factory;
[CreateAssetMenu(fileName = "NewParticleFactory", menuName = "Factory/Particle Factory")]
public class ParticleFactorySO : ComponentFactorySO<PoolableParticle>
public class ParticleFactorySO : FactorySO<ParticleSystem>
[SerializeField]
private PoolableParticle _prefab = default;
public override PoolableParticle Prefab
public override ParticleSystem Create()
get
{
return _prefab;
}
set
{
_prefab = value;
}
return new GameObject("ParticleSystem").AddComponent<ParticleSystem>();
}
}

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


using UOP1.Factory;
[CreateAssetMenu(fileName = "NewParticlePool", menuName = "Pool/Particle Pool")]
public class ParticlePoolSO : ComponentPoolSO<PoolableParticle>
public class ParticlePoolSO : ComponentPoolSO<ParticleSystem>
{
[SerializeField]
private ParticleFactorySO _factory;

public override IFactory<PoolableParticle> Factory
public override IFactory<ParticleSystem> Factory
{
get
{

21
UOP1_Project/Assets/Scripts/Pool/Example/PoolTester.cs


[SerializeField]
private ParticlePoolSO _pool = default;
private IEnumerator Start()
private void Start()
List<PoolableParticle> particles = _pool.Request(10) as List<PoolableParticle>;
foreach (PoolableParticle particle in particles)
List<ParticleSystem> particles = _pool.Request(10) as List<ParticleSystem>;
foreach (ParticleSystem particle in particles)
particle.transform.position = Random.insideUnitSphere * 5f;
particle.Play();
StartCoroutine(DoParticleBehaviour(particle));
yield return new WaitForSeconds(5f);
_pool.Return(particles);
}
private IEnumerator DoParticleBehaviour(ParticleSystem particle)
{
particle.transform.position = Random.insideUnitSphere * 5f;
particle.Play();
yield return new WaitForSeconds(particle.main.duration);
particle.Stop(true, ParticleSystemStopBehavior.StopEmitting);
yield return new WaitUntil(() => particle.particleCount == 0);
_pool.Return(particle);
}
}

1001
UOP1_Project/Assets/Scripts/Pool/Example/Assets/Particles.prefab
文件差异内容过多而无法显示
查看文件

7
UOP1_Project/Assets/Scripts/Pool/Example/Assets/Particles.prefab.meta


fileFormatVersion: 2
guid: 13e8a2294914b7744a0b9fe47b807ead
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

37
UOP1_Project/Assets/Scripts/Pool/Example/PoolableParticle.cs


using UOP1.Pool;
using System;
using System.Collections;
using UnityEngine;
public class PoolableParticle : MonoBehaviour, IPoolable
{
[SerializeField]
private ParticleSystem _particleSystem = default;
public void OnRequest()
{
gameObject.SetActive(true);
}
public void Play()
{
_particleSystem.Play();
}
public void OnReturn(Action onReturned)
{
StartCoroutine(DoReturn(onReturned));
}
IEnumerator DoReturn(Action onReturned)
{
if (_particleSystem.isPlaying)
{
yield return new WaitForSeconds(_particleSystem.main.duration - (_particleSystem.time % _particleSystem.main.duration));
_particleSystem.Stop();
}
yield return new WaitUntil(() => _particleSystem.particleCount == 0);
onReturned.Invoke();
gameObject.SetActive(false);
}
}

11
UOP1_Project/Assets/Scripts/Pool/Example/PoolableParticle.cs.meta


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