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<GameObject>("SpeedView");                   //加載ab1包中的資源名爲 Sphere-Head 文件的數據,返回Object對象 (這是一個預設物)
        GameObject prefabInstance =Instantiate((GameObject)speedview,transform);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章