| | |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using UnityEngine.UI; |
| | | namespace vnxbqy.UI |
| | | public class UIEffectBehaviour : MonoBehaviour |
| | | { |
| | | public class UIEffectBehaviour : MonoBehaviour |
| | | private List<ParticleSystem> m_Particles = new List<ParticleSystem>(); |
| | | private List<Animator> m_Animators = new List<Animator>(); |
| | | private List<Renderer> m_Renderers = new List<Renderer>(); |
| | | |
| | | public float duration { get; private set; } |
| | | private Vector3 m_RectMaskPosition; |
| | | private bool m_RectMaskEnable; |
| | | private RectTransform m_RectMask; |
| | | |
| | | public bool IsAnimatorControl { get; private set; } |
| | | |
| | | private void Awake() |
| | | { |
| | | private List<ParticleSystem> m_Particles = new List<ParticleSystem>(); |
| | | private List<Animator> m_Animators = new List<Animator>(); |
| | | private List<Renderer> m_Renderers = new List<Renderer>(); |
| | | GetComponent(); |
| | | this.gameObject.SetLayer(LayerUtility.UIEffectLayer, true); |
| | | } |
| | | |
| | | public float duration { get; private set; } |
| | | private Vector3 m_RectMaskPosition; |
| | | private bool m_RectMaskEnable; |
| | | private RectTransform m_RectMask; |
| | | |
| | | public bool IsAnimatorControl { get; private set; } |
| | | |
| | | private void Awake() |
| | | public void SetParticlesScale(Vector3 _scale) |
| | | { |
| | | if (_scale != Vector3.one) |
| | | foreach (var par in m_Particles) |
| | | { |
| | | GetComponent(); |
| | | this.gameObject.SetLayer(LayerUtility.UIEffectLayer, true); |
| | | par.transform.localScale = _scale; |
| | | } |
| | | } |
| | | |
| | | public void SetParticlesScale(Vector3 _scale) |
| | | |
| | | public void SetOrder(int _order) |
| | | { |
| | | foreach (var _renderer in m_Renderers) |
| | | { |
| | | if (_scale != Vector3.one) |
| | | foreach (var par in m_Particles) |
| | | { |
| | | par.transform.localScale = _scale; |
| | | } |
| | | _renderer.sortingOrder = _order; |
| | | _renderer.sortingLayerName = "UI"; |
| | | } |
| | | } |
| | | |
| | | |
| | | public void SetOrder(int _order) |
| | | public void PerformMask(RectMask2D[] _masks) |
| | | { |
| | | RectTransform _rect = null; |
| | | var _mask = _masks == null || _masks.Length == 0 ? null : _masks[0]; |
| | | if (_mask == null) |
| | | { |
| | | foreach (var _renderer in m_Renderers) |
| | | { |
| | | _renderer.sortingOrder = _order; |
| | | _renderer.sortingLayerName = "UI"; |
| | | } |
| | | } |
| | | |
| | | public void PerformMask(RectMask2D[] _masks) |
| | | { |
| | | RectTransform _rect = null; |
| | | var _mask = _masks == null || _masks.Length == 0 ? null : _masks[0]; |
| | | if (_mask == null) |
| | | { |
| | | _rect = null; |
| | | } |
| | | else |
| | | { |
| | | _rect = _mask.rectTransform; |
| | | } |
| | | PerformMask(_rect); |
| | | } |
| | | |
| | | public void PerformMask(Mask[] _masks) |
| | | { |
| | | RectTransform _rect = null; |
| | | var _mask = _masks == null || _masks.Length == 0 ? null : _masks[0]; |
| | | if (_mask == null) |
| | | { |
| | | _rect = null; |
| | | } |
| | | else |
| | | { |
| | | _rect = _mask.rectTransform; |
| | | } |
| | | PerformMask(_rect); |
| | | } |
| | | |
| | | public void PerformMask(RectTransform _mask) |
| | | else |
| | | { |
| | | m_RectMask = _mask; |
| | | m_RectMaskEnable = _mask != null; |
| | | if (_mask != null) |
| | | { |
| | | m_RectMaskPosition = _mask.position; |
| | | _mask.GetWorldCorners(s_RectMask); |
| | | s_ClipRect.x = s_RectMask[0].x; s_ClipRect.y = s_RectMask[0].y; |
| | | s_ClipRect.z = s_RectMask[2].x; s_ClipRect.w = s_RectMask[2].y; |
| | | } |
| | | foreach (var _renderer in m_Renderers) |
| | | { |
| | | var _mat = _renderer.material; |
| | | SetClipRect(_mat, s_ClipRect, m_RectMaskEnable); |
| | | } |
| | | _rect = _mask.rectTransform; |
| | | } |
| | | PerformMask(_rect); |
| | | } |
| | | |
| | | private void GetComponent() |
| | | public void PerformMask(Mask[] _masks) |
| | | { |
| | | RectTransform _rect = null; |
| | | var _mask = _masks == null || _masks.Length == 0 ? null : _masks[0]; |
| | | if (_mask == null) |
| | | { |
| | | transform.GetComponentsInChildren(true, m_Renderers); |
| | | IsAnimatorControl = false; |
| | | TraverseChild(transform); |
| | | _rect = null; |
| | | } |
| | | |
| | | private void TraverseChild(Transform parent) |
| | | else |
| | | { |
| | | ParticleSystem particle = parent.GetComponent<ParticleSystem>(); |
| | | if (particle != null) |
| | | { |
| | | if (particle.main.duration > duration && !IsAnimatorControl) |
| | | { |
| | | duration = particle.main.duration; |
| | | } |
| | | m_Particles.Add(particle); |
| | | } |
| | | Animator animator = parent.GetComponent<Animator>(); |
| | | if (animator != null) |
| | | { |
| | | m_Animators.Add(animator); |
| | | AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(0); |
| | | duration = state.length > duration ? state.length : duration; |
| | | IsAnimatorControl = true; |
| | | } |
| | | foreach (Transform child in parent) |
| | | { |
| | | TraverseChild(child); |
| | | } |
| | | _rect = _mask.rectTransform; |
| | | } |
| | | PerformMask(_rect); |
| | | } |
| | | |
| | | private Vector4 s_ClipRect = Vector4.zero; |
| | | private Vector3[] s_RectMask = new Vector3[4]; |
| | | |
| | | private void SetClipRect(Material _mat, Vector4 _clipRect, bool _mask) |
| | | public void PerformMask(RectTransform _mask) |
| | | { |
| | | m_RectMask = _mask; |
| | | m_RectMaskEnable = _mask != null; |
| | | if (_mask != null) |
| | | { |
| | | _mat.SetVector("_ClipRect", _clipRect); |
| | | _mat.SetFloat("_UseClipRect", _mask ? 1 : 0); |
| | | m_RectMaskPosition = _mask.position; |
| | | _mask.GetWorldCorners(s_RectMask); |
| | | s_ClipRect.x = s_RectMask[0].x; s_ClipRect.y = s_RectMask[0].y; |
| | | s_ClipRect.z = s_RectMask[2].x; s_ClipRect.w = s_RectMask[2].y; |
| | | } |
| | | |
| | | public Animator GetAnimator() |
| | | foreach (var _renderer in m_Renderers) |
| | | { |
| | | if (IsAnimatorControl && m_Animators.Count > 0) |
| | | var _mat = _renderer.material; |
| | | SetClipRect(_mat, s_ClipRect, m_RectMaskEnable); |
| | | } |
| | | } |
| | | |
| | | private void GetComponent() |
| | | { |
| | | transform.GetComponentsInChildren(true, m_Renderers); |
| | | IsAnimatorControl = false; |
| | | TraverseChild(transform); |
| | | } |
| | | |
| | | private void TraverseChild(Transform parent) |
| | | { |
| | | ParticleSystem particle = parent.GetComponent<ParticleSystem>(); |
| | | if (particle != null) |
| | | { |
| | | if (particle.main.duration > duration && !IsAnimatorControl) |
| | | { |
| | | return m_Animators[0]; |
| | | duration = particle.main.duration; |
| | | } |
| | | return null; |
| | | m_Particles.Add(particle); |
| | | } |
| | | |
| | | private void LateUpdate() |
| | | Animator animator = parent.GetComponent<Animator>(); |
| | | if (animator != null) |
| | | { |
| | | if (m_RectMask != null && (m_RectMaskPosition != m_RectMask.position)) |
| | | { |
| | | m_RectMaskPosition = m_RectMask.position; |
| | | PerformMask(m_RectMask); |
| | | } |
| | | m_Animators.Add(animator); |
| | | AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(0); |
| | | duration = state.length > duration ? state.length : duration; |
| | | IsAnimatorControl = true; |
| | | } |
| | | |
| | | private void OnDisable() |
| | | foreach (Transform child in parent) |
| | | { |
| | | m_RectMask = null; |
| | | TraverseChild(child); |
| | | } |
| | | } |
| | | |
| | | private Vector4 s_ClipRect = Vector4.zero; |
| | | private Vector3[] s_RectMask = new Vector3[4]; |
| | | |
| | | private void SetClipRect(Material _mat, Vector4 _clipRect, bool _mask) |
| | | { |
| | | _mat.SetVector("_ClipRect", _clipRect); |
| | | _mat.SetFloat("_UseClipRect", _mask ? 1 : 0); |
| | | } |
| | | |
| | | public Animator GetAnimator() |
| | | { |
| | | if (IsAnimatorControl && m_Animators.Count > 0) |
| | | { |
| | | return m_Animators[0]; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private void LateUpdate() |
| | | { |
| | | if (m_RectMask != null && (m_RectMaskPosition != m_RectMask.position)) |
| | | { |
| | | m_RectMaskPosition = m_RectMask.position; |
| | | PerformMask(m_RectMask); |
| | | } |
| | | } |
| | | |
| | | private void OnDisable() |
| | | { |
| | | m_RectMask = null; |
| | | } |
| | | } |
| | | |