@@ -70,27 +70,78 @@ public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBu
70
70
LoadPlatformMainfest ( rootAbPath ) ;
71
71
}
72
72
73
- /// <summary>
74
- /// 加载资源
75
- /// </summary>
76
- /// <typeparam name="T"></typeparam>
77
- /// <param name="assetBundleName"></param>
78
- /// <param name="assetName"></param>
79
- /// <returns></returns>
80
- public async Task < T > LoadAsset < T > ( string assetBundleName , string assetName ) where T : Object
73
+ /// <summary>
74
+ /// 加载ab包
75
+ /// </summary>
76
+ /// <param name="assetBundleName"></param>
77
+ /// <returns></returns>
78
+ public async Task < AssetBundle > LoadAssetBundle ( string assetBundleName )
79
+ {
80
+ //加载Assetbundle
81
+ AssetBundle assetBundle ;
82
+ KeyValuePair < AssetBundle , string [ ] > assetBundles ;
83
+ if ( ! _allAssetBundles . ContainsKey ( assetBundleName ) )
84
+ {
85
+ string assetBundlePath = Path . Combine ( _readPath , assetBundleName ) ;
86
+ if ( ! File . Exists ( assetBundlePath ) )
87
+ throw new GamekException ( "AssetBundle is Null" ) ;
88
+ //加载assetbundle
89
+ assetBundle = await LoadAssetBundleFromPath ( assetBundlePath ) ;
90
+ //存储资源名称
91
+ string [ ] assetNames = assetBundle . GetAllAssetNames ( ) ;
92
+ if ( assetBundle . isStreamedSceneAssetBundle )
93
+ assetNames = assetBundle . GetAllScenePaths ( ) ;
94
+ foreach ( var name in assetNames )
95
+ {
96
+ if ( ! _allAssets . ContainsKey ( name ) )
97
+ _allAssets . Add ( name , assetBundle ) ;
98
+ }
99
+ //存储assetbundle
100
+ assetBundles = new KeyValuePair < AssetBundle , string [ ] > ( assetBundle , assetNames ) ;
101
+ _allAssetBundles [ assetBundleName ] = assetBundles ;
102
+ }
103
+ return assetBundles . Key ;
104
+ }
105
+
106
+ /// <summary>
107
+ /// 加载资源 同步加载
108
+ /// </summary>
109
+ /// <typeparam name="T"></typeparam>
110
+ /// <param name="assetBundleName"></param>
111
+ /// <param name="assetName"></param>
112
+ /// <returns></returns>
113
+ public T LoadAssetSync < T > ( string assetBundleName , string assetName ) where T : Object
114
+ {
115
+ AssetBundle assetBundle ;
116
+ if ( _allAssets . TryGetValue ( assetName , out assetBundle ) )
117
+ {
118
+ return assetBundle . LoadAsset < T > ( assetName ) ;
119
+ }
120
+ return null ;
121
+ }
122
+
123
+ /// <summary>
124
+ /// 加载资源
125
+ /// </summary>
126
+ /// <typeparam name="T"></typeparam>
127
+ /// <param name="assetBundleName"></param>
128
+ /// <param name="assetName"></param>
129
+ /// <returns></returns>
130
+ public async Task < T > LoadAsset < T > ( string assetBundleName , string assetName ) where T : Object
81
131
{
82
132
//转小写
83
133
assetName = assetName . ToLower ( ) ;
84
134
85
135
//加载Assetbundle
86
136
AssetBundle assetBundle ;
137
+
87
138
if ( ! _allAssets . TryGetValue ( assetName , out assetBundle ) )
88
139
{
89
140
string assetBundlePath = Path . Combine ( _readPath , assetBundleName ) ;
90
141
if ( ! File . Exists ( assetBundlePath ) )
91
142
throw new GamekException ( "AssetBundle is Null" ) ;
92
143
//加载assetbundle
93
- assetBundle = await LoadAssetBundle ( assetBundlePath ) ;
144
+ assetBundle = await LoadAssetBundleFromPath ( assetBundlePath ) ;
94
145
//存储资源名称
95
146
string [ ] assetNames = assetBundle . GetAllAssetNames ( ) ;
96
147
if ( assetBundle . isStreamedSceneAssetBundle )
@@ -105,7 +156,8 @@ public async Task<T> LoadAsset<T>(string assetBundleName, string assetName) wher
105
156
}
106
157
107
158
//加载依赖项
108
- LoadDependenciesAssetBundel ( assetBundleName ) ;
159
+ await LoadDependenciesAssetBundel ( assetBundleName ) ;
160
+
109
161
//加载资源
110
162
var asset = await assetBundle . LoadAssetAsync ( assetName ) ;
111
163
@@ -148,9 +200,9 @@ public async Task<AsyncOperation> LoadSceneAsync(string assetBundleName, string
148
200
{
149
201
string assetBundlePath = Path . Combine ( _readPath , assetBundleName ) ;
150
202
151
- AssetBundle assetBundle = await LoadAssetBundle ( assetBundlePath ) ;
203
+ AssetBundle assetBundle = await LoadAssetBundleFromPath ( assetBundlePath ) ;
152
204
//加载依赖项
153
- LoadDependenciesAssetBundel ( assetBundleName ) ;
205
+ await LoadDependenciesAssetBundel ( assetBundleName ) ;
154
206
155
207
asyncOperation = SceneManager . LoadSceneAsync ( sceneName , mode ) ;
156
208
//场景加载完成卸载相关的引用
@@ -202,7 +254,7 @@ private async void LoadPlatformMainfest(string rootBundlePath)
202
254
{
203
255
try
204
256
{
205
- AssetBundle mainfestAssetBundle = await LoadAssetBundle ( rootBundlePath ) ;
257
+ AssetBundle mainfestAssetBundle = await LoadAssetBundleFromPath ( rootBundlePath ) ;
206
258
_mainfest = mainfestAssetBundle . LoadAsset < AssetBundleManifest > ( "AssetBundleManifest" ) ;
207
259
mainfestAssetBundle . Unload ( false ) ;
208
260
}
@@ -211,10 +263,9 @@ private async void LoadPlatformMainfest(string rootBundlePath)
211
263
Debug . LogError ( ex . ToString ( ) ) ;
212
264
}
213
265
}
214
-
215
-
216
- //同步加载AssetBundle
217
- private async Task < AssetBundle > LoadAssetBundle ( string path )
266
+
267
+ //同步加载AssetBundle
268
+ private async Task < AssetBundle > LoadAssetBundleFromPath ( string path )
218
269
{
219
270
if ( ! File . Exists ( path ) )
220
271
throw new Exception ( "assetbundle not found :" + path ) ;
@@ -236,28 +287,9 @@ private async Task<AssetBundle> LoadAssetBundle(string path)
236
287
237
288
return mainfestAssetBundle ;
238
289
}
239
-
240
- //异步加载AssetBundle
241
- private AssetBundleCreateRequest LoadAssetBundleAsync ( string path )
242
- {
243
- if ( ! File . Exists ( path ) )
244
- throw new Exception ( "assetbundle not found :" + path ) ;
245
- AssetBundleCreateRequest assetBundleCreateRequest ;
246
- if ( _enciphererkeyAsset != null )
247
- {
248
- byte [ ] datas = Encipherer . AESDecrypt ( File . ReadAllBytes ( path ) , _enciphererkeyAsset ) ;
249
- assetBundleCreateRequest = AssetBundle . LoadFromMemoryAsync ( datas ) ;
250
- }
251
- else
252
- assetBundleCreateRequest = AssetBundle . LoadFromFileAsync ( path ) ;
253
-
254
- return assetBundleCreateRequest ;
255
- }
256
-
257
-
258
-
290
+
259
291
//加载引用的assetbundle --引用的assetbundle不卸载
260
- private async void LoadDependenciesAssetBundel ( string assetBundleName )
292
+ private async Task LoadDependenciesAssetBundel ( string assetBundleName )
261
293
{
262
294
//加载相关依赖 依赖暂时不异步加载了
263
295
string [ ] dependencies = _mainfest . GetAllDependencies ( assetBundleName ) ;
@@ -267,7 +299,7 @@ private async void LoadDependenciesAssetBundel(string assetBundleName)
267
299
continue ;
268
300
269
301
string dependenciesBundlePath = Path . Combine ( _readPath , item ) ;
270
- AssetBundle assetBundle = await LoadAssetBundle ( dependenciesBundlePath ) ;
302
+ AssetBundle assetBundle = await LoadAssetBundleFromPath ( dependenciesBundlePath ) ;
271
303
272
304
//存储资源名称
273
305
string [ ] assetNames = assetBundle . GetAllAssetNames ( ) ;
0 commit comments