Skip to content

Commit

Permalink
feat: UIParticle no longer inherits from MaskableGraphic
Browse files Browse the repository at this point in the history
NOTE: Some members inherited from MaskableGraphic will no longer be available.
  • Loading branch information
mob-sakai committed Nov 27, 2023
1 parent 179162b commit 385a1a0
Show file tree
Hide file tree
Showing 20 changed files with 1,091 additions and 176 deletions.
27 changes: 10 additions & 17 deletions Packages/src/Scripts/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_2021_2_OR_NEWER
using UnityEditor.Overlays;
#else
using System.Reflection;
#endif
#if UNITY_2021_2_OR_NEWER
using UnityEditor.SceneManagement;

#elif UNITY_2018_3_OR_NEWER
using UnityEditor.Experimental.SceneManagement;
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Coffee.UIParticleExtensions;
using UnityEditor;
using UnityEditor.UI;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace Coffee.UIExtensions
{
[CustomEditor(typeof(UIParticle))]
[CanEditMultipleObjects]
internal class UIParticleEditor : GraphicEditor
internal class UIParticleEditor : Editor
{
#if UNITY_2021_2_OR_NEWER
#if UNITY_2022_1_OR_NEWER
Expand Down Expand Up @@ -144,10 +141,8 @@ SerializedObject CreateSerializeObject()
/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
protected override void OnEnable()
private void OnEnable()
{
base.OnEnable();

_maskable = serializedObject.FindProperty("m_Maskable");
_scale3D = serializedObject.FindProperty("m_Scale3D");
_animatableProperties = serializedObject.FindProperty("m_AnimatableProperties");
Expand Down Expand Up @@ -525,9 +520,7 @@ private void DestroyUIParticle(UIParticle p, bool ignoreCurrent = false)
{
if (!p || (ignoreCurrent && target == p)) return;

var cr = p.canvasRenderer;
DestroyImmediate(p);
DestroyImmediate(cr);

#if UNITY_2018_3_OR_NEWER
var stage = PrefabStageUtility.GetCurrentPrefabStage();
Expand Down
70 changes: 0 additions & 70 deletions Packages/src/Scripts/ModifiedMaterial.cs

This file was deleted.

91 changes: 57 additions & 34 deletions Packages/src/Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using Coffee.UIParticleExtensions;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.Serialization;
using UnityEngine.UI;
using Random = UnityEngine.Random;

[assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")]
Expand All @@ -19,7 +19,7 @@ namespace Coffee.UIExtensions
[ExecuteAlways]
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(CanvasRenderer))]
public class UIParticle : MaskableGraphic, ISerializationCallbackReceiver
public class UIParticle : UIBehaviour, ISerializationCallbackReceiver
{
public enum AutoScalingMode
{
Expand All @@ -43,6 +43,9 @@ public enum PositionMode
Absolute
}

[SerializeField]
private bool m_Maskable = true;

[HideInInspector]
[SerializeField]
internal bool m_IsTrail;
Expand Down Expand Up @@ -104,17 +107,43 @@ public enum PositionMode
private bool m_ResetScaleOnEnable;

private readonly List<UIParticleRenderer> _renderers = new List<UIParticleRenderer>();
private Canvas _canvas;
private int _groupId;
private Camera _orthoCamera;
private DrivenRectTransformTracker _tracker;

public RectTransform rectTransform => transform as RectTransform;

public Canvas canvas
{
get
{
if (_canvas == null)
{
var tr = transform;
while (tr && !_canvas)
{
if (tr.TryGetComponent(out _canvas)) return _canvas;
tr = tr.parent;
}
}

return _canvas;
}
}

/// <summary>
/// Should this graphic be considered a target for ray-casting?
/// Does this graphic allow masking.
/// </summary>
public override bool raycastTarget
public bool maskable
{
get => false;
set { }
get => m_Maskable;
set
{
if (value == m_Maskable) return;
m_Maskable = value;
UpdateRendererMaterial();
}
}

/// <summary>
Expand Down Expand Up @@ -269,8 +298,6 @@ public IEnumerable<Material> materials
}
}

public override Material materialForRendering => null;

/// <summary>
/// Paused.
/// </summary>
Expand All @@ -285,7 +312,6 @@ protected override void OnEnable()
ResetGroupId();
UpdateTracker();
UIParticleUpdater.Register(this);
RegisterDirtyMaterialCallback(UpdateRendererMaterial);

if (0 < particles.Count)
{
Expand All @@ -296,7 +322,7 @@ protected override void OnEnable()
RefreshParticles();
}

base.OnEnable();
UpdateRendererMaterial();

// Reset scale for upgrade.
if (m_ResetScaleOnEnable)
Expand All @@ -314,9 +340,12 @@ protected override void OnDisable()
UpdateTracker();
UIParticleUpdater.Unregister(this);
_renderers.ForEach(r => r.Reset());
UnregisterDirtyMaterialCallback(UpdateRendererMaterial);
_canvas = null;
}

base.OnDisable();
protected override void OnCanvasHierarchyChanged()
{
_canvas = null;
}

/// <summary>
Expand All @@ -326,11 +355,17 @@ protected override void OnDidApplyAnimationProperties()
{
}

protected override void OnTransformParentChanged()
{
_canvas = null;
}

#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
UpdateTracker();
UpdateRendererMaterial();
}
#endif

Expand Down Expand Up @@ -469,7 +504,7 @@ private void RefreshParticles(GameObject root)
RefreshParticles(particles);
}

public void RefreshParticles(List<ParticleSystem> particles)
public void RefreshParticles(List<ParticleSystem> particleSystems)
{
// #246: Nullptr exceptions when using nested UIParticle components in hierarchy
_renderers.Clear();
Expand All @@ -489,9 +524,9 @@ public void RefreshParticles(List<ParticleSystem> particles)
}

var j = 0;
for (var i = 0; i < particles.Count; i++)
for (var i = 0; i < particleSystems.Count; i++)
{
var ps = particles[i];
var ps = particleSystems[i];
if (!ps) continue;
GetRenderer(j++).Set(this, ps, false);
if (ps.trails.enabled)
Expand Down Expand Up @@ -521,18 +556,18 @@ internal void UpdateRenderers()
for (var i = 0; i < _renderers.Count; i++)
{
var r = _renderers[i];
if (!r)
{
RefreshParticles(particles);
break;
}
if (r) continue;

RefreshParticles(particles);
break;
}

var bakeCamera = GetBakeCamera();
for (var i = 0; i < _renderers.Count; i++)
{
var r = _renderers[i];
if (!r) continue;

r.UpdateMesh(bakeCamera);
}
}
Expand All @@ -554,17 +589,6 @@ internal void ResetGroupId()
: Random.Range(m_GroupId, m_GroupMaxId + 1);
}

protected override void UpdateMaterial()
{
}

/// <summary>
/// Call to update the geometry of the Graphic onto the CanvasRenderer.
/// </summary>
protected override void UpdateGeometry()
{
}

private void UpdateRendererMaterial()
{
for (var i = 0; i < _renderers.Count; i++)
Expand Down Expand Up @@ -628,8 +652,7 @@ private Camera GetBakeCamera()
}

//
var size = ((RectTransform)root.transform).rect.size;
_orthoCamera.orthographicSize = Mathf.Max(size.x, size.y) * root.scaleFactor;
_orthoCamera.orthographicSize = 10;
_orthoCamera.transform.SetPositionAndRotation(new Vector3(0, 0, -1000), Quaternion.identity);
_orthoCamera.orthographic = true;
_orthoCamera.farClipPlane = 2000f;
Expand All @@ -639,7 +662,7 @@ private Camera GetBakeCamera()

private void UpdateTracker()
{
if (!enabled || !autoScaling || autoScalingMode != AutoScalingMode.Transform)
if (!enabled || autoScalingMode != AutoScalingMode.Transform)
{
_tracker.Clear();
}
Expand Down
Loading

0 comments on commit 385a1a0

Please sign in to comment.