框架使用AB包的方式来加载资源,这样意味着如果资源出现了变更,那么就要重新打包,这样才能加载出最新的资源。为了加快开发效率,可以使用AssetDatabase的形式来加载资源
0.
如下,用两种方式去加载cube
1 using UnityEngine; 2 using UnityEditor; 3 4 public class TestAB : MonoBehaviour 5 { 6 public bool isAssetBundle = true; 7 8 void Start() 9 { 10 if (isAssetBundle) 11 { 12 AssetBundle cubeAB = AssetBundle.LoadFromFile("Assets/StreamingAssets/cube"); 13 GameObject go = null; 14 if (cubeAB != null) 15 { 16 go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 17 go.transform.localScale = Vector3.one * 2; 18 } 19 } 20 else 21 { 22 #if UNITY_EDITOR 23 GameObject cubeAsset = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefab/Cube.prefab"); 24 GameObject go = Instantiate(cubeAsset); 25 go.transform.localScale = Vector3.one * 10; 26 #endif 27 } 28 } 29 }
1.
统一加载路径
使用AB包时,路径为:Assets/StreamingAssets/cube,而且还需要提供资源名:cube
使用AssetDatabase时,路径为:Assets/Prefab/Cube.prefab,注意需要提供后缀
首先,打包时一般以文件夹为单位,因此这里需要对Prefab文件夹进行打包,Cube.prefab即为Prefab包内的资源。
然后,对于AssetDatabase,其路径是没有大小写敏感的,需要提供后缀。
1 using UnityEngine; 2 using UnityEditor; 3 using System; 4 using UObject = UnityEngine.Object; 5 6 public class TestAB : MonoBehaviour 7 { 8 public bool isAssetBundle = true; 9 10 void Start() 11 { 12 Load("Prefab/Cube.prefab", (obj) => { 13 GameObject go = Instantiate(obj) as GameObject; 14 go.transform.localScale = Vector3.one * 5; 15 }); 16 } 17 18 //path:Assets下的路径 19 private void Load(string path, Action<UObject> cb) 20 { 21 if (isAssetBundle) 22 { 23 path = "Assets/StreamingAssets/" + path; 24 path = path.Substring(0, path.LastIndexOf(".")); 25 string assetBundleName = path.Substring(0, path.LastIndexOf("/")); 26 string assetName = path.Substring(path.LastIndexOf("/") + 1); 27 Debug.LogWarning(assetBundleName); 28 Debug.LogWarning(assetName); 29 30 AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundleName); 31 if (assetBundle != null) 32 { 33 UObject asset = assetBundle.LoadAsset<UObject>(assetName); 34 if (cb != null && asset != null) 35 { 36 cb(asset); 37 } 38 } 39 } 40 else 41 { 42 #if UNITY_EDITOR 43 path = "Assets/" + path; 44 UObject asset = AssetDatabase.LoadAssetAtPath<UObject>(path); 45 if (cb != null && asset != null) 46 { 47 cb(asset); 48 } 49 #endif 50 } 51 } 52 }
2.
修改框架源码
ResourceManager.cs
Packager.cs
3.
调用
分类:
Unity热更新
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?