Unity3D学习心得--从本地动态加载AssetBundle文件到游戏场景

上代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

public class ReadAssetbundles : MonoBehaviour
{
    //不同平台下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
        "file://" + Application.dataPath + "/StreamingAssets/";
#else
        string.Empty;
#endif*/

    //设置需要读取的AssetBundle文件名
    public static  string fileName = "scene/model.ab";
    /*
     
     备注知识点

        ⑴Path.Combine(string, string) 连接两个字符串

        ⑵ Application 访问应用程序运行时数据

        Application.streamingAssetsPath输出

            E:/UnityProject/ARVR/Workspace/MyCharacter/Assets/StreamingAssets

        Application.dataPath输出

            E:/UnityProject/ARVR/Workspace/MyCharacter/Assets

        值得注意的是如果在PC上使用时需要在每个文件夹前加入斜杠,如:

            string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
     */

    private void Start()
    {
        //Debug.Log("Start!");
        //从文件夹里加载包
        //将本地文件放到项目下的StreamingAssets文件夹中
        //var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "model.ab"));
        //加载绝对文件
        string myPath = "D:/AB/model.ab";
        var myLoadedAssetBundle = AssetBundle.LoadFromFile(myPath);

        if (myLoadedAssetBundle == null)

        {

            Debug.Log("Failed to load AssetBundle!");

            return;

        }

        //从Bundle包中加载名字为:XXXX的资源,加载为GameObject

        var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("Obj1");

        //实例化

        Instantiate(prefab);

        //卸载包中资源的内存

        myLoadedAssetBundle.Unload(false);

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值