| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | namespace DCL |
| | 4 | | { |
| | 5 | | public class ReleaseParticlesOnFinish : MonoBehaviour |
| | 6 | | { |
| | 7 | | private const int INDEX_AMOUNT = 10; |
| | 8 | |
|
| | 9 | | [SerializeField] private ParticleSystem particles; |
| | 10 | | private PoolableObject poolable = null; |
| | 11 | | private int index; |
| | 12 | | private bool initialized; |
| | 13 | |
|
| | 14 | |
|
| | 15 | | public void Initialize(PoolableObject poolableObject) |
| | 16 | | { |
| 0 | 17 | | if (particles == null) |
| | 18 | | { |
| 0 | 19 | | Debug.LogError($"The script: {nameof(ReleaseParticlesOnFinish)}, " + |
| | 20 | | $"should never be initialized without particles attached: Aborting initialization"); |
| 0 | 21 | | Destroy(this); |
| 0 | 22 | | return; |
| | 23 | | } |
| | 24 | |
|
| 0 | 25 | | poolable = poolableObject; |
| 0 | 26 | | index = Random.Range(0, INDEX_AMOUNT); |
| 0 | 27 | | initialized = false; |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | public void FixedUpdate() |
| | 31 | | { |
| 0 | 32 | | if (initialized && Time.frameCount % INDEX_AMOUNT != index) |
| 0 | 33 | | return; |
| | 34 | |
|
| 0 | 35 | | if (particles == null || particles.IsAlive()) |
| 0 | 36 | | return; |
| | 37 | |
|
| 0 | 38 | | poolable.Release(); |
| 0 | 39 | | initialized = false; |
| 0 | 40 | | } |
| | 41 | | } |
| | 42 | | } |