Skip to content

Commit

Permalink
feat: automatically update
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Aug 28, 2020
1 parent 41ea07b commit 96a868b
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions Packages/UIParticle/Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Coffee.UIExtensions
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(CanvasRenderer))]
public class UIParticle : MaskableGraphic
public class UIParticle : MaskableGraphic, ISerializationCallbackReceiver
{
[HideInInspector] [SerializeField] bool m_IsTrail = false;

Expand Down Expand Up @@ -114,6 +114,13 @@ public void RefreshParticles()
{
GetComponentsInChildren(particles);

foreach (var ps in particles)
{
var tsa = ps.textureSheetAnimation;
if (tsa.mode == ParticleSystemAnimationMode.Sprites && tsa.uvChannelMask == (UVChannelFlags) 0)
tsa.uvChannelMask = UVChannelFlags.UV0;
}

particles.Exec(p => p.GetComponent<ParticleSystemRenderer>().enabled = !enabled);
particles.SortForRendering(transform);

Expand Down Expand Up @@ -258,26 +265,53 @@ protected override void OnDidApplyAnimationProperties()

private void InitializeIfNeeded()
{
if (0 < particles.Count) return;
if (!this || 0 < particles.Count) return;

if (m_IsTrail
|| transform.parent && transform.parent.GetComponentInParent<UIParticle>())
if (m_IsTrail)
{
gameObject.SetActive(false);
UnityEngine.Debug.LogWarningFormat("[UIParticle] Remove this UIParticle: {0}\nReason: UIParticle for trails is no longer needed.", name);
if (Application.isPlaying)
Destroy(gameObject);
else
DestroyImmediate(gameObject);
return;
}

if (transform.parent && transform.parent.GetComponentInParent<UIParticle>())
{
UnityEngine.Debug.LogWarningFormat("[UIParticle] Remove this UIParticle: {0}\nReason: The parent UIParticle exists.", name);
if (Application.isPlaying)
Destroy(this);
else
DestroyImmediate(this);
return;
}

// refresh.
#if UNITY_EDITOR
if (!Application.isPlaying)
UnityEditor.EditorApplication.delayCall += RefreshParticles;
UnityEditor.EditorApplication.delayCall += () =>
{
if (this) RefreshParticles();
};
else
#endif
RefreshParticles();
}

#if UNITY_EDITOR
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
InitializeIfNeeded();
}

void ISerializationCallbackReceiver.OnAfterDeserialize()
{
UnityEditor.EditorApplication.delayCall += () =>
{
if (this) InitializeIfNeeded();
};
}
#endif
}
}

0 comments on commit 96a868b

Please sign in to comment.