Skip to content

Commit

Permalink
feat: UIParticle no longer inherits from MaskableGraphic
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Some members inherited from MaskableGraphic will no longer be available.
  • Loading branch information
mob-sakai committed Sep 29, 2024
1 parent 81d9750 commit b99a7c2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
15 changes: 7 additions & 8 deletions Packages/src/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.UI;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Profiling;
Expand All @@ -26,7 +25,7 @@ namespace Coffee.UIExtensions
{
[CustomEditor(typeof(UIParticle))]
[CanEditMultipleObjects]
internal class UIParticleEditor : GraphicEditor
internal class UIParticleEditor : Editor
{
//################################
// Constant or Static Members.
Expand Down Expand Up @@ -81,10 +80,8 @@ internal class UIParticleEditor : GraphicEditor
/// <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 @@ -437,9 +434,11 @@ private void DestroyUIParticle(UIParticle p, bool ignoreCurrent = false)
{
if (!p || (ignoreCurrent && target == p)) return;

var cr = p.canvasRenderer;
DestroyImmediate(p);
DestroyImmediate(cr);
Misc.DestroyImmediate(p);
if (p.TryGetComponent<CanvasRenderer>(out var cr))
{
Misc.DestroyImmediate(cr);
}

#if UNITY_2018_3_OR_NEWER
var stage = PrefabStageUtility.GetCurrentPrefabStage();
Expand Down
75 changes: 53 additions & 22 deletions Packages/src/Runtime/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Runtime.CompilerServices;
using Coffee.UIParticleInternal;
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 @@ -18,7 +18,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 Down Expand Up @@ -117,20 +117,48 @@ public enum PositionMode
"Change the bake view size.")]
private float m_CustomViewSize = 10;

[SerializeField]
private bool m_Maskable = true;

private readonly List<UIParticleRenderer> _renderers = new List<UIParticleRenderer>();
private Camera _bakeCamera;
private Canvas _canvas;
private int _groupId;
private bool _isScaleStored;
private Vector3 _storedScale;
private DrivenRectTransformTracker _tracker;

public RectTransform rectTransform => transform as RectTransform;

public Canvas canvas
{
get
{
if (_canvas) return _canvas;

var tr = transform;
while (tr && !_canvas)
{
if (tr.TryGetComponent(out _canvas)) return _canvas;
tr = tr.parent;
}

return null;
}
}

/// <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 @@ -307,15 +335,15 @@ public Vector3 scale3D

public Vector3 parentScale { get; private set; }

public Vector3 canvasScale { get; private set; }
private Vector3 canvasScale { get; set; }

protected override void OnEnable()
{
_isScaleStored = false;
ResetGroupId();
UIParticleUpdater.Register(this);
RegisterDirtyMaterialCallback(UpdateRendererMaterial);

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

base.OnEnable();
UpdateRendererMaterial();
}

/// <summary>
Expand All @@ -342,9 +370,15 @@ protected override void OnDisable()
_isScaleStored = false;
UIParticleUpdater.Unregister(this);
_renderers.ForEach(r => r.Reset());
UnregisterDirtyMaterialCallback(UpdateRendererMaterial);
_canvas = null;
}

base.OnDisable();
/// <summary>
/// Called when the state of the parent Canvas is changed.
/// </summary>
protected override void OnCanvasHierarchyChanged()
{
_canvas = null;
}

/// <summary>
Expand All @@ -354,6 +388,14 @@ protected override void OnDidApplyAnimationProperties()
{
}

/// <summary>
/// This function is called when a direct or indirect parent of the transform of the GameObject has changed.
/// </summary>
protected override void OnTransformParentChanged()
{
_canvas = null;
}

void ISerializationCallbackReceiver.OnBeforeSerialize()
{
}
Expand Down Expand Up @@ -646,17 +688,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

0 comments on commit b99a7c2

Please sign in to comment.