AssetBundle加载

本文介绍了如何在Unity中创建和打包AssetBundle,包括创建一个蓝色Cube prefab并设置包名。强调AB包名应全小写,且不同平台、不同Unity版本的AB文件可能不通用,建议使用相同版本进行打包和发布。示例代码展示了BuildBundle和Inst两个类的用途,通过运行代码实现资源包的加载和对象实例化。

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

首先将资源打包
建一个工程 创建一个蓝色的cube 做成prefab 设置包名 结束
这里写图片描述
ab包名必须全小写
// 资源包 打包类 不同平台使用各自生成的.ab资源包 不同平台之间不通用
// 不同版本的unity 生成的AB文件或许是不通用的 建议使用相同版本打包并且发布
public class BuildBundle :MonoBehaviour {

[MenuItem("BuildAssetBundle/BuildForWindows")]
static void BuildForWindows() {
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}

[MenuItem("BuildAssetBundle/BuildForIOS")]
static void BuildForIOS()
{
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.None, BuildTarget.iOS);
}

[MenuItem("BuildAssetBundle/BuildForAndroid")]
static void BuildForAndroid()
{
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.None, BuildTarget.Android);
}

}

生成菜单之后点击BuildForWindows

然后在将streamingAssetsPath路径下的文件拷贝到persistentDataPath
代码:

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

private void Start()
{
    FileInfo[] files;// 文件数组
    List<string> paths = new List<string>();// 路径集合(文件名)
    if (Directory.Exists(Application.streamingAssetsPath))
    {
        DirectoryInfo d = new DirectoryInfo(Application.st
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值