5.2_AssetBundles(加载)

本文介绍了一种在Unity中使用C#脚本RunAssetBundles加载AssetBundle资源的方法。详细解释了如何根据不同平台(如Android、iOS和Windows)设置正确的资源路径,以及如何通过按钮触发加载指定的Prefab或场景。同时,提供了加载单个和多个对象的示例代码,展示了如何利用WWW和AssetBundle类进行资源下载、实例化和内存管理。

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

读取资源文件:

创建C#脚本名称为RunAssetBundles。

using UnityEngine;

using System.Collections;

 

public class RunAssetBundles : MonoBehaviour {

    //AssetBundle资源路径,不同平台下StreamingAssets的路径是不同的,这里需要注意一下。

    public static readonly string PathURL =

#if UNITY_ANDROID//安卓

     "jar:file://" + Application.dataPath + "!/assets/";

#elif UNITY_IPHONE//苹果手机

      Application.dataPath + "/Raw/";

#elif UNITY_STANDALONE_WIN || UNITY_EDITOR//windows或unity编辑器

 "file://" + Application.dataPath + "/StreamingAssets/";

#else

    String.Empty;

#endif

    void OnGUI()

    {

        if (GUILayout.Button("Prefab0"))

        {

            StartCoroutine(LoadMainObj(PathURL + "Prefab0.assetbundle"));

        }

        if (GUILayout.Button("Prefab1"))

        {

            StartCoroutine(LoadMainObj(PathURL + "Prefab1.assetbundle"));

        }

        if (GUILayout.Button("All AssetBundle"))

        {

            StartCoroutine(LoadAllObj(PathURL + "ALL.assetbundle"));

        }

        if (GUILayout.Button("Open Scene"))

        {

            StartCoroutine(LoadAllObj(PathURL + "MyScene.unity3d"));

        }

    }

 

    private IEnumerator LoadMainObj(string path)

    {

        //下载类WWW("路径"),bundle只保存在内存中

        WWW bundle = new WWW(path);

        yield return bundle;

        //实例化获取的assetbundle对象

        yield return Instantiate(bundle.assetBundle.mainAsset);

       // 卸载所有包含在bundle中的内存镜像,若参数为true,在使用中的对象也会被清楚(慎用)

        bundle.assetBundle.Unload(false);    

    }

    private IEnumerator LoadAllObj(string path) {

        WWW bundle = new WWW(path);

        yield return bundle;

        Object obj0=bundle.assetBundle.LoadAsset("Prefab0");

        Object obj1=bundle.assetBundle.LoadAsset("Prefab1");

        yield return Instantiate(obj0);

        yield return Instantiate(obj1);

//施放内存--参数:false释放内存、true对象也会被删除

        bundle.assetBundle.Unload(false);

    }

 

private IEnumerator LoadScene(string path)

{

 var download=  WWW. LoadFromCacheOrDownload(PathURL + "AssetbundleScene.unity3d",1);

  yield return download;

     Application.LoadLevel ("MyScene");

}

}

 

/*补充:

也可以用AssetBundle.CreatFromFile下载。

Prefab打包资源包选项为

BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.UncompressedAssetBundle

Scene打包资源包选项为

BuildOptions.UncompressedAssetBundle

路径地址中没有file://

(Android平台中要注意:www方法中路径apk!/assets,CreatFromFile方法中路径apk!asse,叹号后面没有/)

string path=Application.dataPath + "/StreamingAssets/"+"资源名称";

 AssetBundle download = AssetBundle.CreateFromFile(path);

yield return download;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值