AssetBundle的最简单的资源打包

拿一个简单的Cube 来打包

先建立好层级文件夹Editor Test 文件下放资源建立层级在这里插入图片描述
文件夹名字自定义自己熟悉最好 我自己那个简单明了 接下来上代码

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

public class CreateAssetBundles : Editor
{
static List files = new List();
[MenuItem(“AssetBundle/CreateAssetBundle”)]//编辑器扩展菜单
static void AssetBundles()
{
Recurtion(“Assets/Test”);
//资源导入器
for (int i = 0; i < files.Count; i++)
{
AssetImporter assetImporter = AssetImporter.GetAtPath(files[i]);
string path = files[i].Replace(“Assets/”,string.Empty);
path = path.Substring(0,path.LastIndexOf("."));
assetImporter.assetBundleName = path;
//ab 是资源的包名
assetImporter.assetBundleVariant = “ab”;
}
//创建的文件夹
Directory.CreateDirectory(“AB/ABTest”);
//发布的平台
BuildPipeline.BuildAssetBundles(“AB/ABTest”, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
///
/// 递归遍历文件夹下的文件夹和文件
///
///
private static void Recurtion(string path)
{
string[]fileNames = Directory.GetFiles(path);
string[] dirs = Directory.GetDirectories(path);
foreach (var fileName in fileNames)
{
string extension = Path.GetExtension(fileName);
//.meta文件给去除
if (extension.EndsWith(".meta"))
{
continue;
}
//菜单层级
files.Add(fileName.Replace("\","/"));
}
foreach (var dir in dirs)
{
Recurtion(dir);
}
}
}

对资源打包命名 为 ab在这里插入图片描述

把继承editor的脚本放在这个Editor文件夹下面 注意命名空间的引入 效果出现一个AssetBundle菜单栏
点击assetbundle 进行打包

最后效果为在这里插入图片描述在这里插入图片描述
在这里插入图片描述
生成.ab文件与ABTest文件 需要右键点击刷新一下才能显示出来 在这里插入图片描述
大功告成!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity AssetBundle中的引用技术可以帮助我们在加载AssetBundle时,自动加载该AssetBundle所依赖的其他AssetBundle。使用引用技术可以减少AssetBundle的重复加载,提高加载效率和减少内存占用。 在创建AssetBundle时,需要设置AssetBundle的名字和标识符。标识符可以是一个字符串,用于唯一标识该AssetBundle。在打包AssetBundle时,需要把该AssetBundle所依赖的其他AssetBundle打包进去。 在加载AssetBundle时,需要先加载该AssetBundle所依赖的其他AssetBundle,然后再加载该AssetBundle本身。可以使用Unity的AssetBundleManifest来管理AssetBundle之间的依赖关系。 以下是一个简单的示例: ```csharp IEnumerator LoadAssetBundle(string assetBundleName) { string manifestPath = Application.streamingAssetsPath + "/AssetBundles"; AssetBundle manifestAssetBundle = AssetBundle.LoadFromFile(manifestPath); AssetBundleManifest manifest = manifestAssetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); string[] dependencies = manifest.GetAllDependencies(assetBundleName); foreach (string dependency in dependencies) { AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AssetBundles/" + dependency); } AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/AssetBundles/" + assetBundleName); GameObject prefab = assetBundle.LoadAsset<GameObject>("myprefab"); Instantiate(prefab); assetBundle.Unload(false); manifestAssetBundle.Unload(false); yield return null; } ``` 在上述示例中,我们先加载AssetBundleManifest,获取该AssetBundle所依赖的其他AssetBundle,然后逐个加载这些依赖的AssetBundle。最后再加载该AssetBundle本身,并使用其中的资源。 需要注意的是,在打包AssetBundle时,需要把该AssetBundle所依赖的其他AssetBundle打包进去。在使用引用技术时,也需要注意AssetBundle之间的依赖关系,以避免加载错误和资源泄漏等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值