@@ -49,6 +49,12 @@ public List<string> AllAssetPaths
49
49
return _allAssetPaths ;
50
50
}
51
51
}
52
+ /// <summary>
53
+ /// 预加载的所有资源
54
+ /// </summary>
55
+ private Dictionary < string , Object > _preloadAssets = new Dictionary < string , Object > ( ) ;
56
+ //异步加载锁定的文件
57
+ private HashSet < string > _asyncLockFiles = new HashSet < string > ( ) ;
52
58
53
59
/// <summary>
54
60
/// 设置资源准备
@@ -84,10 +90,7 @@ public void SetResource(Action callback)
84
90
StartCoroutine ( LoadPlatformMainfest ( _readPath , callback ) ) ;
85
91
}
86
92
87
- /// <summary>
88
- /// 预加载的所有资源
89
- /// </summary>
90
- private Dictionary < string , Object > _preloadAssets = new Dictionary < string , Object > ( ) ;
93
+
91
94
92
95
/// <summary>
93
96
/// 资源预加载
@@ -427,16 +430,19 @@ private IEnumerator LoadAssetBundleFromPathAsync(string path, Action<AssetBundle
427
430
if ( ! File . Exists ( path ) )
428
431
throw new GameException ( "Assetbundle not found :" + path ) ;
429
432
430
- using ( Stream abStream = _isEncrypt ? new EncryptFileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , 1024 * 4 , false ) : new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , 1024 * 4 , false ) )
433
+ if ( _asyncLockFiles . Contains ( path ) )
431
434
{
432
- while ( ! abStream . CanRead )
435
+ string fileName = Path . GetFileNameWithoutExtension ( path ) ;
436
+ while ( ! _liveAssetBundle . ContainsKey ( fileName ) )
433
437
{
434
438
yield return null ;
435
439
}
436
- string fileName = Path . GetFileNameWithoutExtension ( path ) ;
437
- yield return null ;
438
- AssetBundle assetBundle ;
439
- if ( ! _liveAssetBundle . TryGetValue ( fileName , out assetBundle ) )
440
+ callback . Invoke ( _liveAssetBundle [ fileName ] ) ;
441
+ }
442
+ else
443
+ {
444
+ _asyncLockFiles . Add ( path ) ;
445
+ using ( Stream abStream = _isEncrypt ? new EncryptFileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , 1024 * 4 , false ) : new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . None , 1024 * 4 , false ) )
440
446
{
441
447
float loadTime = Time . realtimeSinceStartup ;
442
448
var ablfsa = AssetBundle . LoadFromStreamAsync ( abStream ) ;
@@ -448,9 +454,10 @@ private IEnumerator LoadAssetBundleFromPathAsync(string path, Action<AssetBundle
448
454
yield return null ;
449
455
loadTime = Time . realtimeSinceStartup - loadTime ;
450
456
Debug . Log ( $ "LoadAssetBundleFromPath Async time spent: { loadTime } { path } ") ;
451
- assetBundle = ablfsa . assetBundle ;
457
+ callback ? . Invoke ( ablfsa . assetBundle ) ;
452
458
}
453
- callback ? . Invoke ( assetBundle ) ;
459
+ yield return null ;
460
+ _asyncLockFiles . Remove ( path ) ;
454
461
}
455
462
}
456
463
#endregion
0 commit comments