实现从本地获取AssetBundle包

1、在场景中创建物体并设置为预设,为其设置打AssetBundle包的名字和后缀;

2、工程中创建Editor文件夹(不再过多阐述),编写脚本实现将项目设置的预设体打成AssetBundle包。此脚本放在 Editor文件夹内(代码如下)
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// 打包代码
/// </summary>
public class CreateAss  {
    //创建菜单 实现点击自定义按键生成AssetBundle所在的文件夹
    [MenuItem("Assets/BuildAssetBundles")]
    static void BuildAllAss()
    {
        string dir = "AssetBundlesSOHO";
        if (Directory.Exists(dir)==false)
        {
            Directory.CreateDirectory(dir);
        }
     BuildPipeline.BuildAssetBundles("AssetBundlesSOHO",BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}
代码保存,点击代码所写的菜单位置,生成代码指定的 AssetBundle文件夹。
3、实现从本地 AssetBundle文件夹里加载资源(代码如下)

using UnityEngine;
public class loadFromFile : MonoBehaviour {
 void Start () {
        //从本地AssetBundle文件夹里加载资源
       AssetBundle ab= AssetBundle.LoadFromFile("AssetBundlesSOHO/one.unity3d");
       GameObject go= ab.LoadAsset<GameObject>("Cube");
        Instantiate(go,this.transform);
               }
}

            //从内存读取
            AssetBundleCreateRequest req = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));//需要AssetBundleCreateRequest类型接收
            //yield return req;
            AssetBundle ab = req.assetBundle;//转成AssetBundle类型
            GameObject go = ab.LoadAsset<GameObject>("cube");//根据包来实例化物体
            Instantiate(go, this.transform);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值