unity 打包ab包的方法,加载的方法

 1、打包ab包的方法,路径为工程目录下StreamingAssets文件夹

using UnityEditor;
using System.IO;
using UnityEngine;

public class AssetBundleBuild {
    [MenuItem("AssetsBundle/Build AssetBundles")]
    static void BuildAllAssetBundles() {
        string dir = Application.streamingAssetsPath+"/AssetBundle";
        if (Directory.Exists(dir) == false)
        {
            Directory.CreateDirectory(dir);//在工程下创建AssetBundles目录
        }
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
    }
}

 2、加载的方法

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

public class jietu : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject cba;

    private Transform canvasTrans;
    void Start()
    {
        //canvasTrans = GameObject.Find("2D画布").transform;

        string nn = "";

        AssetBundle ab = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/AssetBundle/sculpture.u3d.unity3d", 0).assetBundle;

        for (int i = 0; i < ab.GetAllAssetNames().Length; i++)
        {
            nn = ab.GetAllAssetNames()[i];
            cba = Instantiate(ab.LoadAsset<GameObject>(nn));
            //ClickBuild();
            
        }
    }

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页