using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using System;
|
using UnityEngine.Rendering;
|
using UnityEngine.UI;
|
|
#if UNITY_EDITOR
|
using UnityEditor;
|
#endif
|
|
|
|
public class UIEffect : MonoBehaviour
|
{
|
public int renderQueue = 3000;
|
public int effect = 0;
|
public UIEffectBehaviour target;
|
public Vector3 effectPos = Vector3.zero;
|
public Vector3 effectRot = Vector3.zero;
|
public Vector3 effectScl = Vector3.one;
|
public bool playOnAwake = false;
|
public bool loop = false;
|
public bool keep = false;
|
public float duration { get; private set; }
|
public bool IsPlaying { get; protected set; }
|
public Action OnComplete;
|
[SerializeField] RectTransform m_MaskArea;
|
public RectTransform maskArea
|
{
|
get { return m_MaskArea; }
|
set { m_MaskArea = value; }
|
}
|
private UIBase retrospectWindow;
|
public void Play()
|
{
|
if (EffectMgr.Instance.IsNotShowBySetting(effect))
|
return;
|
if (target != null)
|
{
|
StopImediatly();
|
}
|
target = EffectMgr.Instance.GetUIEffect(effect, true);
|
if (target != null)
|
{
|
Active();
|
}
|
}
|
|
public void Active()
|
{
|
target.SetActive(true);
|
target.transform.SetParent(this.transform);
|
retrospectWindow = RetrospectWindow(transform);
|
duration = target.duration;
|
target.SetOrder(renderQueue);
|
target.transform.localPosition = effectPos;
|
target.transform.localEulerAngles = effectRot;
|
target.transform.localScale = effectScl;
|
target.SetParticlesScale(effectScl);
|
RockonEffect();
|
IsPlaying = true;
|
var _effectCfg = EffectConfig.Get(effect);
|
if (_effectCfg != null)
|
{
|
SoundPlayer.Instance.PlayUIAudio(_effectCfg.audio);
|
}
|
SetMask();
|
}
|
|
public void ResetOrder(int _order)
|
{
|
renderQueue = _order;
|
if (target != null)
|
{
|
target.SetOrder(renderQueue);
|
}
|
}
|
|
public void SetMask()
|
{
|
if (target == null)
|
{
|
return;
|
}
|
if (maskArea != null)
|
{
|
target.PerformMask(maskArea);
|
return;
|
}
|
var _masks = GetComponentsInParent<RectMask2D>(true);
|
if (_masks != null && _masks.Length > 0)
|
{
|
target.PerformMask(_masks);
|
}
|
else
|
{
|
var _smoothMask = GetComponentsInParent<SmoothMask>(true);
|
if (_smoothMask != null && _smoothMask.Length > 0)
|
{
|
target.PerformMask(_smoothMask[0].rectTransform);
|
return;
|
}
|
target.PerformMask(GetComponentsInParent<Mask>(true));
|
}
|
}
|
|
private void OnWinPreClose(UIBase _window)
|
{
|
if (retrospectWindow != null)
|
{
|
if (retrospectWindow == _window)
|
{
|
StopImediatly();
|
}
|
}
|
}
|
private void RockonEffect()
|
{
|
if (!loop)
|
{
|
this.SetWait(duration);
|
this.DoWaitRestart();
|
this.OnWaitCompelete(OnEffectComplete);
|
}
|
}
|
|
private void OnEffectComplete(Component comp)
|
{
|
this.DoWaitStop();
|
if (target != null)
|
{
|
if (!keep)
|
{
|
EffectMgr.Instance.RecyleUIEffect(effect, target.gameObject);
|
target = null;
|
}
|
if (OnComplete != null)
|
{
|
OnComplete();
|
}
|
}
|
IsPlaying = false;
|
}
|
|
private void OnEnable()
|
{
|
UIManager.Instance.OnCloseWindow += OnWinPreClose;
|
if (playOnAwake)
|
{
|
Play();
|
}
|
}
|
|
private void OnDisable()
|
{
|
UIManager.Instance.OnCloseWindow -= OnWinPreClose;
|
if (target != null && !playOnAwake)
|
{
|
StopImediatly();
|
}
|
}
|
|
public void Stop()
|
{
|
OnEffectComplete(this);
|
}
|
public void StopImediatly()
|
{
|
this.DoWaitStop();
|
if (target != null)
|
{
|
EffectMgr.Instance.RecyleUIEffect(effect, target.gameObject);
|
target = null;
|
}
|
IsPlaying = false;
|
}
|
public void SetLayer(int _layer)
|
{
|
if (target != null)
|
{
|
target.gameObject.SetLayer(_layer, true);
|
}
|
}
|
|
|
private UIBase RetrospectWindow(Transform parent)
|
{
|
if (parent == null)
|
{
|
return null;
|
}
|
UIBase window = null;
|
window = parent.GetComponent<UIBase>();
|
if (window != null)
|
{
|
return window;
|
}
|
else
|
{
|
return RetrospectWindow(parent.parent);
|
}
|
}
|
|
private const string EDITOR_EFFECT_PATH = "Assets/ResourcesOut/Effect/UI/";
|
private const string EFFECT_EXTENAL = ".prefab";
|
public void Preview(string _name)
|
{
|
#if UNITY_EDITOR
|
if (target != null)
|
{
|
DestroyImmediate(target);
|
}
|
var _prefab = AssetDatabase.LoadAssetAtPath<GameObject>(StringUtility.Contact(EDITOR_EFFECT_PATH, _name, EFFECT_EXTENAL));
|
if (_prefab != null)
|
{
|
_prefab = Instantiate(_prefab);
|
}
|
target = _prefab.AddComponent<UIEffectBehaviour>();
|
target.SetActive(true);
|
target.transform.SetParent(this.transform);
|
target.SetOrder(renderQueue);
|
//target.PerformMask(GetComponentsInParent<RectMask2D>(true));
|
target.transform.localPosition = Vector3.zero;
|
target.transform.localEulerAngles = Vector3.zero;
|
target.transform.localScale = Vector3.one;
|
Selection.activeGameObject = target.gameObject;
|
#endif
|
}
|
|
}
|
#if UNITY_EDITOR
|
[CustomEditor(typeof(UIEffect))]
|
[CanEditMultipleObjects]
|
public class UIEffectEditor : Editor
|
{
|
public UIEffect uieffect;
|
private string effectPath = string.Empty;
|
|
private void OnEnable()
|
{
|
if (target != null)
|
{
|
uieffect = target as UIEffect;
|
}
|
}
|
|
public override void OnInspectorGUI()
|
{
|
if (uieffect == null)
|
{
|
return;
|
}
|
EditorGUILayout.BeginHorizontal();
|
effectPath = EditorGUILayout.TextField("Effect Path", effectPath);
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
if (GUILayout.Button("Preview"))
|
{
|
uieffect.Preview(effectPath);
|
}
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
uieffect.playOnAwake = EditorGUILayout.Toggle("OnEnable", uieffect.playOnAwake);
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
uieffect.effect = EditorGUILayout.IntField("Effect ID", uieffect.effect);
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
uieffect.renderQueue = EditorGUILayout.IntField("RenderQueue", uieffect.renderQueue);
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
uieffect.loop = EditorGUILayout.Toggle("Loop", uieffect.loop);
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
uieffect.maskArea = EditorGUILayout.ObjectField("Mask", uieffect.maskArea, typeof(RectTransform), true) as RectTransform;
|
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
if (GUILayout.Button("ResetOrder"))
|
{
|
uieffect.target.SetOrder(uieffect.renderQueue);
|
}
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.BeginHorizontal();
|
uieffect.effectScl = EditorGUILayout.Vector3Field("Scale", uieffect.effectScl);
|
EditorGUILayout.EndHorizontal();
|
}
|
}
|
#endif
|