309 lines
8.6 KiB
C#
309 lines
8.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace AlicizaX
|
|
{
|
|
[DisallowMultipleComponent]
|
|
public sealed class PoolAutoRecycleAfterSeconds : MonoBehaviour, IPoolAutoRecycle
|
|
{
|
|
[Min(0f)]
|
|
public float delaySeconds = 1f;
|
|
|
|
public bool TryGetAutoRecycleDelay(out float delay)
|
|
{
|
|
delay = delaySeconds;
|
|
return delay > 0f;
|
|
}
|
|
}
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class PoolAutoRecycleParticleStop : MonoBehaviour, IGameObjectPoolable
|
|
{
|
|
[SerializeField] private bool includeChildren = true;
|
|
private ParticleSystem[] _particleSystems;
|
|
private bool _subscribed;
|
|
|
|
public void OnPoolCreate()
|
|
{
|
|
_particleSystems = includeChildren
|
|
? GetComponentsInChildren<ParticleSystem>(true)
|
|
: GetComponents<ParticleSystem>();
|
|
}
|
|
|
|
public void OnPoolGet(in PoolSpawnContext context)
|
|
{
|
|
if (_particleSystems == null)
|
|
{
|
|
OnPoolCreate();
|
|
}
|
|
|
|
for (int i = 0; i < _particleSystems.Length; i++)
|
|
{
|
|
ParticleSystem particle = _particleSystems[i];
|
|
if (particle == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var main = particle.main;
|
|
main.stopAction = ParticleSystemStopAction.Callback;
|
|
particle.Clear(true);
|
|
particle.Play(true);
|
|
}
|
|
|
|
_subscribed = true;
|
|
}
|
|
|
|
public void OnPoolRelease()
|
|
{
|
|
_subscribed = false;
|
|
}
|
|
|
|
public void OnPoolDestroy()
|
|
{
|
|
_subscribed = false;
|
|
}
|
|
|
|
private void OnParticleSystemStopped()
|
|
{
|
|
if (!_subscribed || !AppServices.TryGet(out GameObjectPoolManager poolManager))
|
|
{
|
|
return;
|
|
}
|
|
|
|
poolManager.Release(gameObject);
|
|
}
|
|
}
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class PoolResetRigidbodyState : MonoBehaviour, IPoolResettablePhysics
|
|
{
|
|
[SerializeField] private bool includeChildren = true;
|
|
private Rigidbody[] _rigidbodies3D;
|
|
private Rigidbody2D[] _rigidbodies2D;
|
|
|
|
private void EnsureCache()
|
|
{
|
|
if (_rigidbodies3D == null)
|
|
{
|
|
_rigidbodies3D = includeChildren ? GetComponentsInChildren<Rigidbody>(true) : GetComponents<Rigidbody>();
|
|
}
|
|
|
|
if (_rigidbodies2D == null)
|
|
{
|
|
_rigidbodies2D = includeChildren ? GetComponentsInChildren<Rigidbody2D>(true) : GetComponents<Rigidbody2D>();
|
|
}
|
|
}
|
|
|
|
public void ResetPhysicsState()
|
|
{
|
|
EnsureCache();
|
|
|
|
for (int i = 0; i < _rigidbodies3D.Length; i++)
|
|
{
|
|
Rigidbody body = _rigidbodies3D[i];
|
|
if (body == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
body.velocity = Vector3.zero;
|
|
body.angularVelocity = Vector3.zero;
|
|
body.Sleep();
|
|
}
|
|
|
|
for (int i = 0; i < _rigidbodies2D.Length; i++)
|
|
{
|
|
Rigidbody2D body = _rigidbodies2D[i];
|
|
if (body == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
body.velocity = Vector2.zero;
|
|
body.angularVelocity = 0f;
|
|
body.Sleep();
|
|
}
|
|
}
|
|
}
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class PoolResetParticleSystems : MonoBehaviour, IPoolResettableVisual
|
|
{
|
|
[SerializeField] private bool includeChildren = true;
|
|
private ParticleSystem[] _particleSystems;
|
|
|
|
private void EnsureCache()
|
|
{
|
|
if (_particleSystems == null)
|
|
{
|
|
_particleSystems = includeChildren
|
|
? GetComponentsInChildren<ParticleSystem>(true)
|
|
: GetComponents<ParticleSystem>();
|
|
}
|
|
}
|
|
|
|
public void ResetVisualState()
|
|
{
|
|
EnsureCache();
|
|
|
|
for (int i = 0; i < _particleSystems.Length; i++)
|
|
{
|
|
ParticleSystem particle = _particleSystems[i];
|
|
if (particle == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
particle.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
|
|
particle.Clear(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class PoolResetTrailRenderers : MonoBehaviour, IPoolResettableVisual
|
|
{
|
|
[SerializeField] private bool includeChildren = true;
|
|
private TrailRenderer[] _trailRenderers;
|
|
|
|
private void EnsureCache()
|
|
{
|
|
if (_trailRenderers == null)
|
|
{
|
|
_trailRenderers = includeChildren
|
|
? GetComponentsInChildren<TrailRenderer>(true)
|
|
: GetComponents<TrailRenderer>();
|
|
}
|
|
}
|
|
|
|
public void ResetVisualState()
|
|
{
|
|
EnsureCache();
|
|
|
|
for (int i = 0; i < _trailRenderers.Length; i++)
|
|
{
|
|
TrailRenderer trail = _trailRenderers[i];
|
|
if (trail == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
trail.Clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class PoolResetAnimatorState : MonoBehaviour, IPoolResettableAnimation
|
|
{
|
|
[SerializeField] private bool includeChildren = true;
|
|
private Animator[] _animators;
|
|
|
|
private void EnsureCache()
|
|
{
|
|
if (_animators == null)
|
|
{
|
|
_animators = includeChildren ? GetComponentsInChildren<Animator>(true) : GetComponents<Animator>();
|
|
}
|
|
}
|
|
|
|
public void ResetAnimationState()
|
|
{
|
|
EnsureCache();
|
|
|
|
for (int i = 0; i < _animators.Length; i++)
|
|
{
|
|
Animator animator = _animators[i];
|
|
if (animator == null || !animator.isActiveAndEnabled)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
animator.Rebind();
|
|
animator.Update(0f);
|
|
}
|
|
}
|
|
}
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class PoolSleepableGameObjectGroup : MonoBehaviour, IPoolSleepable
|
|
{
|
|
[SerializeField] private List<Behaviour> disableOnSleep = new List<Behaviour>();
|
|
[SerializeField] private List<Renderer> hideOnSleep = new List<Renderer>();
|
|
[SerializeField] private List<Collider> disableCollider3D = new List<Collider>();
|
|
[SerializeField] private List<Collider2D> disableCollider2D = new List<Collider2D>();
|
|
|
|
public void EnterSleep()
|
|
{
|
|
for (int i = 0; i < disableOnSleep.Count; i++)
|
|
{
|
|
if (disableOnSleep[i] != null)
|
|
{
|
|
disableOnSleep[i].enabled = false;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < hideOnSleep.Count; i++)
|
|
{
|
|
if (hideOnSleep[i] != null)
|
|
{
|
|
hideOnSleep[i].enabled = false;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < disableCollider3D.Count; i++)
|
|
{
|
|
if (disableCollider3D[i] != null)
|
|
{
|
|
disableCollider3D[i].enabled = false;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < disableCollider2D.Count; i++)
|
|
{
|
|
if (disableCollider2D[i] != null)
|
|
{
|
|
disableCollider2D[i].enabled = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ExitSleep(in PoolSpawnContext context)
|
|
{
|
|
for (int i = 0; i < disableOnSleep.Count; i++)
|
|
{
|
|
if (disableOnSleep[i] != null)
|
|
{
|
|
disableOnSleep[i].enabled = true;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < hideOnSleep.Count; i++)
|
|
{
|
|
if (hideOnSleep[i] != null)
|
|
{
|
|
hideOnSleep[i].enabled = true;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < disableCollider3D.Count; i++)
|
|
{
|
|
if (disableCollider3D[i] != null)
|
|
{
|
|
disableCollider3D[i].enabled = true;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < disableCollider2D.Count; i++)
|
|
{
|
|
if (disableCollider2D[i] != null)
|
|
{
|
|
disableCollider2D[i].enabled = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|