unityAB包加载四种方式笔记

这篇博客介绍了如何在Unity中创建和管理资源包。作者提供了两个脚本,一个用于打包AssetBundle,另一个用于从内存、文件和Web加载AssetBundle。通过这些示例,读者可以学习到AssetBundle的创建、预设物打标签和加载到场景中的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一做一个扩展脚本放在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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值