U3D assetbundle加载与卸载的深入理解
using UnityEngine; using System.Collections; using System; public class testLoadFromAB : MonoBehaviour { IEnumerator DownloadAndCache() { while (!Caching.ready) yield return null; //注意,从本地加载时,必须使用前缀 file:///或file://,从网络加载则使用 http://,这两种协议可以在iphone和WINDOWS, 安卓上通用 //UNITY MANUAL: //http://, https:// and file:// protocols are supported on iPhone. //ftp:// protocol support is limited to anonymous downloads only. Other protocols are not supported. //WWW www = WWW.LoadFromCacheOrDownload ("file:///Z:/unity/learn-test/Assets/AssetBundles/cubes.unity3d", 22); Debug.Log (Application.dataPath); Debug.Log (Application.streamingAssetsPath); Debug.Log (Application.persistentDataPath); Debug.Log (Application.temporaryCachePath); AssetBundle bundle = AssetBundle.LoadFromFile ("Assets/AssetBundles/cubes.unity3d"); // yield return www; // if(!string.IsNullOrEmpty (www.error)){//有些平台不支持string为null,这种写法可以避免意外 // Debug.LogError (www.error); // yield break; // } //AssetBundle bundle = www.assetBundle; //注意必须使用Instantiate实例化出来才能将两个CUBE显示到场景中 //Instantiate实际上复制,浅复制,destroy也是浅销毁,只销毁物体本身不管物体的引用。 //prefab与gameobject一样,有些组件是引用的,比如材质贴图。 //loadasset去加载一个prefab时,会把此预设用到的所有资源都加载到内存,比如材质贴图。 Instantiate (bundle.LoadAsset ("DecalCube2")); Instantiate (bundle.LoadAsset ("DecalCube3")) ; //参数为true时,将销毁所有从loadasset加载出来的资源,比如DecalCube2.prefab的材质与贴图。 //虽然我们使用了Instantiate对DecalCube2.prefab进行了复制,但只是浅复制,当bundle.Unload (true)时 //贴图被释放,我们的prefab就会丢失贴图变为紫色。 bundle.Unload (true); } // Use this for initialization void Start () { StartCoroutine ("DownloadAndCache"); } // Update is called once per frame void Update () { } }
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· golang中写个字符串遍历谁不会?且看我如何提升 50 倍
· C# 代码如何影响 CPU 缓存速度?
· 智能桌面机器人:使用 .NET 为树莓派开发 Wifi 配网功能
· C# 模式匹配全解:原理、用法与易错点
· 记一次SSD性能瓶颈排查之路——寿命与性能之间的取舍
· 时隔半年,拾笔分享:来自一个大龄程序员的迷茫自问
· C#-Visual Studio工具使用实践
· 《程序员的底层思维》读后感
· WineHQ 发布的 Framework Mono 6.14 的这个特性对Windows Form
· 不写一行代码 .NET 使用 FluentCMS 快速构建现代化内容管理系统(CMS)