2022-06-15 09:24:31 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Coffee.UIParticleExtensions;
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
|
{
|
|
|
|
|
[ExecuteAlways]
|
|
|
|
|
public class UIParticleAttractor : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public enum Movement
|
|
|
|
|
{
|
|
|
|
|
Linear,
|
|
|
|
|
Smooth,
|
|
|
|
|
Sphere,
|
|
|
|
|
}
|
2023-08-15 11:04:41 +08:00
|
|
|
|
|
|
|
|
|
public enum UpdateMode
|
|
|
|
|
{
|
|
|
|
|
Normal,
|
|
|
|
|
UnscaledTime,
|
|
|
|
|
}
|
2022-06-15 09:24:31 +08:00
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private ParticleSystem m_ParticleSystem;
|
|
|
|
|
|
|
|
|
|
[Range(0.1f, 10f)]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private float m_DestinationRadius = 1;
|
|
|
|
|
|
|
|
|
|
[Range(0f, 0.95f)]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private float m_DelayRate = 0;
|
|
|
|
|
|
|
|
|
|
[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;
|
|
|
|
|
|
2023-08-14 15:47:16 +08:00
|
|
|
|
public float destinationRadius
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_DestinationRadius;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_DestinationRadius = Mathf.Clamp(value, 0.1f, 10f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
|
public float delay
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_DelayRate;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_DelayRate = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float maxSpeed
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_MaxSpeed;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_MaxSpeed = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Movement movement
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_Movement;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_Movement = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 11:04:41 +08:00
|
|
|
|
public UpdateMode updateMode
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_UpdateMode;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_UpdateMode = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 15:47:16 +08:00
|
|
|
|
public UnityEvent onAttracted
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_OnAttracted;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_OnAttracted = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 19:33:26 +08:00
|
|
|
|
public ParticleSystem particleSystem
|
2022-06-15 09:24:31 +08:00
|
|
|
|
{
|
2023-03-12 19:33:26 +08:00
|
|
|
|
get
|
2022-06-15 09:24:31 +08:00
|
|
|
|
{
|
2023-03-12 19:33:26 +08:00
|
|
|
|
return m_ParticleSystem;
|
2022-06-15 09:24:31 +08:00
|
|
|
|
}
|
2023-03-12 19:33:26 +08:00
|
|
|
|
set
|
2022-06-15 09:24:31 +08:00
|
|
|
|
{
|
2023-03-12 19:33:26 +08:00
|
|
|
|
m_ParticleSystem = value;
|
2023-08-14 15:47:16 +08:00
|
|
|
|
ApplyParticleSystem();
|
2022-06-15 09:24:31 +08:00
|
|
|
|
}
|
2023-03-12 19:33:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UIParticle _uiParticle;
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
2023-08-14 15:47:16 +08:00
|
|
|
|
ApplyParticleSystem();
|
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()
|
|
|
|
|
{
|
|
|
|
|
_uiParticle = null;
|
|
|
|
|
m_ParticleSystem = null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
|
internal void Attract()
|
|
|
|
|
{
|
|
|
|
|
if (m_ParticleSystem == null) return;
|
|
|
|
|
|
|
|
|
|
var count = m_ParticleSystem.particleCount;
|
|
|
|
|
if (count == 0) return;
|
|
|
|
|
|
|
|
|
|
var particles = ParticleSystemExtensions.GetParticleArray(count);
|
|
|
|
|
m_ParticleSystem.GetParticles(particles, count);
|
|
|
|
|
|
|
|
|
|
var dstPos = GetDestinationPosition();
|
|
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
// Attracted
|
|
|
|
|
var p = particles[i];
|
|
|
|
|
if (0f < p.remainingLifetime && Vector3.Distance(p.position, dstPos) < m_DestinationRadius)
|
|
|
|
|
{
|
|
|
|
|
p.remainingLifetime = 0f;
|
|
|
|
|
particles[i] = p;
|
|
|
|
|
|
|
|
|
|
if (m_OnAttracted != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
m_OnAttracted.Invoke();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-14 15:47:16 +08:00
|
|
|
|
|
2022-06-15 09:24:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calc attracting time
|
|
|
|
|
var delayTime = p.startLifetime * m_DelayRate;
|
|
|
|
|
var duration = p.startLifetime - delayTime;
|
|
|
|
|
var time = Mathf.Max(0, p.startLifetime - p.remainingLifetime - delayTime);
|
|
|
|
|
|
|
|
|
|
// Delay
|
|
|
|
|
if (time <= 0) continue;
|
|
|
|
|
|
|
|
|
|
// Attract
|
|
|
|
|
p.position = GetAttractedPosition(p.position, dstPos, duration, time);
|
|
|
|
|
p.velocity *= 0.5f;
|
|
|
|
|
particles[i] = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_ParticleSystem.SetParticles(particles, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector3 GetDestinationPosition()
|
|
|
|
|
{
|
|
|
|
|
var isUI = _uiParticle && _uiParticle.enabled;
|
|
|
|
|
var psPos = m_ParticleSystem.transform.position;
|
|
|
|
|
var attractorPos = transform.position;
|
|
|
|
|
var dstPos = attractorPos;
|
2023-08-14 22:01:46 +08:00
|
|
|
|
var isLocalSpace = m_ParticleSystem.main.simulationSpace == ParticleSystemSimulationSpace.Local;
|
|
|
|
|
|
|
|
|
|
if (isLocalSpace)
|
2022-06-15 09:24:31 +08:00
|
|
|
|
{
|
|
|
|
|
dstPos = m_ParticleSystem.transform.InverseTransformPoint(dstPos);
|
|
|
|
|
}
|
2023-08-14 22:01:46 +08:00
|
|
|
|
|
|
|
|
|
if (isUI)
|
2022-06-15 09:24:31 +08:00
|
|
|
|
{
|
2023-08-14 22:01:46 +08:00
|
|
|
|
var inverseScale = _uiParticle.parentScale.Inverse();
|
|
|
|
|
dstPos = dstPos.GetScaled(inverseScale, _uiParticle.scale3D.Inverse());
|
|
|
|
|
|
|
|
|
|
// Relative mode
|
|
|
|
|
if (!_uiParticle.absoluteMode)
|
2022-06-15 09:24:31 +08:00
|
|
|
|
{
|
2023-08-14 22:01:46 +08:00
|
|
|
|
var diff = _uiParticle.transform.position - psPos;
|
|
|
|
|
diff.Scale(_uiParticle.scale3D - inverseScale);
|
|
|
|
|
diff.Scale(_uiParticle.scale3D.Inverse());
|
|
|
|
|
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-08-14 22:01:46 +08:00
|
|
|
|
dstPos += psPos - psPos.GetScaled(inverseScale, _uiParticle.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;
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 15:47:16 +08:00
|
|
|
|
private void ApplyParticleSystem()
|
2023-03-12 19:33:26 +08:00
|
|
|
|
{
|
2023-08-14 15:47:16 +08:00
|
|
|
|
_uiParticle = null;
|
2023-03-12 19:33:26 +08:00
|
|
|
|
if (m_ParticleSystem == null)
|
|
|
|
|
{
|
2023-08-14 15:47:16 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (Application.isPlaying)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("No particle system attached to particle attractor script", this);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2023-03-12 19:33:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_uiParticle = m_ParticleSystem.GetComponentInParent<UIParticle>();
|
|
|
|
|
if (_uiParticle && !_uiParticle.particles.Contains(m_ParticleSystem))
|
|
|
|
|
{
|
|
|
|
|
_uiParticle = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-15 09:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|