Unity打包ab包 和 windows下调用ab包

打包代码,要放在  \Assets\Editor 的目录下,就会在菜单 Assets 增加了一个打包的 功能 Build AssetBundles

using System.IO;
using UnityEditor;
using UnityEngine;
 
public class BuildAB : MonoBehaviour {
 
    //[MenuItem("AssetBundle/Package (Default)")]
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string streamPath = Application.streamingAssetsPath;
        if (!Directory.Exists(streamPath))
        {
            Directory.CreateDirectory(streamPath);
        }
        BuildPipeline.BuildAssetBundles(streamPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
}

在运行环境中读取包,包里面的预制体,预制体里面的运行代码只记录了绑定关系,不能动态更新代码

    void Start()
    {
        AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath+"/speedview.unity3d");
        if(ab!=null)    Debug.Log("load ok");
        else            Debug.Log("load err");
        //2:直接读出资源,生成预制体
        GameObject speedview = ab.LoadAsset("SpeedView");                   //加载ab1包中的资源名为 Sphere-Head 文件的数据,返回Object对象 (这是一个预设物)
        GameObject prefabInstance =Instantiate((GameObject)speedview,transform);
    }

 

你可能感兴趣的:(unity)