UIBase.cs
3.25 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIBase : MonoBehaviour, IUIBase
{
private UIManager uiManger;
/// <summary>
/// 是否是可以删除的UI
/// </summary>
public bool isCanDestroy = false;
/// <summary>
/// 是否打开
/// </summary>
public bool IsOpen;
/// <summary>
/// 是否正在播放动画
/// </summary>
private bool isAnimation = false;
/// <summary>
/// UI打开动画枚举
/// </summary>
public enum EUiAnimationType
{
None,
Fade,
Zoom
}
/// <summary>
/// UI打开得方式选择
/// </summary>
public EUiAnimationType animationType = EUiAnimationType.None;
#region 注册与删除
private void Awake()
{
IUIBase uiBase = this;
uiBase.RegisterUI();
}
private void OnDestroy()
{
IUIBase uiBase = this;
uiBase.UnRegisterUI();
}
#endregion
public void Open(UIManager uiManager)
{
uiManger = uiManager;
IsOpen = true;
gameObject.transform.SetAsFirstSibling();
SwitchOpenAnimation();
}
public void Close(UIManager uiManager)
{
uiManger = uiManager;
IsOpen = false;
SwitchCloseAnimation();
}
public UIBase GetUI()
{
return this;
}
#region 基类打开与关闭回调
/// <summary>
/// 打开后完成回调 子类重写
/// </summary>
protected virtual void OpenComplete()
{
isAnimation = false;
gameObject.transform.SetAsLastSibling();
}
/// <summary>
/// 关闭后的完成回调 子类重写
/// </summary>
protected virtual void CloseComplete()
{
isAnimation = false;
if (isCanDestroy)
{
uiManger.UnRegisterUI(this);
Destroy(gameObject);
}
}
#endregion
#region 打开方式与动画选择
private void SwitchOpenAnimation()
{
// 动画没放完 return
if (isAnimation)
{
return;
}
isAnimation = true;
switch (animationType)
{
case EUiAnimationType.None:
gameObject.SetActive(true);
OpenComplete();
break;
case EUiAnimationType.Zoom:
UIAnimation.ZoomIn(gameObject, OpenComplete);
break;
case EUiAnimationType.Fade:
gameObject.transform.localScale = Vector3.one;
UIAnimation.FadeIn(gameObject, OpenComplete);
break;
}
}
private void SwitchCloseAnimation()
{
// 动画没放完 return
if (isAnimation)
{
return;
}
isAnimation = true;
switch (animationType)
{
case EUiAnimationType.None:
gameObject.SetActive(false);
CloseComplete();
break;
case EUiAnimationType.Zoom:
UIAnimation.ZoomOut(gameObject, CloseComplete);
break;
case EUiAnimationType.Fade:
UIAnimation.FadeOut(gameObject, CloseComplete);
break;
}
}
#endregion
}