unityAB包加载四种方式笔记

第一做一个扩展脚本放在Editor文件夹下
using UnityEditor;
using System.IO;

public class MyToos : Editor
{
[MenuItem(“Toos/CreatBundle”)]
static void CreatAssetBudle()
{
string path = “AB”;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.iOS);

}

}
制作预设物 打标签和包
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBulde : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// StartCoroutine(“FromMemory”);
// FromFile();
// AllFromFile_();
// StartCoroutine(“FromWWW”);
StartCoroutine(“FromWebRequest”);
}
//从内存加载
//IEnumerator FromMemory()
//{
// string path = @"/Users/cuihao/Downloads/worldMap/AB/fangkuai.u3d";
// string matpath = @"/Users/cuihao/Downloads/worldMap/AB/material.u3d";
// string j_path = @"/Users/cuihao/Downloads/worldMap/AB/jiaonang.u3d";
// byte[] bytrs = File.ReadAllBytes(path);
// byte[] M_byte = File.ReadAllBytes(matpath);
// byte[] j_byte = File.ReadAllBytes(j_path);
// AssetBundle assetBundle = AssetBundle.LoadFromMemory(bytrs);
// AssetBundle assetBundle_m = AssetBundle.LoadFromMemory(M_byte);
// AssetBundle assetBundle_j = AssetBundle.LoadFromMemory(j_byte);
// GameObject fkGame = assetBundle.LoadAsset(“Cube”) as GameObject;//这里Cubo是预设物的名字
// GameObject jnGame = assetBundle_j.LoadAsset(“Capsule”) as GameObject;
// GameObject go = Instantiate(fkGame);
// GameObject go_j = Instantiate(jnGame);
// yield return null;
//}

//从文件加载(单个加载)
public void FromFile()
{
    
    string path = @"/Users/cuihao/Downloads/worldMap/AB/jiaonang.u3d";
    string matpath = @"/Users/cuihao/Downloads/worldMap/AB/material.u3d";
    
    AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
    AssetBundle.LoadFromFile(matpath);
    GameObject fkGame = assetBundle.LoadAsset("Capsule") as GameObject;//这里Cubo是预设物的名字
    Instantiate(fkGame);print("111");
   
}

//从文件加载(多个加载)
public void AllFromFile_()
{
string path = @"/Users/cuihao/Downloads/worldMap/AB/wuti.new";
string matpath = @"/Users/cuihao/Downloads/worldMap/AB/material.u3d";
AssetBundle.LoadFromFile(matpath);
AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
object[] objects = assetBundle.LoadAllAssets();
foreach (var item in objects)
{
Instantiate((GameObject)item);
}
}

//www加载 无缓存机制 有问题 不执行协程

//IEnumerable FromWWW()
//{
    
//    string path = @"file:///Users/cuihao/Downloads/worldMap/AB/wuti.new";
//    WWW www = WWW.LoadFromCacheOrDownload(path, 1);//有缓存机制 下载缓存 后边数字版本号
// //   WWW www = new WWW(path);
//    yield return www;
//    if (!string.IsNullOrEmpty(www.error))
//    {
//        Debug.Log(www.error);
//        yield break;
//    }
//    AssetBundle assetBundle = www.assetBundle;
//    object[] objects = assetBundle.LoadAllAssets();
//    foreach (var item in objects)
//    {
//        Instantiate((GameObject)item);
//    }
//    www.Dispose();//卸载
//    www = null;
//}2

//从服务器加载
IEnumerable FromWebRequest()
{
    string path = @"file:///Users/cuihao/Downloads/worldMap/AB/wuti.new";
    UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path);
    yield return webRequest.SendWebRequest();
    AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(webRequest);
    object[] objects = assetBundle.LoadAllAssets();
    foreach (var item in objects)
    {
        Instantiate((GameObject)item);
    }
    yield return null;

}

}

上一篇:热更新基础--AssetBundle学习笔记


下一篇:Unity 游戏角色模型的边缘发红