using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEditor; using UnityEngine; namespace Kill.Base { public class LoadRes : MonoBehaviour { [Serializable] public enum ResPosition { 本地 = 1, 资源包 = 2 } public ResPosition resPosition; public static LoadRes Instance; public Dictionary loadedBundles; public List commonList; void Awake() { Instance = this; } public async Task Init() { #if !UNITY_EDITOR resPosition=ResPosition.资源包; #endif if (resPosition == ResPosition.资源包) { loadedBundles = new Dictionary(); if (commonList != null) { foreach (string bundleName in commonList) { await LoadBundleAsync(bundleName); } } } await LanguageManager.Instance.Init(); UI.UIManager.Instance.Init(); } /// /// 异步加载AssetBundle /// /// Bundle名称 /// AssetBundle对象 public async Task LoadBundleAsync(string bundleName) { if (resPosition == ResPosition.本地) { return null; } // 检查是否已经加载过该bundle if (loadedBundles.ContainsKey(bundleName)) { return loadedBundles[bundleName]; } string localPath = Application.persistentDataPath + "/" + bundleName + ".ab"; AssetBundle bundleToReturn = null; try { if (System.IO.File.Exists(localPath)) { // 优先加载热更新后的包 var loadRequest = AssetBundle.LoadFromFileAsync(localPath); while (!loadRequest.isDone) { await Task.Yield(); } bundleToReturn = loadRequest.assetBundle; } else { // fallback:加载 StreamingAssets 中的原始包(首次安装时) string streamingPath = Application.streamingAssetsPath + "/" + bundleName + ".ab"; var loadRequest = AssetBundle.LoadFromFileAsync(streamingPath); while (!loadRequest.isDone) { await Task.Yield(); } bundleToReturn = loadRequest.assetBundle; } // 如果加载成功,添加到字典中避免下次重复加载 if (bundleToReturn != null) { loadedBundles.Add(bundleName, bundleToReturn); } } catch (Exception e) { Debug.LogError($"加载AssetBundle失败: {bundleName}, 错误: {e.Message}"); } return bundleToReturn; } /// /// 同步加载AssetBundle(保留原有方法) /// /// Bundle名称 /// AssetBundle对象 public AssetBundle LoadBundle(string bundleName) { if (resPosition == ResPosition.本地) { return null; } // 检查是否已经加载过该bundle if (loadedBundles.ContainsKey(bundleName)) { return loadedBundles[bundleName]; } string localPath = Application.persistentDataPath + "/" + bundleName + ".ab"; AssetBundle bundleToReturn = null; try { if (System.IO.File.Exists(localPath)) { // 优先加载热更新后的包 bundleToReturn = AssetBundle.LoadFromFile(localPath); } else { // fallback:加载 StreamingAssets 中的原始包(首次安装时) string streamingPath = Application.streamingAssetsPath + "/" + bundleName + ".ab"; bundleToReturn = AssetBundle.LoadFromFile(streamingPath); } // 如果加载成功,添加到字典中避免下次重复加载 if (bundleToReturn != null) { loadedBundles.Add(bundleName, bundleToReturn); } } catch (Exception e) { Debug.LogError($"加载AssetBundle失败: {bundleName}, 错误: {e.Message}"); } return bundleToReturn; } string localPath = "Assets/Res/"; /// /// 通用资源加载方法(同步) /// /// 资源类型 /// Bundle名称 /// 资源名称 /// 指定类型的资源 public T LoadAsset(string bundleName, string assetName) where T : UnityEngine.Object { #if UNITY_EDITOR if (resPosition == ResPosition.本地) { string[] paths = bundleName.Split('_'); string realPath = localPath; foreach (string path in paths) { realPath += path + "/"; } return AssetDatabase.LoadAssetAtPath(realPath + assetName); } #endif AssetBundle ab = LoadBundle(bundleName); if (ab != null) { return ab.LoadAsset(assetName); } return null; } /// /// 通用资源加载方法(异步) /// /// 资源类型 /// Bundle名称 /// 资源名称 /// 指定类型的资源 public async Task LoadAssetAsync(string bundleName, string assetName) where T : UnityEngine.Object { #if UNITY_EDITOR if (resPosition == ResPosition.本地) { string[] paths = bundleName.Split('_'); string realPath = localPath; foreach (string path in paths) { realPath += path + "/"; } return AssetDatabase.LoadAssetAtPath(realPath + assetName); } #endif AssetBundle ab = await LoadBundleAsync(bundleName); if (ab != null) { var assetRequest = ab.LoadAssetAsync(assetName); while (!assetRequest.isDone) { await Task.Yield(); } return assetRequest.asset as T; } return null; } public void UnloadBundle(string bundleName) { if (loadedBundles.ContainsKey(bundleName)) { AssetBundle bundle = loadedBundles[bundleName]; bundle.Unload(false); loadedBundles.Remove(bundleName); } } } }