From 77624eaf62df2834aded7cc0a89366422ab02822 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Thu, 23 May 2024 00:12:28 +0900 Subject: [PATCH] refactor: refactor --- Packages/src/Runtime/UIParticle.cs | 67 +++++++++++++++++----- Packages/src/Runtime/UIParticleRenderer.cs | 26 ++++----- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/Packages/src/Runtime/UIParticle.cs b/Packages/src/Runtime/UIParticle.cs index 4c26549..0d35f65 100644 --- a/Packages/src/Runtime/UIParticle.cs +++ b/Packages/src/Runtime/UIParticle.cs @@ -109,7 +109,7 @@ namespace Coffee.UIExtensions private readonly List _renderers = new List(); private Canvas _canvas; private int _groupId; - private Camera _orthoCamera; + private Camera _orthographicCamera; private DrivenRectTransformTracker _tracker; public RectTransform rectTransform => transform as RectTransform; @@ -304,7 +304,7 @@ namespace Coffee.UIExtensions public Vector3 parentScale { get; private set; } - public Vector3 canvasScale { get; private set; } + private Vector3 canvasScale { get; set; } protected override void OnEnable() { @@ -390,29 +390,44 @@ namespace Coffee.UIExtensions #pragma warning restore CS0612 // Type or member is obsolete } + /// + /// Play the ParticleSystems. + /// public void Play() { particles.Exec(p => p.Simulate(0, false, true)); isPaused = false; } + /// + /// Pause the ParticleSystems. + /// public void Pause() { particles.Exec(p => p.Pause()); isPaused = true; } + /// + /// Unpause the ParticleSystems. + /// public void Resume() { isPaused = false; } + /// + /// Stop the ParticleSystems. + /// public void Stop() { particles.Exec(p => p.Stop()); isPaused = true; } + /// + /// Start emission of the ParticleSystems. + /// public void StartEmission() { particles.Exec(p => @@ -422,6 +437,9 @@ namespace Coffee.UIExtensions }); } + /// + /// Stop emission of the ParticleSystems. + /// public void StopEmission() { particles.Exec(p => @@ -431,17 +449,26 @@ namespace Coffee.UIExtensions }); } + /// + /// Clear the particles of the ParticleSystems. + /// public void Clear() { particles.Exec(p => p.Clear()); isPaused = true; } + /// + /// Refresh UIParticle using the ParticleSystem instance. + /// public void SetParticleSystemInstance(GameObject instance) { SetParticleSystemInstance(instance, true); } + /// + /// Refresh UIParticle using the ParticleSystem instance. + /// public void SetParticleSystemInstance(GameObject instance, bool destroyOldParticles) { if (!instance) return; @@ -464,6 +491,10 @@ namespace Coffee.UIExtensions RefreshParticles(instance); } + /// + /// Refresh UIParticle using the prefab. + /// The prefab is automatically instantiated. + /// public void SetParticleSystemPrefab(GameObject prefab) { if (!prefab) return; @@ -471,11 +502,19 @@ namespace Coffee.UIExtensions SetParticleSystemInstance(Instantiate(prefab.gameObject), true); } + /// + /// Refresh UIParticle. + /// Collect ParticleSystems under the GameObject and refresh the UIParticle. + /// public void RefreshParticles() { RefreshParticles(gameObject); } + /// + /// Refresh UIParticle. + /// Collect ParticleSystems under the GameObject and refresh the UIParticle. + /// private void RefreshParticles(GameObject root) { if (!root) return; @@ -622,8 +661,8 @@ namespace Coffee.UIExtensions return root.worldCamera ? root.worldCamera : Camera.main; } - // When render mode is ScreenSpaceOverlay, use ortho-camera. - if (!_orthoCamera) + // When render mode is ScreenSpaceOverlay, use orthographic-camera. + if (!_orthographicCamera) { // Find existing orthographic-camera. var childCount = transform.childCount; @@ -632,13 +671,13 @@ namespace Coffee.UIExtensions if (transform.GetChild(i).TryGetComponent(out var cam) && cam.name == "[generated] UIParticleOverlayCamera") { - _orthoCamera = cam; + _orthographicCamera = cam; break; } } - // Create ortho-camera. - if (!_orthoCamera) + // Create orthographic-camera. + if (!_orthographicCamera) { var go = new GameObject("[generated] UIParticleOverlayCamera") { @@ -646,18 +685,18 @@ namespace Coffee.UIExtensions }; go.SetActive(false); go.transform.SetParent(transform, false); - _orthoCamera = go.AddComponent(); - _orthoCamera.enabled = false; + _orthographicCamera = go.AddComponent(); + _orthographicCamera.enabled = false; } } // - _orthoCamera.orthographicSize = 10; - _orthoCamera.transform.SetPositionAndRotation(new Vector3(0, 0, -1000), Quaternion.identity); - _orthoCamera.orthographic = true; - _orthoCamera.farClipPlane = 2000f; + _orthographicCamera.orthographicSize = 10; + _orthographicCamera.transform.SetPositionAndRotation(new Vector3(0, 0, -1000), Quaternion.identity); + _orthographicCamera.orthographic = true; + _orthographicCamera.farClipPlane = 2000f; - return _orthoCamera; + return _orthographicCamera; } private void UpdateTracker() diff --git a/Packages/src/Runtime/UIParticleRenderer.cs b/Packages/src/Runtime/UIParticleRenderer.cs index ce1b6ff..5096049 100644 --- a/Packages/src/Runtime/UIParticleRenderer.cs +++ b/Packages/src/Runtime/UIParticleRenderer.cs @@ -22,12 +22,10 @@ namespace Coffee.UIExtensions [AddComponentMenu("")] internal class UIParticleRenderer : MaskableGraphic { - private static readonly List s_Components = new List(); private static readonly CombineInstance[] s_CombineInstances = { new CombineInstance() }; private static readonly List s_Materials = new List(2); private static MaterialPropertyBlock s_Mpb; - private static readonly List s_Renderers = new List(); - private static readonly List s_Colors = new List(); + private static readonly List s_Renderers = new List(8); private static readonly Vector3[] s_Corners = new Vector3[4]; private bool _delay; private int _index; @@ -41,7 +39,7 @@ namespace Coffee.UIExtensions private Vector3 _prevPsPos; private Vector3 _prevScale; private Vector2Int _prevScreenSize; - private bool _prewarm; + private bool _preWarm; private ParticleSystemRenderer _renderer; public override Texture mainTexture => _isTrail ? null : _particleSystem.GetTextureForSprite(); @@ -233,13 +231,13 @@ namespace Coffee.UIExtensions gameObject.layer = parent.gameObject.layer; _particleSystem = ps; - _prewarm = _particleSystem.main.prewarm; + _preWarm = _particleSystem.main.prewarm; #if UNITY_EDITOR if (Application.isPlaying) #endif { - if (_particleSystem.isPlaying || _prewarm) + if (_particleSystem.isPlaying || _preWarm) { _particleSystem.Clear(); _particleSystem.Pause(); @@ -547,10 +545,7 @@ namespace Coffee.UIExtensions return Matrix4x4.Scale(scale); case ParticleSystemSimulationSpace.Custom: return Matrix4x4.Translate(_particleSystem.main.customSimulationSpace.position.GetScaled(scale)) - //* Matrix4x4.Translate(wpos) - * Matrix4x4.Scale(scale) - //* Matrix4x4.Translate(-wpos) - ; + * Matrix4x4.Scale(scale); default: throw new NotSupportedException(); } @@ -566,7 +561,8 @@ namespace Coffee.UIExtensions var screenSize = new Vector2Int(Screen.width, Screen.height); var isWorldSpace = _particleSystem.IsWorldSpace(); var canvasScale = _parent.canvas ? _parent.canvas.scaleFactor : 1f; - var resolutionChanged = _prevScreenSize != screenSize || _prevCanvasScale != canvasScale; + var resolutionChanged = _prevScreenSize != screenSize + || !Mathf.Approximately(_prevCanvasScale, canvasScale); if (resolutionChanged && isWorldSpace) { // Update particle array size and get particles. @@ -574,7 +570,7 @@ namespace Coffee.UIExtensions var particles = ParticleSystemExtensions.GetParticleArray(size); _particleSystem.GetParticles(particles, size); - // Resolusion resolver: + // Resolution resolver: // (psPos / scale) / (prevPsPos / prevScale) -> psPos * scale.inv * prevPsPos.inv * prevScale var modifier = psPos.GetScaled( scale.Inverse(), @@ -608,11 +604,11 @@ namespace Coffee.UIExtensions ? Time.unscaledDeltaTime : Time.deltaTime; - // Prewarm: - if (0 < deltaTime && _prewarm) + // Pre-warm: + if (0 < deltaTime && _preWarm) { deltaTime += main.duration; - _prewarm = false; + _preWarm = false; } // get world position.