Unity AssetBundle初体验

加载资源脚本

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

public class JIAZAI : MonoBehaviour
{
    void Start()
    {
       //从本地加载包里的object
        AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/ca.assets");
        Object[] objs = ab.LoadAllAssets();

        //加载材质
        AssetBundle m = AssetBundle.LoadFromFile("AssetBundles/cy.assets");
        var material = m.LoadAllAssets();

        foreach (Object item in objs)
        {
            Instantiate(item);
        }

    }
}

打包资源脚本(挂在相机或任何物体上)

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

public class NewBehaviourScript : Editor {

    public class CreateAssetBundles
    {
        [MenuItem("Assets/Build AssetBundles")]
        static void BuildAllAssetBundles()
        {
            //定义文件夹名字
            string dir = "AssetBundles";
            //文件夹不存在,则创建
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);

            }

            BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        }
    }



}