unityzgy/Assets/Scripts/PublicTool/AssetBundleHelper.cs
ayuan9957 bf12e02276 feat: 多语言本地化系统 - 支持中/英/法/俄实时切换
- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg)
- LoginPanel: InputField placeholder本地化、字体颜色保持
- HistoryPanel: 用时数据本地化、placeholder本地化
- RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建)
- AppraiseWindowBase: 评价等级本地化、操作消息重建
- EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized
- StudentOperateRecorder: 新增InjectOperateMsgLocalized方法
- LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选
- 字体切换时保留颜色和verticalOverflow
2026-07-16 10:05:59 +08:00

128 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using XFramework;
public static class AssetBundleHelper
{
static Dictionary<string, AssetBundle> bundles = new Dictionary<string, AssetBundle>();
static Dictionary<string, Object> prefabs = new Dictionary<string, Object>();
public static UnityWebRequest sceneWebRequest;
//public static IEnumerator LoadGOAB<T>(string url, string objName, UnityAction<T> loadedAction) where T : Object
//{
// if (!bundles.ContainsKey(url))
// {
// var goWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(url);
// yield return goWebRequest.SendWebRequest();
// if (goWebRequest.error != null)
// {
// loadedAction.Invoke(null);
// yield return null;
// }
// AssetBundle ab = DownloadHandlerAssetBundle.GetContent(goWebRequest);
// bundles.Add(url, ab);
// }
// T go;
// if (!prefabs.ContainsKey(objName))
// {
// go = bundles[url].LoadAsset<T>(objName);
// prefabs.Add(objName, go);
// }
// else
// go = prefabs[objName] as T;
// loadedAction?.Invoke(go);
//}
/// <summary>
/// await调用 加载AB包 预制件
/// </summary>
/// <param name="path">AB包路径</param>
/// <param name="prefabName">预制件名称</param>
/// <returns>预制件</returns>
public static async Task<GameObject> LoadABPrefab(string path,string prefabName)
{
#if UNITY_EDITOR
//string eidtorPath = $"Assets/AssetBundlePrefab/{prefabName}.prefab";
//if (!File.Exists(eidtorPath))
//{
// Debug.LogError($"不存在的资源:{eidtorPath},无法加载!");
// return null;
//}
//GameObject go = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(eidtorPath);
//return go;
#endif
if (prefabs.ContainsKey(path))
return prefabs[path] as GameObject;
//if (!bundles.ContainsKey(path))
//{
//}
var task = LoadAsync(path, prefabName);
Task<GameObject> t = await Task.WhenAny(task);
return t.Result;
}
static async Task<GameObject> LoadAsync(string url,string prefabName)
{
//string path = Path.Combine(Application.streamingAssetsPath, url + end);
var goWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(url);
await goWebRequest.SendWebRequest();
if (goWebRequest.error != null)
{
Debug.Log($"资源:{url}加载失败,{goWebRequest.error}");
return null;
}
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(goWebRequest);
GameObject go = ab.LoadAsset<GameObject>(prefabName);
prefabs.Add(url, go);
return go;
}
/// <summary>
/// 加载场景AB包
/// </summary>
/// <param name="url">场景AB包路径</param>
/// <param name="sceneLoaded"></param>
/// <returns></returns>
public static IEnumerator LoadSceneAB(string url, UnityAction sceneLoaded)
{
if (!bundles.ContainsKey(url))
{
//UnityWebRequest
sceneWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(url);
Debug.Log("尝试加载文件");
yield return sceneWebRequest.SendWebRequest();
// Debug.Log(sceneWebRequest.error);
Debug.Log("获取AssetBundle");
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(sceneWebRequest);
bundles.Add(url, ab);
}
sceneLoaded?.Invoke();
}
/// <summary>
/// 释放所有的资源
/// </summary>
/// <param name="unloadAllLoadedObjects"> true 卸载所有AB资源false 卸载当前不用资源</param>
public static void UnloadAllAB(bool unloadAllLoadedObjects)
{
foreach (var item in bundles)
{
item.Value.Unload(unloadAllLoadedObjects);
}
bundles.Clear();
prefabs.Clear();
AssetBundle.UnloadAllAssetBundles(unloadAllLoadedObjects);
Resources.UnloadUnusedAssets();
System.GC.Collect();
}
}