201808111219-简单读取assetsbundle

本文介绍了一个简单的Unity脚本,用于加载AssetBundle资源。通过使用WWW类进行异步加载,加载完成后实例化预设,并确保正确卸载AssetBundle以避免内存泄漏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


    public class SimpleLoadAssetBundle : MonoBehaviour
    {
        string path = null;

        // Use this for initialization
        void Start()
        {
            path = string.Format("{0}/{1}", Application.streamingAssetsPath, "simpleassetsbundle");

            StartCoroutine("GetBundle");
        }

        private IEnumerator GetBundle()
        {
            Debug.Log("enter GetBundle");

            WWW www = new WWW(path);

            yield return www;

            Debug.Log("load GetBundle");

            if (www.error == null)
            {
                Debug.Log("success GetBundle");

                AssetBundle bundle = www.assetBundle;

                GameObject go = GameObject.Instantiate<GameObject>(bundle.LoadAsset("Cube1") as GameObject, this.transform);

                go.transform.localScale = Vector3.one;

                bundle.Unload(false);

            }
            else
            {
                Debug.Log("fail GetBundle");

                Debug.Log(">>> error :"+ www.error);
            }

            www.Dispose();

            www = null;

            yield break;
        }
    }

 

脚本挂载于物体

思路是利用www类来加载ab包

然后读取到相应的包后loadassets指定的ab包中的对象

读取完之后需要注意的是

ab包需要卸载

如果不卸载的话就会占用过多的内存降低性能

以及利用完www之后记得释放置空

注意ab包释放完之后引用有可能也随之置空

所以利用ab包的话需要

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值