2024-01-23 22:17:57 +08:00
|
|
|
using System;
|
2024-07-07 16:05:42 +08:00
|
|
|
using System.Collections.Generic;
|
2024-09-29 23:18:21 +08:00
|
|
|
using Coffee.UIParticleInternal;
|
2023-08-17 08:43:02 +08:00
|
|
|
using UnityEngine;
|
2022-06-15 09:24:31 +08:00
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
{
|
|
|
|
[ExecuteAlways]
|
2024-07-08 00:15:15 +08:00
|
|
|
public class UIParticleAttractor : MonoBehaviour, ISerializationCallbackReceiver
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
|
|
|
public enum Movement
|
|
|
|
{
|
|
|
|
Linear,
|
|
|
|
Smooth,
|
2023-08-17 08:43:02 +08:00
|
|
|
Sphere
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
2023-08-15 11:04:41 +08:00
|
|
|
public enum UpdateMode
|
|
|
|
{
|
|
|
|
Normal,
|
2023-08-17 08:43:02 +08:00
|
|
|
UnscaledTime
|
2023-08-15 11:04:41 +08:00
|
|
|
}
|
2022-06-15 09:24:31 +08:00
|
|
|
|
|
|
|
[SerializeField]
|
2024-07-08 00:15:15 +08:00
|
|
|
[HideInInspector]
|
|
|
|
private ParticleSystem m_ParticleSystem;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private List<ParticleSystem> m_ParticleSystems = new List<ParticleSystem>();
|
2022-06-15 09:24:31 +08:00
|
|
|
|
|
|
|
[Range(0.1f, 10f)]
|
|
|
|
[SerializeField]
|
|
|
|
private float m_DestinationRadius = 1;
|
|
|
|
|
|
|
|
[Range(0f, 0.95f)]
|
|
|
|
[SerializeField]
|
2023-08-17 08:43:02 +08:00
|
|
|
private float m_DelayRate;
|
2022-06-15 09:24:31 +08:00
|
|
|
|
|
|
|
[Range(0.001f, 100f)]
|
|
|
|
[SerializeField]
|
|
|
|
private float m_MaxSpeed = 1;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private Movement m_Movement;
|
|
|
|
|
2023-08-15 11:04:41 +08:00
|
|
|
[SerializeField]
|
|
|
|
private UpdateMode m_UpdateMode;
|
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
[SerializeField]
|
|
|
|
private UnityEvent m_OnAttracted;
|
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
private List<UIParticle> _uiParticles = new List<UIParticle>();
|
2023-08-17 08:43:02 +08:00
|
|
|
|
2023-08-14 15:47:16 +08:00
|
|
|
public float destinationRadius
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_DestinationRadius;
|
|
|
|
set => m_DestinationRadius = Mathf.Clamp(value, 0.1f, 10f);
|
2023-08-14 15:47:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
public float delay
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_DelayRate;
|
|
|
|
set => m_DelayRate = value;
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public float maxSpeed
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_MaxSpeed;
|
|
|
|
set => m_MaxSpeed = value;
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Movement movement
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_Movement;
|
|
|
|
set => m_Movement = value;
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
|
|
|
|
2023-08-15 11:04:41 +08:00
|
|
|
public UpdateMode updateMode
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_UpdateMode;
|
|
|
|
set => m_UpdateMode = value;
|
2023-08-15 11:04:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-14 15:47:16 +08:00
|
|
|
public UnityEvent onAttracted
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_OnAttracted;
|
|
|
|
set => m_OnAttracted = value;
|
2023-08-14 15:47:16 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 01:26:11 +08:00
|
|
|
/// <summary>
|
2024-07-07 16:05:42 +08:00
|
|
|
/// The target ParticleSystems to attract. Use <see cref="AddParticleSystem"/> and
|
|
|
|
/// <see cref="RemoveParticleSystem"/> to modify the list.
|
2024-01-24 01:26:11 +08:00
|
|
|
/// </summary>
|
2024-07-07 16:05:42 +08:00
|
|
|
public IReadOnlyList<ParticleSystem> particleSystems => m_ParticleSystems;
|
|
|
|
|
|
|
|
public void AddParticleSystem(ParticleSystem ps)
|
|
|
|
{
|
|
|
|
if (m_ParticleSystems == null)
|
|
|
|
{
|
|
|
|
m_ParticleSystems = new List<ParticleSystem>();
|
|
|
|
}
|
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
var i = m_ParticleSystems.IndexOf(ps);
|
|
|
|
if (0 <= i) return; // Already added: skip
|
|
|
|
|
|
|
|
m_ParticleSystems.Add(ps);
|
|
|
|
_uiParticles.Clear();
|
2024-07-07 16:05:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveParticleSystem(ParticleSystem ps)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
if (m_ParticleSystems == null)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
var i = m_ParticleSystems.IndexOf(ps);
|
|
|
|
if (i < 0) return; // Not found. skip
|
|
|
|
|
|
|
|
m_ParticleSystems.RemoveAt(i);
|
|
|
|
_uiParticles.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
UpgradeIfNeeded();
|
2023-03-12 19:33:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
2022-06-15 09:24:31 +08:00
|
|
|
UIParticleUpdater.Register(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
{
|
|
|
|
UIParticleUpdater.Unregister(this);
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:47:16 +08:00
|
|
|
private void OnDestroy()
|
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
_uiParticles = null;
|
|
|
|
m_ParticleSystems = null;
|
2023-08-14 15:47:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
internal void Attract()
|
|
|
|
{
|
2024-07-08 00:15:15 +08:00
|
|
|
// Collect UIParticle if needed (same size as m_ParticleSystems)
|
|
|
|
CollectUIParticlesIfNeeded();
|
2022-06-15 09:24:31 +08:00
|
|
|
|
2024-09-30 00:12:08 +08:00
|
|
|
for (var particleIndex = 0; particleIndex < m_ParticleSystems.Count; particleIndex++)
|
2024-07-07 16:05:42 +08:00
|
|
|
{
|
|
|
|
var particleSystem = m_ParticleSystems[particleIndex];
|
2024-07-08 00:15:15 +08:00
|
|
|
|
|
|
|
// Skip: The ParticleSystem is not active
|
2024-07-07 16:05:42 +08:00
|
|
|
if (particleSystem == null || !particleSystem.gameObject.activeInHierarchy) continue;
|
2022-06-15 09:24:31 +08:00
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
// Skip: No active particles
|
2024-07-07 16:05:42 +08:00
|
|
|
var count = particleSystem.particleCount;
|
|
|
|
if (count == 0) continue;
|
2022-06-15 09:24:31 +08:00
|
|
|
|
2024-07-07 16:05:42 +08:00
|
|
|
var particles = ParticleSystemExtensions.GetParticleArray(count);
|
|
|
|
particleSystem.GetParticles(particles, count);
|
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
var uiParticle = _uiParticles[particleIndex];
|
2024-09-30 00:12:08 +08:00
|
|
|
var dstPos = GetDestinationPosition(uiParticle, particleSystem);
|
2024-07-07 16:05:42 +08:00
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
// Attracted
|
|
|
|
var p = particles[i];
|
2024-09-30 00:12:08 +08:00
|
|
|
if (0f < p.remainingLifetime && Vector3.Distance(p.position, dstPos) < m_DestinationRadius)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
p.remainingLifetime = 0f;
|
|
|
|
particles[i] = p;
|
|
|
|
|
2024-09-30 00:12:08 +08:00
|
|
|
if (m_OnAttracted != null)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
try
|
|
|
|
{
|
2024-09-30 00:12:08 +08:00
|
|
|
m_OnAttracted.Invoke();
|
2024-07-07 16:05:42 +08:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Debug.LogException(e);
|
|
|
|
}
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2024-07-07 16:05:42 +08:00
|
|
|
|
|
|
|
continue;
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2023-08-14 15:47:16 +08:00
|
|
|
|
2024-07-07 16:05:42 +08:00
|
|
|
// Calc attracting time
|
2024-09-30 00:12:08 +08:00
|
|
|
var delayTime = p.startLifetime * m_DelayRate;
|
2024-07-07 16:05:42 +08:00
|
|
|
var duration = p.startLifetime - delayTime;
|
|
|
|
var time = Mathf.Max(0, p.startLifetime - p.remainingLifetime - delayTime);
|
2022-06-15 09:24:31 +08:00
|
|
|
|
2024-07-07 16:05:42 +08:00
|
|
|
// Delay
|
|
|
|
if (time <= 0) continue;
|
2022-06-15 09:24:31 +08:00
|
|
|
|
2024-07-07 16:05:42 +08:00
|
|
|
// Attract
|
2024-09-30 00:12:08 +08:00
|
|
|
p.position = GetAttractedPosition(p.position, dstPos, duration, time);
|
2024-07-07 16:05:42 +08:00
|
|
|
p.velocity *= 0.5f;
|
|
|
|
particles[i] = p;
|
|
|
|
}
|
2022-06-15 09:24:31 +08:00
|
|
|
|
2024-07-07 16:05:42 +08:00
|
|
|
particleSystem.SetParticles(particles, count);
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-07 16:05:42 +08:00
|
|
|
private Vector3 GetDestinationPosition(UIParticle uiParticle, ParticleSystem particleSystem)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
var isUI = uiParticle && uiParticle.enabled;
|
|
|
|
var psPos = particleSystem.transform.position;
|
2022-06-15 09:24:31 +08:00
|
|
|
var attractorPos = transform.position;
|
|
|
|
var dstPos = attractorPos;
|
2024-07-07 16:05:42 +08:00
|
|
|
var isLocalSpace = particleSystem.IsLocalSpace();
|
2023-08-14 22:01:46 +08:00
|
|
|
|
|
|
|
if (isLocalSpace)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
dstPos = particleSystem.transform.InverseTransformPoint(dstPos);
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2023-08-14 22:01:46 +08:00
|
|
|
|
|
|
|
if (isUI)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
var inverseScale = uiParticle.parentScale.Inverse();
|
|
|
|
var scale3d = uiParticle.scale3DForCalc;
|
2023-11-07 10:42:16 +08:00
|
|
|
dstPos = dstPos.GetScaled(inverseScale, scale3d.Inverse());
|
2023-08-14 22:01:46 +08:00
|
|
|
|
|
|
|
// Relative mode
|
2024-07-07 16:05:42 +08:00
|
|
|
if (uiParticle.positionMode == UIParticle.PositionMode.Relative)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2024-07-07 16:05:42 +08:00
|
|
|
var diff = uiParticle.transform.position - psPos;
|
2023-11-07 10:42:16 +08:00
|
|
|
diff.Scale(scale3d - inverseScale);
|
|
|
|
diff.Scale(scale3d.Inverse());
|
2023-08-14 22:01:46 +08:00
|
|
|
dstPos += diff;
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2023-08-14 22:01:46 +08:00
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
if (!Application.isPlaying && !isLocalSpace)
|
2022-06-15 09:24:31 +08:00
|
|
|
{
|
2023-11-07 10:42:16 +08:00
|
|
|
dstPos += psPos - psPos.GetScaled(inverseScale, scale3d.Inverse());
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2023-08-14 22:01:46 +08:00
|
|
|
#endif
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2023-08-14 15:47:16 +08:00
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
return dstPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Vector3 GetAttractedPosition(Vector3 current, Vector3 target, float duration, float time)
|
|
|
|
{
|
|
|
|
var speed = m_MaxSpeed;
|
2023-08-15 11:04:41 +08:00
|
|
|
switch (m_UpdateMode)
|
|
|
|
{
|
|
|
|
case UpdateMode.Normal:
|
|
|
|
speed *= 60 * Time.deltaTime;
|
|
|
|
break;
|
|
|
|
case UpdateMode.UnscaledTime:
|
|
|
|
speed *= 60 * Time.unscaledDeltaTime;
|
|
|
|
break;
|
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
switch (m_Movement)
|
|
|
|
{
|
|
|
|
case Movement.Linear:
|
|
|
|
speed /= duration;
|
|
|
|
break;
|
|
|
|
case Movement.Smooth:
|
|
|
|
target = Vector3.Lerp(current, target, time / duration);
|
|
|
|
break;
|
|
|
|
case Movement.Sphere:
|
|
|
|
target = Vector3.Slerp(current, target, time / duration);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Vector3.MoveTowards(current, target, speed);
|
|
|
|
}
|
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
private void CollectUIParticlesIfNeeded()
|
2023-03-12 19:33:26 +08:00
|
|
|
{
|
2024-07-08 00:15:15 +08:00
|
|
|
if (m_ParticleSystems.Count == 0 || _uiParticles.Count != 0) return;
|
|
|
|
|
|
|
|
// Expand capacity
|
|
|
|
if (_uiParticles.Capacity < m_ParticleSystems.Capacity)
|
2023-03-12 19:33:26 +08:00
|
|
|
{
|
2024-07-08 00:15:15 +08:00
|
|
|
_uiParticles.Capacity = m_ParticleSystems.Capacity;
|
2023-03-12 19:33:26 +08:00
|
|
|
}
|
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
// Find UIParticle that controls the ParticleSystem
|
|
|
|
for (var i = 0; i < m_ParticleSystems.Count; i++)
|
2023-03-12 19:33:26 +08:00
|
|
|
{
|
2024-07-08 00:15:15 +08:00
|
|
|
var ps = m_ParticleSystems[i];
|
|
|
|
if (ps == null)
|
|
|
|
{
|
|
|
|
_uiParticles.Add(null);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var uiParticle = ps.GetComponentInParent<UIParticle>(true);
|
|
|
|
_uiParticles.Add(uiParticle.particles.Contains(ps) ? uiParticle : null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
private void OnValidate()
|
|
|
|
{
|
|
|
|
_uiParticles.Clear();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
|
|
|
{
|
|
|
|
UpgradeIfNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpgradeIfNeeded()
|
|
|
|
{
|
|
|
|
// Multiple ParticleSystems support: from 'm_ParticleSystem' to 'm_ParticleSystems'
|
|
|
|
if (m_ParticleSystem != null)
|
|
|
|
{
|
|
|
|
if (!m_ParticleSystems.Contains(m_ParticleSystem))
|
|
|
|
{
|
|
|
|
m_ParticleSystems.Add(m_ParticleSystem);
|
|
|
|
}
|
2024-07-07 16:05:42 +08:00
|
|
|
|
2024-07-08 00:15:15 +08:00
|
|
|
m_ParticleSystem = null;
|
|
|
|
Debug.Log($"Upgraded!");
|
2023-03-12 19:33:26 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-15 09:24:31 +08:00
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|