2023-11-07 10:42:16 +08:00
|
|
|
using System;
|
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;
|
2023-11-07 10:42:16 +08:00
|
|
|
using Random = UnityEngine.Random;
|
2018-06-22 18:48:14 +08:00
|
|
|
|
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))]
|
2023-08-18 10:42:22 +08:00
|
|
|
public class UIParticle : MaskableGraphic, ISerializationCallbackReceiver
|
2020-02-12 20:38:06 +08:00
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
public enum AutoScalingMode
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
UIParticle,
|
|
|
|
Transform
|
|
|
|
}
|
|
|
|
|
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-18 10:42:22 +08:00
|
|
|
public enum PositionMode
|
|
|
|
{
|
|
|
|
Relative,
|
|
|
|
Absolute
|
|
|
|
}
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
[HideInInspector]
|
|
|
|
[SerializeField]
|
|
|
|
internal bool m_IsTrail;
|
2020-02-12 20:38:06 +08:00
|
|
|
|
2023-08-18 10:42:56 +08:00
|
|
|
[HideInInspector]
|
|
|
|
[FormerlySerializedAs("m_IgnoreParent")]
|
|
|
|
[SerializeField]
|
|
|
|
private bool m_IgnoreCanvasScaler;
|
|
|
|
|
2023-08-18 10:42:22 +08:00
|
|
|
[HideInInspector]
|
|
|
|
[SerializeField]
|
|
|
|
private bool m_AbsoluteMode;
|
|
|
|
|
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
|
|
|
|
2023-08-18 10:42:22 +08:00
|
|
|
[Tooltip("Relative: The particles will be emitted from the scaled position of ParticleSystem.\n" +
|
|
|
|
"Absolute: The particles will be emitted from the world position of ParticleSystem.")]
|
2022-06-25 01:04:23 +08:00
|
|
|
[SerializeField]
|
2023-08-18 10:42:22 +08:00
|
|
|
private PositionMode m_PositionMode = PositionMode.Relative;
|
2022-06-25 01:04:23 +08:00
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
[SerializeField]
|
2023-11-07 10:42:16 +08:00
|
|
|
[Tooltip("Prevent the root-Canvas scale from affecting the hierarchy-scaled ParticleSystem.")]
|
2023-08-18 10:42:56 +08:00
|
|
|
private bool m_AutoScaling = true;
|
2023-11-07 10:42:16 +08:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
[Tooltip("Transform: Transform.lossyScale (=world scale) will be set to (1, 1, 1)." +
|
|
|
|
"UIParticle: UIParticle.scale will be adjusted.")]
|
|
|
|
private AutoScalingMode m_AutoScalingMode = AutoScalingMode.Transform;
|
|
|
|
|
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;
|
2024-06-18 16:13:35 +08:00
|
|
|
private Vector3 _storedScale;
|
|
|
|
private bool _isScaleStored;
|
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
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => 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
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_MeshSharing;
|
|
|
|
set => m_MeshSharing = value;
|
2022-06-11 22:10:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => _groupId;
|
2022-06-14 12:27:54 +08:00
|
|
|
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
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_GroupMaxId;
|
2022-06-14 12:27:54 +08:00
|
|
|
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.
|
2023-08-18 10:42:22 +08:00
|
|
|
/// Relative: The particles will be emitted from the scaled position of the ParticleSystem.
|
|
|
|
/// Absolute: The particles will be emitted from the world position of the ParticleSystem.
|
|
|
|
/// </summary>
|
|
|
|
public PositionMode positionMode
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_PositionMode;
|
|
|
|
set => m_PositionMode = value;
|
2023-08-18 10:42:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Particle position mode.
|
|
|
|
/// Relative: The particles will be emitted from the scaled position of the ParticleSystem.
|
|
|
|
/// Absolute: The particles will be emitted from the world position of the ParticleSystem.
|
2022-06-25 01:04:23 +08:00
|
|
|
/// </summary>
|
|
|
|
public bool absoluteMode
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_PositionMode == PositionMode.Absolute;
|
|
|
|
set => positionMode = value ? PositionMode.Absolute : PositionMode.Relative;
|
2022-06-25 01:04:23 +08:00
|
|
|
}
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
/// <summary>
|
2023-11-07 10:42:16 +08:00
|
|
|
/// Prevents the root-Canvas scale from affecting the hierarchy-scaled ParticleSystem.
|
2023-08-17 08:43:02 +08:00
|
|
|
/// </summary>
|
2023-11-07 10:42:16 +08:00
|
|
|
[Obsolete("The autoScaling is now obsolete. Please use the autoScalingMode instead.", false)]
|
2023-08-15 20:56:09 +08:00
|
|
|
public bool autoScaling
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_AutoScalingMode != AutoScalingMode.None;
|
|
|
|
set => autoScalingMode = value ? AutoScalingMode.Transform : AutoScalingMode.None;
|
2023-11-07 10:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Auto scaling mode.
|
|
|
|
/// Transform: Transform.lossyScale (=world scale) will be set to (1, 1, 1).
|
|
|
|
/// UIParticle: UIParticle.scale will be adjusted.
|
|
|
|
/// </summary>
|
|
|
|
public AutoScalingMode autoScalingMode
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_AutoScalingMode;
|
2023-08-15 20:56:09 +08:00
|
|
|
set
|
|
|
|
{
|
2023-11-07 10:42:16 +08:00
|
|
|
if (m_AutoScalingMode == value) return;
|
|
|
|
m_AutoScalingMode = value;
|
2024-06-18 16:13:35 +08:00
|
|
|
|
|
|
|
if (autoScalingMode != AutoScalingMode.Transform && _isScaleStored)
|
|
|
|
{
|
|
|
|
transform.localScale = _storedScale;
|
|
|
|
_isScaleStored = false;
|
|
|
|
}
|
2023-08-15 20:56:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:17:57 +08:00
|
|
|
internal bool useMeshSharing => m_MeshSharing != MeshSharing.None;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
2024-01-23 22:17:57 +08:00
|
|
|
internal bool isPrimary =>
|
|
|
|
m_MeshSharing == MeshSharing.Primary
|
|
|
|
|| m_MeshSharing == MeshSharing.PrimarySimulator;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
2024-01-23 22:17:57 +08:00
|
|
|
internal bool canSimulate =>
|
|
|
|
m_MeshSharing == MeshSharing.None
|
|
|
|
|| m_MeshSharing == MeshSharing.Auto
|
|
|
|
|| m_MeshSharing == MeshSharing.Primary
|
|
|
|
|| m_MeshSharing == MeshSharing.PrimarySimulator;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
2024-01-23 22:17:57 +08:00
|
|
|
internal bool canRender =>
|
|
|
|
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
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_Scale3D.x;
|
|
|
|
set => m_Scale3D = new Vector3(value, value, value);
|
2020-10-04 22:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Particle effect scale.
|
|
|
|
/// </summary>
|
|
|
|
public Vector3 scale3D
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
get => m_Scale3D;
|
|
|
|
set => m_Scale3D = value;
|
2020-02-12 20:38:06 +08:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:42:16 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Particle effect scale.
|
|
|
|
/// </summary>
|
2024-06-18 16:47:45 +08:00
|
|
|
public Vector3 scale3DForCalc => autoScalingMode == AutoScalingMode.Transform
|
|
|
|
? m_Scale3D
|
2024-06-18 16:48:08 +08:00
|
|
|
: m_Scale3D.GetScaled(canvasScale, transform.localScale);
|
2023-11-07 10:42:16 +08:00
|
|
|
|
2024-01-23 22:17:57 +08:00
|
|
|
public List<ParticleSystem> particles => 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
|
|
|
|
2024-01-23 22:17:57 +08:00
|
|
|
public override Material materialForRendering => null;
|
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-11-07 10:42:16 +08:00
|
|
|
public Vector3 canvasScale { get; private set; }
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
protected override void OnEnable()
|
|
|
|
{
|
2024-06-18 16:13:35 +08:00
|
|
|
_isScaleStored = false;
|
2023-08-17 08:43:02 +08:00
|
|
|
ResetGroupId();
|
|
|
|
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()
|
|
|
|
{
|
2024-06-18 16:13:35 +08:00
|
|
|
_tracker.Clear();
|
|
|
|
if (autoScalingMode == AutoScalingMode.Transform && _isScaleStored)
|
|
|
|
{
|
|
|
|
transform.localScale = _storedScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isScaleStored = false;
|
2023-08-17 08:43:02 +08:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-18 10:42:22 +08:00
|
|
|
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
|
|
|
{
|
2023-11-07 10:42:16 +08:00
|
|
|
if (m_IgnoreCanvasScaler || m_AutoScaling)
|
2023-08-18 10:42:56 +08:00
|
|
|
{
|
2023-08-18 19:30:01 +08:00
|
|
|
m_IgnoreCanvasScaler = false;
|
2023-08-18 10:42:56 +08:00
|
|
|
m_AutoScaling = false;
|
2023-11-07 10:42:16 +08:00
|
|
|
m_AutoScalingMode = AutoScalingMode.Transform;
|
2023-08-18 10:42:56 +08:00
|
|
|
}
|
|
|
|
|
2023-08-18 10:42:22 +08:00
|
|
|
if (m_AbsoluteMode)
|
|
|
|
{
|
2023-08-18 19:30:01 +08:00
|
|
|
m_AbsoluteMode = false;
|
2023-08-18 10:42:22 +08:00
|
|
|
m_PositionMode = PositionMode.Absolute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2023-08-17 16:48:48 +08:00
|
|
|
root.GetComponentsInChildren(true, particles);
|
|
|
|
particles.RemoveAll(x => x.GetComponentInParent<UIParticle>(true) != 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
|
|
|
{
|
2024-06-18 16:13:35 +08:00
|
|
|
_tracker.Clear();
|
2023-11-07 10:42:16 +08:00
|
|
|
canvasScale = canvas.rootCanvas.transform.localScale.Inverse();
|
2023-08-15 20:56:09 +08:00
|
|
|
parentScale = transform.parent.lossyScale;
|
2024-06-18 16:13:35 +08:00
|
|
|
if (autoScalingMode != AutoScalingMode.Transform)
|
|
|
|
{
|
|
|
|
if (_isScaleStored)
|
|
|
|
{
|
|
|
|
transform.localScale = _storedScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isScaleStored = false;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-15 20:56:09 +08:00
|
|
|
|
2024-06-18 16:13:35 +08:00
|
|
|
var currentScale = transform.localScale;
|
|
|
|
_storedScale = currentScale;
|
|
|
|
_isScaleStored = true;
|
|
|
|
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
|
2023-08-15 20:56:09 +08:00
|
|
|
var newScale = parentScale.Inverse();
|
2024-06-18 16:13:35 +08:00
|
|
|
if (currentScale != 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-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-18 16:20:12 +08:00
|
|
|
// When render mode is ScreenSpaceCamera or WorldSpace, 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
|
|
|
|
2023-08-18 16:20:12 +08:00
|
|
|
// When render mode is ScreenSpaceOverlay, use ortho-camera.
|
2022-06-08 11:54:11 +08:00
|
|
|
if (!_orthoCamera)
|
2020-10-04 22:26:53 +08:00
|
|
|
{
|
2023-08-18 16:23:13 +08:00
|
|
|
// Find existing ortho-camera.
|
|
|
|
foreach (Transform child in transform)
|
|
|
|
{
|
|
|
|
var cam = child.GetComponent<Camera>();
|
|
|
|
if (cam && cam.name == "[generated] UIParticleOverlayCamera")
|
|
|
|
{
|
|
|
|
_orthoCamera = cam;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-18 16:20:12 +08:00
|
|
|
|
|
|
|
// Create ortho-camera.
|
2022-06-08 11:54:11 +08:00
|
|
|
if (!_orthoCamera)
|
|
|
|
{
|
2024-01-23 22:17:57 +08:00
|
|
|
var go = new GameObject("[generated] UIParticleOverlayCamera")
|
|
|
|
{
|
|
|
|
hideFlags = HideFlags.HideAndDontSave
|
|
|
|
};
|
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;
|
2024-06-14 09:47:32 +08:00
|
|
|
_orthoCamera.clearFlags = CameraClearFlags.Nothing;
|
|
|
|
_orthoCamera.cullingMask = 0; // Nothing
|
|
|
|
_orthoCamera.allowHDR = false;
|
|
|
|
_orthoCamera.allowMSAA = false;
|
|
|
|
_orthoCamera.renderingPath = RenderingPath.Forward;
|
|
|
|
_orthoCamera.useOcclusionCulling = false;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
return _orthoCamera;
|
2020-08-28 15:50:13 +08:00
|
|
|
}
|
2020-02-12 20:38:06 +08:00
|
|
|
}
|
2020-04-30 11:28:48 +08:00
|
|
|
}
|