using System; using System.Collections.Generic; namespace BF { public class AssetLoader : LoaderBase { private List delayUnload; public AssetLoader() : base() { delayUnload = new List(); } public override AssetObject LoadAssetAsync(string assetPath, Type type, Action complete) { var aob = new AssetObject(assetPath, type); #if UNITY_EDITOR string path = char.ToUpper(assetPath[0]) + assetPath.Substring(1); var asset = UnityEditor.AssetDatabase.LoadAssetAtPath(path, type); if (ReferenceEquals(asset, null)) { throw new Exception("加载资源为空 " + path); } BFMain.Instance.OneShotManager.AddOneShot(() => { aob.AssetDataBaseLoadEnd(asset); }); // 延缓一帧执行 #endif return aob; } public override AssetObject LoadAssetSync(string assetPath, Type type) { var aob = new AssetObject(assetPath, type, true); #if UNITY_EDITOR string path = char.ToUpper(assetPath[0]) + assetPath.Substring(1); var asset = UnityEditor.AssetDatabase.LoadAssetAtPath(path, type); aob.LoadComplete(asset); #endif return aob; } public override void LoadAssetSync(AssetObject assetObject) { #if UNITY_EDITOR var asset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetObject.AssetPath, assetObject.AssetType); assetObject.LoadComplete(asset); assetObject.LoadSync(); #endif } public override void Unload(AssetObject asset, bool immediately) { if (EAssetLoadStatus.Unloading == asset.LoadStatus) { return; } if (EAssetLoadStatus.Unloaded == asset.LoadStatus) { resMgr.RemoveAsset(asset.AssetPath); } else if (asset.Unload(immediately)) { delayUnload.Add(asset); } } public override string GetSceneLoadPath(string assetBundlePath) { return assetBundlePath; } public override void LoadSceneAsync(string assetBundlePath, Action complete) { } public override void UnloadScene(string assetBundlePath) { } public override void Tick() { TickDelayUnload(false); } void TickDelayUnload(bool immediately) { var length = delayUnload.Count; for (int i = 0; i < length;) { var asset = delayUnload[i]; asset.UnloadCountdown(immediately); //卸载完成,或者卸载过程中,重新被加载使用 if (EAssetLoadStatus.Unloaded == asset.LoadStatus || EAssetLoadStatus.Unloading != asset.LoadStatus) { delayUnload.RemoveAt(i); --length; } else { ++i; } } } public override void UnloadAllDelayAssets() { TickDelayUnload(true); } public override void Clear() { } public override bool ContainsAsset(string assetPath) { return System.IO.File.Exists(assetPath); } } }