diff --git a/Assets/Coffee/UIExtensions/UIParticle/Editor/UIParticleEditor.cs b/Assets/Coffee/UIExtensions/UIParticle/Editor/UIParticleEditor.cs index 86400e7..e02c65d 100644 --- a/Assets/Coffee/UIExtensions/UIParticle/Editor/UIParticleEditor.cs +++ b/Assets/Coffee/UIExtensions/UIParticle/Editor/UIParticleEditor.cs @@ -62,6 +62,7 @@ namespace Coffee.UIExtensions { EditorGUILayout.PropertyField (serializedObject.FindProperty ("m_Scale")); } + EditorGUILayout.PropertyField (serializedObject.FindProperty ("m_IgnoreParent")); serializedObject.ApplyModifiedProperties(); } diff --git a/Assets/Coffee/UIExtensions/UIParticle/UIParticle.cs b/Assets/Coffee/UIExtensions/UIParticle/UIParticle.cs index d56b290..edfdb70 100644 --- a/Assets/Coffee/UIExtensions/UIParticle/UIParticle.cs +++ b/Assets/Coffee/UIExtensions/UIParticle/UIParticle.cs @@ -18,6 +18,8 @@ namespace Coffee.UIExtensions //################################ static readonly int s_IdMainTex = Shader.PropertyToID("_MainTex"); static readonly List s_Vertices = new List(); + static readonly List s_TempRelatables = new List(); + static readonly List s_ActiveSoftMasks = new List(); //################################ @@ -28,8 +30,10 @@ namespace Coffee.UIExtensions [Tooltip("The UIParticle to render trail effect")] [SerializeField] UIParticle m_TrailParticle; [HideInInspector] [SerializeField] bool m_IsTrail = false; - [Tooltip("Particle effect scale.")] + [Tooltip("Particle effect scale")] [SerializeField] float m_Scale = 1; + [Tooltip("Ignore parent scale")] + [SerializeField] bool m_IgnoreParent = false; //################################ @@ -73,6 +77,23 @@ namespace Coffee.UIExtensions /// public float scale { get { return _parent ? _parent.scale : m_Scale; } set { m_Scale = value; } } + /// + /// Should the soft mask ignore parent soft masks? + /// + /// If set to true the soft mask will ignore any parent soft mask settings. + public bool ignoreParent + { + get { return m_IgnoreParent; } + set + { + if(m_IgnoreParent != value) + { + m_IgnoreParent = value; + OnTransformParentChanged(); + } + } + } + public override Material GetModifiedMaterial(Material baseMaterial) { return base.GetModifiedMaterial(_renderer ? _renderer.sharedMaterial : baseMaterial); @@ -80,28 +101,54 @@ namespace Coffee.UIExtensions protected override void OnEnable() { + // Register. + if(s_ActiveSoftMasks.Count == 0) + { + Canvas.willRenderCanvases += UpdateMeshes; + } + s_ActiveSoftMasks.Add(this); + + // Reset the parent-child relation. + GetComponentsInChildren(false, s_TempRelatables); + for(int i = s_TempRelatables.Count - 1; 0 <= i; i--) + { + s_TempRelatables [i].OnTransformParentChanged(); + } + s_TempRelatables.Clear(); + m_ParticleSystem = m_ParticleSystem ? m_ParticleSystem : GetComponent(); _renderer = m_ParticleSystem ? m_ParticleSystem.GetComponent() : null; + // Create objects. _mesh = new Mesh(); _mesh.MarkDynamic(); CheckTrail(); + base.OnEnable(); - - Canvas.willRenderCanvases += UpdateMesh; - - foreach(var c in Resources.FindObjectsOfTypeAll()) - { - Debug.LogFormat ("{0}, {1}, {2}, {3}, {4}",c,c.orthographic,c.orthographicSize,c.transform.localScale,c.transform.position); - } } protected override void OnDisable() { - Canvas.willRenderCanvases -= UpdateMesh; + // Unregister. + s_ActiveSoftMasks.Remove(this); + if(s_ActiveSoftMasks.Count == 0) + { + Canvas.willRenderCanvases -= UpdateMeshes; + } + + // Reset the parent-child relation. + for(int i = _children.Count - 1; 0 <= i; i--) + { + _children [i].SetParent(_parent); + } + _children.Clear(); + SetParent(null); + + // Destroy objects. DestroyImmediate(_mesh); _mesh = null; CheckTrail(); + base.OnDisable(); } @@ -109,13 +156,57 @@ namespace Coffee.UIExtensions { } + /// + /// This function is called when the parent property of the transform of the GameObject has changed. + /// + protected override void OnTransformParentChanged() + { + UIParticle newParent = null; + if(isActiveAndEnabled && !m_IgnoreParent) + { + var parentTransform = transform.parent; + while(parentTransform &&(!newParent || !newParent.enabled)) + { + newParent = parentTransform.GetComponent(); + parentTransform = parentTransform.parent; + } + } + SetParent(newParent); + } + + protected override void OnDidApplyAnimationProperties() + { + } + +#if UNITY_EDITOR + /// + /// This function is called when the script is loaded or a value is changed in the inspector(Called in the editor only). + /// + protected override void OnValidate() + { + OnTransformParentChanged(); + base.OnValidate(); + } +#endif + + //################################ // Private Members. //################################ Mesh _mesh; ParticleSystemRenderer _renderer; + UIParticle _parent; + List _children = new List(); Matrix4x4 scaleaMatrix = default(Matrix4x4); + static void UpdateMeshes() + { + foreach(var uip in s_ActiveSoftMasks) + { + uip.UpdateMesh(); + } + } + void UpdateMesh() { try @@ -161,11 +252,11 @@ namespace Coffee.UIExtensions Profiler.BeginSample("Bake Mesh"); if (m_IsTrail) { - _renderer.BakeTrailsMesh(_mesh, cam, m_UseTransform); + _renderer.BakeTrailsMesh(_mesh, true); } else { - _renderer.BakeMesh(_mesh, cam, m_UseTransform); + _renderer.BakeMesh(_mesh, true); } Profiler.EndSample(); @@ -220,5 +311,27 @@ namespace Coffee.UIExtensions m_TrailParticle.enabled = false; } } + + /// + /// Set the parent of the soft mask. + /// + /// The parent soft mask to use. + void SetParent(UIParticle newParent) + { + if(_parent != newParent && this != newParent) + { + if(_parent && _parent._children.Contains(this)) + { + _parent._children.Remove(this); + _parent._children.RemoveAll(x => x == null); + } + _parent = newParent; + } + + if(_parent && !_parent._children.Contains(this)) + { + _parent._children.Add(this); + } + } } } \ No newline at end of file