@@ -24,12 +24,7 @@ public partial class LoadBase
2424 /// <summary>
2525 /// AssetBundle的引用
2626 /// </summary>
27- public AssetBundle AssetBundle ;
28-
29- /// <summary>
30- /// 加载完成后需要执行的Task
31- /// </summary>
32- private List < ETTask > _loadFinishTasks = new List < ETTask > ( ) ;
27+ public AssetBundle AssetBundle = null ;
3328
3429 /// <summary>
3530 /// 需要统计进度
@@ -82,7 +77,7 @@ internal void LoadAssetBundle(string bundlePackageName)
8277 return ;
8378 }
8479 }
85-
80+ //资源没有加载过也没有正在加载就同步加载出来
8681 if ( AssetComponent . BundleNameToRuntimeInfo [ bundlePackageName ] . Encrypt )
8782 {
8883 string assetBundlePath = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , true ) ;
@@ -95,103 +90,86 @@ internal void LoadAssetBundle(string bundlePackageName)
9590 AssetBundle = AssetBundle . LoadFromFile ( assetBundlePath ) ;
9691 }
9792 _loadState = LoadState . Finish ;
98- for ( int i = 0 ; i < _loadFinishTasks . Count ; i ++ )
99- {
100- _loadFinishTasks [ i ] . SetResult ( ) ;
101- }
102- _loadFinishTasks . Clear ( ) ;
10393 }
10494
105- internal async ETTask LoadAssetBundleAsync ( ETTask tcs , string bundlePackageName )
95+ /// <summary>
96+ /// 异步加载LoadBase的AssetBundle
97+ /// </summary>
98+ internal async ETTask LoadAssetBundleAsync ( string bundlePackageName )
10699 {
107100 AddRefCount ( ) ;
108101 if ( _loadState == LoadState . Finish )
109102 {
110- tcs . SetResult ( ) ;
111- return ;
112- }
113-
114-
115- string path = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , true ) ;
116- CoroutineLock coroutineLock = await CoroutineLockComponent . Wait ( CoroutineLockType . BundleMaster , LoadPathConvertHelper . LoadPathConvert ( path ) ) ;
117-
118- coroutineLock . Dispose ( ) ;
119-
120-
121- if ( _loadState == LoadState . Loading )
122- {
123- _loadFinishTasks . Add ( tcs ) ;
124103 return ;
125104 }
126- _loadFinishTasks . Add ( tcs ) ;
127- _loadState = LoadState . Loading ;
128-
129- if ( AssetComponent . BundleNameToRuntimeInfo [ bundlePackageName ] . Encrypt )
130- {
131- string assetBundlePath = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , true ) ;
132- await LoadDataFinish ( assetBundlePath , bundlePackageName ) ;
133- }
134- else
105+ BundleRuntimeInfo bundleRuntimeInfo = AssetComponent . BundleNameToRuntimeInfo [ bundlePackageName ] ;
106+ string assetBundlePath = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , bundleRuntimeInfo . Encrypt ) ;
107+ //获取一个协程锁
108+ CoroutineLock coroutineLock = await CoroutineLockComponent . Wait ( CoroutineLockType . BundleMaster , LoadPathConvertHelper . LoadPathConvert ( assetBundlePath ) ) ;
109+ if ( _loadState == LoadState . NoLoad )
135110 {
136- string assetBundlePath = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , false ) ;
137- LoadBundleFinish ( assetBundlePath ) ;
111+ _loadState = LoadState . Loading ;
112+ if ( bundleRuntimeInfo . Encrypt )
113+ {
114+ await LoadDataFinish ( assetBundlePath , bundleRuntimeInfo . SecretKey ) ;
115+ }
116+ else
117+ {
118+ await LoadBundleFinish ( assetBundlePath ) ;
119+ }
120+ _loadState = LoadState . Finish ;
138121 }
122+ //协程锁解锁
123+ coroutineLock . Dispose ( ) ;
139124 }
140125
141126 /// <summary>
142127 /// 通过Byte加载完成(只有启用了异或加密才使用此加载方式)
143128 /// </summary>
144- private async ETTask LoadDataFinish ( string assetBundlePath , string bundlePackageName )
129+ private async ETTask LoadDataFinish ( string assetBundlePath , char [ ] bundlePackageSecretKey )
145130 {
146- byte [ ] data = await VerifyHelper . GetDecryptDataAsync ( assetBundlePath , _loadProgress , AssetComponent . BundleNameToRuntimeInfo [ bundlePackageName ] . SecretKey ) ;
131+ byte [ ] data = await VerifyHelper . GetDecryptDataAsync ( assetBundlePath , _loadProgress , bundlePackageSecretKey ) ;
147132 if ( _loadState == LoadState . Finish )
148133 {
149134 return ;
150135 }
136+ ETTask tcs = ETTask . Create ( true ) ;
151137 _assetBundleCreateRequest = AssetBundle . LoadFromMemoryAsync ( data ) ;
152138 _assetBundleCreateRequest . completed += operation =>
153139 {
154140 AssetBundle = _assetBundleCreateRequest . assetBundle ;
155- for ( int i = 0 ; i < _loadFinishTasks . Count ; i ++ )
156- {
157- _loadFinishTasks [ i ] . SetResult ( ) ;
158- }
159- _loadFinishTasks . Clear ( ) ;
160- _loadState = LoadState . Finish ;
141+ tcs . SetResult ( ) ;
161142 //判断是否还需要
162143 if ( _refCount <= 0 )
163144 {
164145 AssetComponent . AddPreUnLoadPool ( this ) ;
165146 }
166147 } ;
148+ await tcs ;
167149 }
168150
169151 /// <summary>
170152 /// 通过路径直接加载硬盘上的AssetBundle
171153 /// </summary>
172- /// <param name="assetBundlePath"></param>
173- private void LoadBundleFinish ( string assetBundlePath )
154+ private async ETTask LoadBundleFinish ( string assetBundlePath )
174155 {
175156 if ( _loadState == LoadState . Finish )
176157 {
177158 return ;
178159 }
160+ ETTask tcs = ETTask . Create ( true ) ;
179161 _assetBundleCreateRequest = AssetBundle . LoadFromFileAsync ( assetBundlePath ) ;
180162 _assetBundleCreateRequest . completed += operation =>
181163 {
182164 AssetBundle = _assetBundleCreateRequest . assetBundle ;
183- for ( int i = 0 ; i < _loadFinishTasks . Count ; i ++ )
184- {
185- _loadFinishTasks [ i ] . SetResult ( ) ;
186- }
187- _loadFinishTasks . Clear ( ) ;
188- _loadState = LoadState . Finish ;
165+ tcs . SetResult ( ) ;
189166 //判断是否还需要
190167 if ( _refCount <= 0 )
191168 {
192169 AssetComponent . AddPreUnLoadPool ( this ) ;
193170 }
194171 } ;
172+ await tcs ;
195173 }
196174
197175 /// <summary>
@@ -213,7 +191,7 @@ internal void ForceLoadFinish(string bundlePackageName)
213191
214192 if ( AssetComponent . BundleNameToRuntimeInfo [ bundlePackageName ] . Encrypt )
215193 {
216- string assetBundlePath = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , true ) ;
194+ string assetBundlePath = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , AssetComponent . BundleNameToRuntimeInfo [ bundlePackageName ] . Encrypt ) ;
217195 byte [ ] data = VerifyHelper . GetDecryptData ( assetBundlePath , AssetComponent . BundleNameToRuntimeInfo [ bundlePackageName ] . SecretKey ) ;
218196 AssetBundle = AssetBundle . LoadFromMemory ( data ) ;
219197 }
@@ -222,11 +200,6 @@ internal void ForceLoadFinish(string bundlePackageName)
222200 string assetBundlePath = AssetComponent . BundleFileExistPath ( bundlePackageName , AssetBundleName , false ) ;
223201 AssetBundle = AssetBundle . LoadFromFile ( assetBundlePath ) ;
224202 }
225- for ( int i = 0 ; i < _loadFinishTasks . Count ; i ++ )
226- {
227- _loadFinishTasks [ i ] . SetResult ( ) ;
228- }
229- _loadFinishTasks . Clear ( ) ;
230203 _loadState = LoadState . Finish ;
231204 //判断是否还需要
232205 if ( _refCount <= 0 )
0 commit comments