diff --git a/Packages/src/Runtime/UIParticleRenderer.cs b/Packages/src/Runtime/UIParticleRenderer.cs index 7e583cd..cfd216e 100644 --- a/Packages/src/Runtime/UIParticleRenderer.cs +++ b/Packages/src/Runtime/UIParticleRenderer.cs @@ -37,8 +37,8 @@ internal class UIParticleRenderer : MaskableGraphic private UIParticle _parent; private ParticleSystem _particleSystem; private float _prevCanvasScale; - private Vector3 _prevPsPos; - private Vector3 _prevScale; + private Vector3? _prevPsPos; + private Vector3? _prevScale; private Vector2Int _prevScreenSize; private bool _prewarm; private ParticleSystemRenderer _renderer; @@ -146,6 +146,8 @@ protected override void OnDisable() ModifiedMaterial.Remove(_modifiedMaterial); _modifiedMaterial = null; _currentMaterialForRendering = null; + _prevScale = null; + _prevPsPos = null; } public static UIParticleRenderer AddRenderer(UIParticle parent, int index) @@ -296,7 +298,7 @@ public void UpdateMesh(Camera bakeCamera) #if UNITY_EDITOR if (!Application.isPlaying) { - SimulateForEditor(psPos - _prevPsPos, scale); + SimulateForEditor(psPos - _prevPsPos.GetValueOrDefault(), scale); } else #endif @@ -581,7 +583,7 @@ private void ResolveResolutionChange(Vector3 psPos, Vector3 scale) var isWorldSpace = _particleSystem.IsWorldSpace(); var canvasScale = _parent.canvas ? _parent.canvas.scaleFactor : 1f; var resolutionChanged = _prevScreenSize != screenSize || _prevCanvasScale != canvasScale; - if (resolutionChanged && isWorldSpace) + if (resolutionChanged && isWorldSpace && _prevPsPos.HasValue && _prevScale.HasValue) { // Update particle array size and get particles. var size = _particleSystem.particleCount; @@ -592,8 +594,8 @@ private void ResolveResolutionChange(Vector3 psPos, Vector3 scale) // (psPos / scale) / (prevPsPos / prevScale) -> psPos * scale.inv * prevPsPos.inv * prevScale var modifier = psPos.GetScaled( scale.Inverse(), - _prevPsPos.Inverse(), - _prevScale); + _prevPsPos.Value.Inverse(), + _prevScale.Value); for (var i = 0; i < size; i++) { var particle = particles[i]; @@ -640,12 +642,12 @@ private void Simulate(Vector3 scale, bool paused) var rateOverDistance = emission.enabled && 0 < emission.rateOverDistance.constant && 0 < emission.rateOverDistanceMultiplier; - if (rateOverDistance && !paused) + if (rateOverDistance && !paused && _prevPsPos.HasValue && _prevScale.HasValue) { // (For rate-over-distance emission,) Move to previous scaled position, simulate (delta = 0). var prevScaledPos = isLocalSpace - ? _prevPsPos - : _prevPsPos.GetScaled(_prevScale.Inverse()); + ? _prevPsPos.Value + : _prevPsPos.Value.GetScaled(_prevScale.Value.Inverse()); psTransform.SetPositionAndRotation(prevScaledPos, originWorldRotation); _particleSystem.Simulate(0, false, false, false); }