2020-11-10 00:42:28 +08:00
|
|
|
|
#if UNITY_2019_3_11 || UNITY_2019_3_12 || UNITY_2019_3_13 || UNITY_2019_3_14 || UNITY_2019_3_15 || UNITY_2019_4_OR_NEWER
|
|
|
|
|
#define SERIALIZE_FIELD_MASKABLE
|
|
|
|
|
#endif
|
2020-08-28 13:38:13 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2020-09-28 20:35:40 +08:00
|
|
|
|
using Coffee.UIParticleExtensions;
|
2018-06-22 18:48:14 +08:00
|
|
|
|
using UnityEngine;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
using UnityEngine.Rendering;
|
2023-08-15 20:56:09 +08:00
|
|
|
|
using UnityEngine.Serialization;
|
2018-06-22 18:48:14 +08:00
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
[assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")]
|
|
|
|
|
|
2018-06-22 18:48:14 +08:00
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
|
{
|
2020-02-12 20:38:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Render maskable and sortable particle effect ,without Camera, RenderTexture or Canvas.
|
|
|
|
|
/// </summary>
|
2022-02-18 03:57:27 +08:00
|
|
|
|
[ExecuteAlways]
|
2020-08-28 13:38:13 +08:00
|
|
|
|
[RequireComponent(typeof(RectTransform))]
|
2020-08-11 23:09:55 +08:00
|
|
|
|
[RequireComponent(typeof(CanvasRenderer))]
|
2020-08-29 11:10:31 +08:00
|
|
|
|
public class UIParticle : MaskableGraphic
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2022-06-11 22:10:17 +08:00
|
|
|
|
public enum MeshSharing
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
Auto,
|
|
|
|
|
Primary,
|
|
|
|
|
PrimarySimulator,
|
2023-08-17 08:43:02 +08:00
|
|
|
|
Replica
|
2022-06-11 22:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
[HideInInspector]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
internal bool m_IsTrail;
|
2020-02-12 20:38:06 +08:00
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
[Tooltip("Particle effect scale")]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private Vector3 m_Scale3D = new Vector3(10, 10, 10);
|
2020-10-04 22:26:53 +08:00
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
[Tooltip("Animatable material properties.\n" +
|
|
|
|
|
"If you want to change the material properties of the ParticleSystem in Animation, enable it.")]
|
2022-06-08 11:54:11 +08:00
|
|
|
|
[SerializeField]
|
2020-08-20 03:42:16 +08:00
|
|
|
|
internal AnimatableProperty[] m_AnimatableProperties = new AnimatableProperty[0];
|
2020-02-12 20:38:06 +08:00
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
[Tooltip("Particles")]
|
|
|
|
|
[SerializeField]
|
2020-08-28 13:38:13 +08:00
|
|
|
|
private List<ParticleSystem> m_Particles = new List<ParticleSystem>();
|
2020-02-12 20:38:06 +08:00
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
[Tooltip("Mesh sharing.\n" +
|
|
|
|
|
"None: disable mesh sharing.\n" +
|
|
|
|
|
"Auto: automatically select Primary/Replica.\n" +
|
|
|
|
|
"Primary: provides particle simulation results to the same group.\n" +
|
|
|
|
|
"Primary Simulator: Primary, but do not render the particle (simulation only).\n" +
|
|
|
|
|
"Replica: render simulation results provided by the primary.")]
|
2022-06-11 22:10:17 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private MeshSharing m_MeshSharing = MeshSharing.None;
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
[Tooltip("Mesh sharing group ID.\n" +
|
|
|
|
|
"If non-zero is specified, particle simulation results are shared within the group.")]
|
2022-06-11 22:10:17 +08:00
|
|
|
|
[SerializeField]
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private int m_GroupId;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
|
2022-06-14 12:27:54 +08:00
|
|
|
|
[SerializeField]
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private int m_GroupMaxId;
|
2022-06-14 12:27:54 +08:00
|
|
|
|
|
2022-06-25 01:04:23 +08:00
|
|
|
|
[SerializeField]
|
2023-08-17 08:43:02 +08:00
|
|
|
|
[Tooltip("Particle position mode.\n" +
|
|
|
|
|
"Absolute Mode: The particles will be emitted from the ParticleSystem position.\n" +
|
|
|
|
|
" Move the UIParticle or ParticleSystem to move the particle.\n" +
|
|
|
|
|
"Relative Mode: The particles will be emitted from the scaled ParticleSystem position.\n" +
|
|
|
|
|
" Move the UIParticle to move the particle.")]
|
2022-06-25 01:04:23 +08:00
|
|
|
|
private bool m_AbsoluteMode = false;
|
|
|
|
|
|
2023-08-15 20:56:09 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This field uses the inverted value as "AutoScaling".
|
|
|
|
|
/// </summary>
|
|
|
|
|
[FormerlySerializedAs("m_IgnoreParent")]
|
2023-08-17 08:43:02 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private bool m_IgnoreCanvasScaler;
|
2020-11-20 12:11:08 +08:00
|
|
|
|
|
2020-11-10 00:42:28 +08:00
|
|
|
|
#if !SERIALIZE_FIELD_MASKABLE
|
2023-08-17 08:43:02 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private bool m_Maskable = true;
|
2020-10-28 21:19:15 +08:00
|
|
|
|
#endif
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private readonly List<UIParticleRenderer> _renderers = new List<UIParticleRenderer>();
|
2022-06-14 12:27:54 +08:00
|
|
|
|
private int _groupId;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private Camera _orthoCamera;
|
|
|
|
|
private DrivenRectTransformTracker _tracker;
|
2020-08-28 13:38:13 +08:00
|
|
|
|
|
2020-08-20 03:42:16 +08:00
|
|
|
|
/// <summary>
|
2023-08-17 08:43:02 +08:00
|
|
|
|
/// Should this graphic be considered a target for ray-casting?
|
2020-08-20 03:42:16 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public override bool raycastTarget
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2020-08-20 03:42:16 +08:00
|
|
|
|
get { return false; }
|
2020-08-28 13:38:13 +08:00
|
|
|
|
set { }
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
2020-02-12 20:38:06 +08:00
|
|
|
|
|
2022-06-11 22:10:17 +08:00
|
|
|
|
/// <summary>
|
2023-08-17 08:43:02 +08:00
|
|
|
|
/// Mesh sharing.
|
|
|
|
|
/// None: disable mesh sharing.
|
2023-08-14 14:32:26 +08:00
|
|
|
|
/// Auto: automatically select Primary/Replica.
|
2022-06-11 22:10:17 +08:00
|
|
|
|
/// Primary: provides particle simulation results to the same group.
|
|
|
|
|
/// Primary Simulator: Primary, but do not render the particle (simulation only).
|
2023-08-14 14:32:26 +08:00
|
|
|
|
/// Replica: render simulation results provided by the primary.
|
2022-06-11 22:10:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public MeshSharing meshSharing
|
|
|
|
|
{
|
|
|
|
|
get { return m_MeshSharing; }
|
|
|
|
|
set { m_MeshSharing = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-08-17 08:43:02 +08:00
|
|
|
|
/// Mesh sharing group ID.
|
|
|
|
|
/// If non-zero is specified, particle simulation results are shared within the group.
|
2022-06-11 22:10:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public int groupId
|
|
|
|
|
{
|
2022-06-14 12:27:54 +08:00
|
|
|
|
get { return _groupId; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (m_GroupId == value) return;
|
|
|
|
|
m_GroupId = value;
|
|
|
|
|
if (m_GroupId != m_GroupMaxId)
|
2023-08-17 08:43:02 +08:00
|
|
|
|
{
|
2022-06-14 12:27:54 +08:00
|
|
|
|
ResetGroupId();
|
2023-08-17 08:43:02 +08:00
|
|
|
|
}
|
2022-06-14 12:27:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int groupMaxId
|
|
|
|
|
{
|
|
|
|
|
get { return m_GroupMaxId; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (m_GroupMaxId == value) return;
|
|
|
|
|
m_GroupMaxId = value;
|
|
|
|
|
ResetGroupId();
|
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-25 01:04:23 +08:00
|
|
|
|
/// <summary>
|
2023-08-17 08:43:02 +08:00
|
|
|
|
/// Particle position mode.
|
|
|
|
|
/// Absolute Mode: The particles will be emitted from the ParticleSystem position.
|
|
|
|
|
/// Move the UIParticle or ParticleSystem to move the particle.
|
|
|
|
|
/// Relative Mode: The particles will be emitted from the scaled ParticleSystem position.
|
|
|
|
|
/// Move the UIParticle to move the particle.
|
2022-06-25 01:04:23 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public bool absoluteMode
|
|
|
|
|
{
|
|
|
|
|
get { return m_AbsoluteMode; }
|
|
|
|
|
set { m_AbsoluteMode = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Transform.lossyScale (=world scale) is automatically set to (1, 1, 1).
|
|
|
|
|
/// It prevents the root-Canvas scale from affecting the hierarchy-scaled ParticleSystem.
|
|
|
|
|
/// Note: This option works in reverse of ’IgnoreCanvasScaler’ option in v3.x.
|
|
|
|
|
/// </summary>
|
2023-08-15 20:56:09 +08:00
|
|
|
|
public bool autoScaling
|
|
|
|
|
{
|
|
|
|
|
get { return !m_IgnoreCanvasScaler; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (m_IgnoreCanvasScaler != value) return;
|
|
|
|
|
m_IgnoreCanvasScaler = !value;
|
|
|
|
|
UpdateTracker();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 22:10:17 +08:00
|
|
|
|
internal bool useMeshSharing
|
|
|
|
|
{
|
|
|
|
|
get { return m_MeshSharing != MeshSharing.None; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool isPrimary
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_MeshSharing == MeshSharing.Primary
|
|
|
|
|
|| m_MeshSharing == MeshSharing.PrimarySimulator;
|
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool canSimulate
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_MeshSharing == MeshSharing.None
|
|
|
|
|
|| m_MeshSharing == MeshSharing.Auto
|
|
|
|
|
|| m_MeshSharing == MeshSharing.Primary
|
|
|
|
|
|| m_MeshSharing == MeshSharing.PrimarySimulator;
|
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool canRender
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_MeshSharing == MeshSharing.None
|
|
|
|
|
|| m_MeshSharing == MeshSharing.Auto
|
|
|
|
|
|| m_MeshSharing == MeshSharing.Primary
|
|
|
|
|
|| m_MeshSharing == MeshSharing.Replica;
|
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Particle effect scale.
|
|
|
|
|
/// </summary>
|
2020-08-12 22:19:08 +08:00
|
|
|
|
public float scale
|
|
|
|
|
{
|
2020-10-04 22:26:53 +08:00
|
|
|
|
get { return m_Scale3D.x; }
|
2022-06-08 11:54:11 +08:00
|
|
|
|
set { m_Scale3D = new Vector3(value, value, value); }
|
2020-10-04 22:26:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Particle effect scale.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Vector3 scale3D
|
|
|
|
|
{
|
|
|
|
|
get { return m_Scale3D; }
|
2022-06-08 11:54:11 +08:00
|
|
|
|
set { m_Scale3D = value; }
|
2020-02-12 20:38:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
public List<ParticleSystem> particles
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2020-08-28 13:38:13 +08:00
|
|
|
|
get { return m_Particles; }
|
2020-02-12 20:38:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all base materials to render.
|
|
|
|
|
/// </summary>
|
2020-08-28 13:38:13 +08:00
|
|
|
|
public IEnumerable<Material> materials
|
2020-08-12 22:19:08 +08:00
|
|
|
|
{
|
2022-06-08 11:54:11 +08:00
|
|
|
|
get
|
2020-08-28 13:38:13 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < _renderers.Count; i++)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var r = _renderers[i];
|
|
|
|
|
if (!r || !r.material) continue;
|
|
|
|
|
yield return r.material;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
2020-08-28 13:38:13 +08:00
|
|
|
|
}
|
2020-08-12 22:19:08 +08:00
|
|
|
|
}
|
2020-02-12 20:38:06 +08:00
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
public override Material materialForRendering
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2022-06-08 11:54:11 +08:00
|
|
|
|
get { return null; }
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
2020-02-12 20:38:06 +08:00
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Paused.
|
|
|
|
|
/// </summary>
|
2023-08-17 08:43:02 +08:00
|
|
|
|
public bool isPaused { get; private set; }
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
2023-08-15 20:56:09 +08:00
|
|
|
|
public Vector3 parentScale { get; private set; }
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
protected override void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
#if !SERIALIZE_FIELD_MASKABLE
|
|
|
|
|
maskable = m_Maskable;
|
|
|
|
|
#endif
|
|
|
|
|
ResetGroupId();
|
|
|
|
|
UpdateTracker();
|
|
|
|
|
UIParticleUpdater.Register(this);
|
|
|
|
|
RegisterDirtyMaterialCallback(UpdateRendererMaterial);
|
|
|
|
|
|
|
|
|
|
if (0 < particles.Count)
|
|
|
|
|
{
|
|
|
|
|
RefreshParticles(particles);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RefreshParticles();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.OnEnable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the behaviour becomes disabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
UpdateTracker();
|
|
|
|
|
UIParticleUpdater.Unregister(this);
|
|
|
|
|
_renderers.ForEach(r => r.Reset());
|
|
|
|
|
UnregisterDirtyMaterialCallback(UpdateRendererMaterial);
|
|
|
|
|
|
|
|
|
|
base.OnDisable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Callback for when properties have been changed by animation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void OnDidApplyAnimationProperties()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
protected override void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
base.OnValidate();
|
|
|
|
|
UpdateTracker();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
public void Play()
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2022-06-08 11:54:11 +08:00
|
|
|
|
particles.Exec(p => p.Simulate(0, false, true));
|
|
|
|
|
isPaused = false;
|
2020-02-12 20:38:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
public void Pause()
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-08-28 13:38:13 +08:00
|
|
|
|
particles.Exec(p => p.Pause());
|
2022-06-08 11:54:11 +08:00
|
|
|
|
isPaused = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Resume()
|
|
|
|
|
{
|
|
|
|
|
isPaused = false;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
public void Stop()
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-08-28 13:38:13 +08:00
|
|
|
|
particles.Exec(p => p.Stop());
|
2022-06-08 11:54:11 +08:00
|
|
|
|
isPaused = true;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
|
2023-08-15 08:52:16 +08:00
|
|
|
|
public void StartEmission()
|
|
|
|
|
{
|
|
|
|
|
particles.Exec(p =>
|
|
|
|
|
{
|
|
|
|
|
var emission = p.emission;
|
|
|
|
|
emission.enabled = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
|
2023-08-15 08:52:16 +08:00
|
|
|
|
public void StopEmission()
|
|
|
|
|
{
|
|
|
|
|
particles.Exec(p =>
|
|
|
|
|
{
|
|
|
|
|
var emission = p.emission;
|
|
|
|
|
emission.enabled = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
2021-02-23 12:30:46 +08:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
particles.Exec(p => p.Clear());
|
2022-06-08 11:54:11 +08:00
|
|
|
|
isPaused = true;
|
2021-02-23 12:30:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 13:15:30 +08:00
|
|
|
|
public void SetParticleSystemInstance(GameObject instance)
|
|
|
|
|
{
|
|
|
|
|
SetParticleSystemInstance(instance, true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 00:02:59 +08:00
|
|
|
|
public void SetParticleSystemInstance(GameObject instance, bool destroyOldParticles)
|
2020-09-02 13:15:30 +08:00
|
|
|
|
{
|
|
|
|
|
if (!instance) return;
|
|
|
|
|
|
|
|
|
|
foreach (Transform child in transform)
|
|
|
|
|
{
|
|
|
|
|
var go = child.gameObject;
|
|
|
|
|
go.SetActive(false);
|
2023-08-17 08:43:02 +08:00
|
|
|
|
if (destroyOldParticles)
|
|
|
|
|
{
|
|
|
|
|
Misc.Destroy(go);
|
|
|
|
|
}
|
2020-09-02 13:15:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tr = instance.transform;
|
|
|
|
|
tr.SetParent(transform, false);
|
|
|
|
|
tr.localPosition = Vector3.zero;
|
|
|
|
|
|
2020-09-03 00:02:59 +08:00
|
|
|
|
RefreshParticles(instance);
|
2020-09-02 13:15:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetParticleSystemPrefab(GameObject prefab)
|
|
|
|
|
{
|
|
|
|
|
if (!prefab) return;
|
|
|
|
|
|
|
|
|
|
SetParticleSystemInstance(Instantiate(prefab.gameObject), true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
public void RefreshParticles()
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2020-09-03 00:02:59 +08:00
|
|
|
|
RefreshParticles(gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 12:35:19 +08:00
|
|
|
|
private void RefreshParticles(GameObject root)
|
2020-09-03 00:02:59 +08:00
|
|
|
|
{
|
|
|
|
|
if (!root) return;
|
|
|
|
|
root.GetComponentsInChildren(particles);
|
2021-02-01 23:38:19 +08:00
|
|
|
|
particles.RemoveAll(x => x.GetComponentInParent<UIParticle>() != this);
|
2020-08-28 13:38:13 +08:00
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < particles.Count; i++)
|
2020-08-28 15:50:13 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var ps = particles[i];
|
2020-08-28 15:50:13 +08:00
|
|
|
|
var tsa = ps.textureSheetAnimation;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
if (tsa.mode == ParticleSystemAnimationMode.Sprites && tsa.uvChannelMask == 0)
|
2023-08-17 08:43:02 +08:00
|
|
|
|
{
|
2020-08-28 15:50:13 +08:00
|
|
|
|
tsa.uvChannelMask = UVChannelFlags.UV0;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
}
|
2020-08-28 15:50:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
RefreshParticles(particles);
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
public void RefreshParticles(List<ParticleSystem> particles)
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2023-08-14 14:42:25 +08:00
|
|
|
|
// #246: Nullptr exceptions when using nested UIParticle components in hierarchy
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_renderers.Clear();
|
2023-08-14 14:42:25 +08:00
|
|
|
|
foreach (Transform child in transform)
|
|
|
|
|
{
|
|
|
|
|
var uiParticleRenderer = child.GetComponent<UIParticleRenderer>();
|
|
|
|
|
|
|
|
|
|
if (uiParticleRenderer != null)
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_renderers.Add(uiParticleRenderer);
|
2023-08-14 14:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-28 13:38:13 +08:00
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < _renderers.Count; i++)
|
2023-08-14 14:44:04 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_renderers[i].Reset(i);
|
2023-08-14 14:44:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
|
var j = 0;
|
|
|
|
|
for (var i = 0; i < particles.Count; i++)
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var ps = particles[i];
|
|
|
|
|
if (!ps) continue;
|
|
|
|
|
GetRenderer(j++).Set(this, ps, false);
|
|
|
|
|
if (ps.trails.enabled)
|
2020-08-20 03:42:16 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
GetRenderer(j++).Set(this, ps, true);
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
2020-02-12 20:38:06 +08:00
|
|
|
|
}
|
2020-08-20 03:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 21:32:14 +08:00
|
|
|
|
internal void UpdateTransformScale()
|
2020-09-14 17:29:16 +08:00
|
|
|
|
{
|
2023-08-15 20:56:09 +08:00
|
|
|
|
parentScale = transform.parent.lossyScale;
|
|
|
|
|
if (!autoScaling) return;
|
|
|
|
|
|
|
|
|
|
var newScale = parentScale.Inverse();
|
2022-06-08 11:54:11 +08:00
|
|
|
|
if (transform.localScale != newScale)
|
2020-09-14 17:29:16 +08:00
|
|
|
|
{
|
2022-06-08 11:54:11 +08:00
|
|
|
|
transform.localScale = newScale;
|
2020-09-14 17:29:16 +08:00
|
|
|
|
}
|
2022-06-11 21:32:14 +08:00
|
|
|
|
}
|
2020-08-29 02:55:46 +08:00
|
|
|
|
|
2022-06-11 21:32:14 +08:00
|
|
|
|
internal void UpdateRenderers()
|
|
|
|
|
{
|
2022-06-17 12:35:19 +08:00
|
|
|
|
if (!isActiveAndEnabled) return;
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < _renderers.Count; i++)
|
2022-07-01 14:55:59 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var r = _renderers[i];
|
|
|
|
|
if (!r)
|
2023-04-28 07:17:24 +08:00
|
|
|
|
{
|
|
|
|
|
RefreshParticles(particles);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-01 14:55:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
var bakeCamera = GetBakeCamera();
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < _renderers.Count; i++)
|
2020-08-29 02:55:46 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var r = _renderers[i];
|
|
|
|
|
if (!r) continue;
|
|
|
|
|
r.UpdateMesh(bakeCamera);
|
2020-08-29 02:55:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 21:43:23 +08:00
|
|
|
|
internal void UpdateParticleCount()
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < _renderers.Count; i++)
|
2022-06-16 21:43:23 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var r = _renderers[i];
|
|
|
|
|
if (!r) continue;
|
|
|
|
|
r.UpdateParticleCount();
|
2022-06-16 21:43:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 12:27:54 +08:00
|
|
|
|
internal void ResetGroupId()
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_groupId = m_GroupId == m_GroupMaxId
|
|
|
|
|
? m_GroupId
|
|
|
|
|
: Random.Range(m_GroupId, m_GroupMaxId + 1);
|
2020-02-12 20:38:06 +08:00
|
|
|
|
}
|
2019-02-26 10:26:56 +08:00
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
protected override void UpdateMaterial()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 20:38:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Call to update the geometry of the Graphic onto the CanvasRenderer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void UpdateGeometry()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
private void UpdateRendererMaterial()
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < _renderers.Count; i++)
|
2020-02-12 20:38:06 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var r = _renderers[i];
|
|
|
|
|
if (!r) continue;
|
|
|
|
|
r.maskable = maskable;
|
|
|
|
|
r.SetMaterialDirty();
|
2020-02-12 20:38:06 +08:00
|
|
|
|
}
|
2020-08-12 22:19:08 +08:00
|
|
|
|
}
|
2020-08-28 15:50:13 +08:00
|
|
|
|
|
2022-06-11 21:32:14 +08:00
|
|
|
|
internal UIParticleRenderer GetRenderer(int index)
|
2020-10-04 22:26:53 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
if (_renderers.Count <= index)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_renderers.Add(UIParticleRenderer.AddRenderer(this, index));
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
|
|
|
|
|
if (!_renderers[index])
|
2022-07-01 14:55:59 +08:00
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_renderers[index] = UIParticleRenderer.AddRenderer(this, index);
|
2022-07-01 14:55:59 +08:00
|
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
|
|
|
|
|
return _renderers[index];
|
2020-10-04 22:26:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
private Camera GetBakeCamera()
|
2020-08-28 15:50:13 +08:00
|
|
|
|
{
|
2022-06-08 11:54:11 +08:00
|
|
|
|
if (!canvas) return Camera.main;
|
2020-08-28 15:50:13 +08:00
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
// Render mode is not ScreenSpaceOverlay, use world camera.
|
2022-06-08 11:54:11 +08:00
|
|
|
|
var root = canvas.rootCanvas;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
if (root.renderMode != RenderMode.ScreenSpaceOverlay)
|
|
|
|
|
{
|
|
|
|
|
return root.worldCamera ? root.worldCamera : Camera.main;
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
|
|
// Create ortho-camera.
|
|
|
|
|
if (!_orthoCamera)
|
2020-10-04 22:26:53 +08:00
|
|
|
|
{
|
2022-06-08 11:54:11 +08:00
|
|
|
|
_orthoCamera = GetComponentInChildren<Camera>();
|
|
|
|
|
if (!_orthoCamera)
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
var go = new GameObject("UIParticleOverlayCamera") { hideFlags = HideFlags.DontSave };
|
2022-06-08 11:54:11 +08:00
|
|
|
|
go.SetActive(false);
|
|
|
|
|
go.transform.SetParent(transform, false);
|
|
|
|
|
_orthoCamera = go.AddComponent<Camera>();
|
|
|
|
|
_orthoCamera.enabled = false;
|
|
|
|
|
}
|
2020-10-04 22:26:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
//
|
2022-06-08 11:54:11 +08:00
|
|
|
|
var size = ((RectTransform)root.transform).rect.size;
|
|
|
|
|
_orthoCamera.orthographicSize = Mathf.Max(size.x, size.y) * root.scaleFactor;
|
|
|
|
|
_orthoCamera.transform.SetPositionAndRotation(new Vector3(0, 0, -1000), Quaternion.identity);
|
|
|
|
|
_orthoCamera.orthographic = true;
|
|
|
|
|
_orthoCamera.farClipPlane = 2000f;
|
|
|
|
|
|
|
|
|
|
return _orthoCamera;
|
2020-08-28 15:50:13 +08:00
|
|
|
|
}
|
2023-08-15 20:56:09 +08:00
|
|
|
|
|
|
|
|
|
private void UpdateTracker()
|
|
|
|
|
{
|
|
|
|
|
if (!enabled || !autoScaling)
|
|
|
|
|
{
|
|
|
|
|
_tracker.Clear();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 20:38:06 +08:00
|
|
|
|
}
|
2020-04-30 11:28:48 +08:00
|
|
|
|
}
|