yyl
2025-06-09 b9751b2f076ee050fe5b685e91ae4fc4469b1015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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