实现从本地获取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);

猜你喜欢

转载自blog.csdn.net/qq_33589628/article/details/80486364