@@ -25,14 +25,14 @@ public sealed class UIManager :GameFrameworkModule
25
25
//资源管理器
26
26
private ResourceManager _resource ;
27
27
//ui堆栈
28
- private Stack < UIViewAttribute > _stackUiAsset = new Stack < UIViewAttribute > ( ) ;
28
+ private Stack < AssetConfig > _stackUiAsset = new Stack < AssetConfig > ( ) ;
29
29
//所有的uiview
30
- private readonly Dictionary < UIViewAttribute , UIView > _allUiViews = new Dictionary < UIViewAttribute , UIView > ( ) ;
30
+ private readonly Dictionary < AssetConfig , UIView > _allUiViews = new Dictionary < AssetConfig , UIView > ( ) ;
31
31
//默认的uipath路径
32
- private readonly Dictionary < int , UIViewAttribute > _uiAssetPath =
33
- new Dictionary < int , UIViewAttribute > ( ) ;
32
+ private readonly Dictionary < int , AssetConfig > _uiAssetPath =
33
+ new Dictionary < int , AssetConfig > ( ) ;
34
34
//所有的uiAsset
35
- private readonly Dictionary < int , UIViewAttribute > _allUiAssets = new Dictionary < int , UIViewAttribute > ( ) ;
35
+ private readonly Dictionary < int , AssetConfig > _allUiAssets = new Dictionary < int , AssetConfig > ( ) ;
36
36
#region 构造函数
37
37
public UIManager ( )
38
38
{
@@ -51,18 +51,18 @@ public UIManager()
51
51
#region 外部接口
52
52
public void Push < T > ( bool allowMulti = false , params object [ ] parameters ) where T : UIView
53
53
{
54
- UIViewAttribute uiViewAttribute = CheckAssetPath ( typeof ( T ) ) ;
55
- if ( uiViewAttribute == null )
54
+ AssetConfig assetConfig = CheckAssetPath ( typeof ( T ) ) ;
55
+ if ( assetConfig == null )
56
56
return ;
57
57
58
58
if ( _stackUiAsset . Count > 0 )
59
59
{
60
- UIViewAttribute lastUiViewAttribute = _stackUiAsset . Peek ( ) ;
60
+ AssetConfig lastAssetConfig = _stackUiAsset . Peek ( ) ;
61
61
//如果界面已经打开 则不在执行
62
- if ( Equals ( lastUiViewAttribute , uiViewAttribute ) && ! allowMulti )
62
+ if ( Equals ( lastAssetConfig , assetConfig ) && ! allowMulti )
63
63
return ;
64
64
65
- IUIView uiView = GetUiView ( lastUiViewAttribute ) ;
65
+ IUIView uiView = GetUiView ( lastAssetConfig ) ;
66
66
67
67
//触发暂停事件
68
68
_uiPauseArgs . UIView = uiView ;
@@ -71,12 +71,14 @@ public void Push<T>(bool allowMulti = false, params object[] parameters) where T
71
71
uiView . OnPause ( ) ;
72
72
}
73
73
74
- UIViewAttribute newUIViewAttribute = uiViewAttribute ;
74
+ AssetConfig newAssetConfig = null ;
75
75
if ( allowMulti )
76
- newUIViewAttribute = new UIViewAttribute ( uiViewAttribute . AssetBundleName , uiViewAttribute . ViewPath ) ;
76
+ newAssetConfig = new AssetConfig ( assetConfig . AssetBundleName , assetConfig . AssetPath ) ;
77
+ else
78
+ newAssetConfig = assetConfig ;
77
79
78
- _stackUiAsset . Push ( newUIViewAttribute ) ;
79
- UIView newUiView = GetUiView ( newUIViewAttribute ) ;
80
+ _stackUiAsset . Push ( newAssetConfig ) ;
81
+ UIView newUiView = GetUiView ( newAssetConfig ) ;
80
82
newUiView . OnEnter ( parameters ) ;
81
83
82
84
//触发打开事件
@@ -90,9 +92,9 @@ public void Pop(bool isDestory = false)
90
92
//移除当前UI
91
93
if ( _stackUiAsset . Count > 0 )
92
94
{
93
- UIViewAttribute lastUIViewAttribute = _stackUiAsset . Pop ( ) ;
94
- UIView lastUiView ;
95
- if ( _allUiViews . TryGetValue ( lastUIViewAttribute , out lastUiView ) )
95
+ AssetConfig lastAssetConfig = _stackUiAsset . Pop ( ) ;
96
+ UIView lastUiView ;
97
+ if ( _allUiViews . TryGetValue ( lastAssetConfig , out lastUiView ) )
96
98
{
97
99
//触发关闭事件
98
100
_uiExitArgs . UIView = lastUiView ;
@@ -101,7 +103,7 @@ public void Pop(bool isDestory = false)
101
103
lastUiView . OnExit ( ) ;
102
104
if ( isDestory )
103
105
{
104
- _allUiViews . Remove ( lastUIViewAttribute ) ;
106
+ _allUiViews . Remove ( lastAssetConfig ) ;
105
107
MonoBehaviour . Destroy ( lastUiView ) ;
106
108
}
107
109
else
@@ -111,9 +113,9 @@ public void Pop(bool isDestory = false)
111
113
112
114
if ( _stackUiAsset . Count > 0 )
113
115
{
114
- UIViewAttribute uiViewAttribute = _stackUiAsset . Peek ( ) ;
115
- UIView lastUiView ;
116
- if ( _allUiViews . TryGetValue ( uiViewAttribute , out lastUiView ) )
116
+ AssetConfig lastAssetConfig = _stackUiAsset . Peek ( ) ;
117
+ UIView lastUiView ;
118
+ if ( _allUiViews . TryGetValue ( lastAssetConfig , out lastUiView ) )
117
119
{
118
120
lastUiView . OnResume ( ) ;
119
121
//触发恢复事件
@@ -129,40 +131,41 @@ public void Pop(bool isDestory = false)
129
131
130
132
#region 内部函数
131
133
//检查路径
132
- private UIViewAttribute CheckAssetPath ( Type t )
134
+ private AssetConfig CheckAssetPath ( Type t )
133
135
{
134
- int hashCode = t . GetHashCode ( ) ;
136
+ int hashCode = t . GetHashCode ( ) ;
135
137
136
- UIViewAttribute uiViewAttribute = null ;
137
- if ( ! _uiAssetPath . TryGetValue ( hashCode , out uiViewAttribute ) )
138
- {
139
- object [ ] attrs = t . GetCustomAttributes ( typeof ( UIViewAttribute ) , false ) ;
140
- if ( attrs . Length == 0 )
141
- return null ;
142
- uiViewAttribute = ( UIViewAttribute ) attrs [ 0 ] ;
143
- if ( string . IsNullOrEmpty ( uiViewAttribute . ViewPath )
144
- || string . IsNullOrEmpty ( uiViewAttribute . AssetBundleName ) )
145
- return null ;
146
- }
147
- return uiViewAttribute ;
148
- }
138
+ AssetConfig assetCofig = null ;
139
+ if ( ! _uiAssetPath . TryGetValue ( hashCode , out assetCofig ) )
140
+ {
141
+ object [ ] attrs = t . GetCustomAttributes ( typeof ( UIViewAttribute ) , false ) ;
142
+ if ( attrs . Length == 0 )
143
+ return null ;
144
+ UIViewAttribute uIViewAttribute = attrs [ 0 ] as UIViewAttribute ;
145
+
146
+ if ( string . IsNullOrEmpty ( uIViewAttribute ? . ViewPath ) || string . IsNullOrEmpty ( uIViewAttribute . AssetBundleName ) )
147
+ return null ;
148
+ assetCofig = new AssetConfig ( uIViewAttribute . AssetBundleName , uIViewAttribute . ViewPath ) ;
149
+ }
150
+ return assetCofig ;
151
+ }
149
152
150
153
151
154
//获取ui界面
152
- private UIView GetUiView ( UIViewAttribute uiViewAttribute )
155
+ private UIView GetUiView ( AssetConfig assetConfig )
153
156
{
154
157
UIView uiView ;
155
- if ( ! _allUiViews . TryGetValue ( uiViewAttribute , out uiView ) )
158
+ if ( ! _allUiViews . TryGetValue ( assetConfig , out uiView ) )
156
159
{
157
- GameObject uiViewSource = _resource . LoadAsset < GameObject > ( uiViewAttribute . AssetBundleName , uiViewAttribute . ViewPath ) ;
160
+ GameObject uiViewSource = _resource . LoadAsset < GameObject > ( assetConfig . AssetBundleName , assetConfig . AssetPath ) ;
158
161
if ( uiViewSource == null )
159
- throw new Exception ( "uiview path not found:" + uiViewAttribute . AssetBundleName + ":" + uiViewAttribute . ViewPath ) ;
162
+ throw new Exception ( "uiview path not found:" + assetConfig . AssetBundleName + ":" + assetConfig . AssetPath ) ;
160
163
161
164
GameObject uiViewClone = GameObject . Instantiate ( uiViewSource ) ;
162
165
uiView = uiViewClone . GetComponent < UIView > ( ) ;
163
166
if ( uiView == null )
164
167
return null ;
165
- _allUiViews [ uiViewAttribute ] = uiView ;
168
+ _allUiViews [ assetConfig ] = uiView ;
166
169
return uiView ;
167
170
}
168
171
uiView . gameObject . SetActive ( true ) ;
@@ -185,9 +188,27 @@ public override void OnClose()
185
188
}
186
189
_allUiViews . Clear ( ) ;
187
190
}
188
- #endregion
189
-
191
+ #endregion
192
+
193
+
194
+ #region 数据结构
195
+ //资源配置
196
+ private class AssetConfig
197
+ {
198
+ public string AssetBundleName ;
199
+ public string AssetPath ;
200
+
201
+ public AssetConfig ( )
202
+ {
203
+ }
190
204
205
+ public AssetConfig ( string assetBundleName , string assetPath )
206
+ {
207
+ AssetBundleName = assetBundleName ;
208
+ AssetPath = assetPath ;
209
+ }
210
+ }
211
+ #endregion
191
212
192
- }
213
+ }
193
214
}
0 commit comments