首页 > 其他分享 >Unity___AB包

Unity___AB包

时间:2023-07-01 13:55:55浏览次数:27  
标签:AB string Unity AssetBundle path ___ ab 加载

AB包

一些压缩文件,模型,预制体,图片....

Resources和AB包区别

  1. Resources打包定死,只读,无法修改
  2. 存储位置自定义,压缩方式,后期更新
  3. 减少包体大小
  4. 热更新
    1.资源
    2.脚本

热更新过程

  1. 获取资源地址
  2. 通过资源对比文件,检测哪些需要热更新,下载

AB包生成的文件

  1. AB包文件 资源文件
  2. manifest文件 AB包文件信息,当加载时,提供关键信息,资源信息,依赖关系,版本信息
  3. 关键AB包和目录名一样

AB Browser 插件相关参数

BuildTarget:目标平台
Output Path:目标输出路径
Clear Folders:是否清空文件夹重新打包
Copy To StreamingAssets:是否拷贝到StreamingAssets

NoCompression

不压缩,解压快,包较大不推荐

LZMA

压缩最小,解压慢
缺点:用一个资源要解压所有

LZ4

压缩,相对LZMA大一点点建议使用,用什么解压什么,内存占用低

Build 参数

ET在资源包中不包含资源的类型信息
FR重新打包时需要重新构建包,和ClearFolders不同,它不会删除不再存在的包
ITTC 增量构建检查时,忽略类型数的更改
Append Hash 将文件哈希值附加到资源包名上
SM 严格模式,如果打包时报错了,则打包直接失败无法成功
DRB 运行时构建

AB包加载资源

//第一步加载AB包
AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" +“mode1");
//第二步加载AB包中的资源
//只是用名字加载会出现同名不同类型资源分不清
//建议大家用泛型加载或者是Type指定类型
Gameobject obj = ab.LoadAsset<Gameobject>( "cube" );
//Gameobject obj = ab.LoadAsset("cube", typeof(Gameobject)) as Game0bject;
Instantiate(obj);

//AB包不能够重复加载否则报错
//AssetBundle ab2 = AssetBundle.LoadFromFile(Application.streamingAssetsPath + " /"+“model");//加载─个圆
GameObject obj2 = ab2.LoadAsset("Sphere" , typeof(GameObject)) as GameObject;
Instantiate(obj);

卸载加载的AB包

//卸载所有加载的AB包,参数为true,加载的资源也会被卸载
AssetBundle.UnloadAllAssetBundles(true);
//也可以自己卸载自己,参数同上
ab.Unload(false);

异步加载:协程

IEnumerator LoadABRes( string ABName,string resName ){
//第一步加载AB包
AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath+"/"+ABName);
yield return abcr;
//第二步加载资源
AssetBundleRequest abq = abcr.assetBundle.LoadAssetAsync(resName,typeof(Sprite));
yield return abq;
//abq.asset as Sprite;
img. sprite = abq.asset as Sprite;
}

依赖问题

一个资源用到别的AB包中的资源,只有把所有需要的依赖包全部加载才能正常使用

通过主包 获取依赖信息

//依赖包的关键知识点利用主包获取依赖信息
//加载主包
AssetBundle abNain = AssetBundle.LoadFromFile(Application.streamingAssetsPath + " /" +"pC");
//加载主包中的固定文件
AssetBundleManifest abManifest = abMain.LoadAsset<AssetBundlewanifest>("AssetBundlewanifest");
//从固定文件中得到依赖信息
string[] strs = abManifest.GetAllDependencies( "model");
//得到了依赖包的名字,并加载需要的依赖
for (int i = 0; i < strs.Length; i++){
AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + strs[i]);
}

打包步骤

  • 设置打包对象
  • 生成AssetBundle
  • 上传服务器
  • 下载AssetBundle
  • 加载对象
  • 使用对象

设置打包对象

设置包名和后缀名

设置打包对象

在Editor目录下创建脚本,方便快速打包

[CustomEditor(typeof(Tools))]
public class Tools : Editor {
    public override void OnInspectorGUI() {
        base.OnInspectorGUI();
        
    }
    //在上方导航栏显示出菜单
    [MenuItem("Tools/CreateAssetBundle")]
    static void CreateAssetBundle(){
        string path ="AB";
        //不存在就就建立
        if(Directory.Exists(path)==false){
            Directory.CreateDirectory(path);
        }
        // 路径 ,打包方式,平台
        BuildPipeline.BuildAssetBundles(path,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
        UnityEngine.Debug.Log("Finished");
    }
}

BuildAssetBundleOptions常用参数介绍

UncompressedAssetBundle:不压缩,包体积大
ChunkBasedCompression:创建AssetBundle时使用LZ4压缩。默认情况是Lzma格式,下载AssetBoundle后立即解压。

材质打包

如果不打包预制体的材质,则打包后每个预制体的包都会变大

AB包使用方法

加载AB

  1. 内存
  2. 文件
  3. WWW
  4. UnityWebRequest
using UnityEngine;
using System.IO;
using System.Collections;
using UnityEngine.Networking;

public class LoadAssetBundle : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(fromUnityWebRequest());
    }
    // 1. 内存
    IEnumerator fromMemery()
    {
        string path = @"E:\Study\UnityLearns\Demo-AssetBundle\AB\box.3dobj";
        string matpath = @"E:\Study\UnityLearns\Demo-AssetBundle\AB\mat.3dobj";
        byte[] Matbytes = File.ReadAllBytes(matpath);
        byte[] bytes = File.ReadAllBytes(path);
        AssetBundle assetBundle = AssetBundle.LoadFromMemory(bytes);
        AssetBundle matAssetBundle = AssetBundle.LoadFromMemory(Matbytes);
        GameObject obj = assetBundle.LoadAsset("Cube") as GameObject;
        GameObject go = GameObject.Instantiate(obj);
        yield return null;
    }
    // 2. 文件
    IEnumerator fromFileOne()
    {
        string matpath = @"E:\Study\UnityLearns\Demo-AssetBundle\AB\mat.3dobj";
        AssetBundle matAssetBundle = AssetBundle.LoadFromFile(matpath);
        string path = @"E:\Study\UnityLearns\Demo-AssetBundle\AB\box.3dobj";
        AssetBundle ab = AssetBundle.LoadFromFile(path);
        GameObject obj = ab.LoadAsset<GameObject>("cube");
        Instantiate(obj);
        yield return null;
    }
    IEnumerator fromFileAll()
    {
        //1.设置所有预制体包名为一样的

        string path = @"E:\Study\UnityLearns\Demo-AssetBundle\AB\allobj.u3d";
        AssetBundle ab = AssetBundle.LoadFromFile(path);
        object[] objs = ab.LoadAllAssets();
        foreach (var item in objs)
        {
            Instantiate((GameObject)item);
        }
        // Instantiate(obj);
        yield return null;
    }
    // 3. WWW
    //1. 无缓存机制
    IEnumerator fromWWW1()
    {
        //1.本地 file:///

        string path = @"file:///E:\Study\UnityLearns\Demo-AssetBundle\AB\allobj.u3d";
        WWW www = new WWW(path);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }

        AssetBundle ab = www.assetBundle;
        object[] objs = ab.LoadAllAssets();
        foreach (var item in objs)
        {
            Instantiate((GameObject)item);
        }
        // Instantiate(obj);
        yield return null;
    }
    //2. 有缓存机制
    IEnumerator fromWWW2()
    {
        //1.本地 file:///

        string path = @"file:///E:\Study\UnityLearns\Demo-AssetBundle\AB\allobj.u3d";
        WWW www = WWW.LoadFromCacheOrDownload(path, 1);//删除这个AB文件夹依然可以从缓存中读取到,如果将版本号改为2,则本地无法读取,但是可以读取AB2文件夹
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }

        AssetBundle ab = www.assetBundle;
        object[] objs = ab.LoadAllAssets();
        foreach (var item in objs)
        {
            Instantiate((GameObject)item);
        }
        // Instantiate(obj);
        yield return null;
        www.Dispose();
        www = null;
    }
    // 4. UnityWebRequest
    IEnumerator fromUnityWebRequest()
    {

        // string path = @"file:///E:\Study\UnityLearns\Demo-AssetBundle\AB2\box.3dobj";
        string path= @"http://localhost:8080/AB/allobj.u3d";
        UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(path);
        yield return unityWebRequest.SendWebRequest();
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(unityWebRequest);
        // GameObject obj = ab.LoadAsset<GameObject>("cube");
        // Instantiate(obj);
        object[] objs = ab.LoadAllAssets();
        foreach (var item in objs)
        {

            Instantiate((GameObject)item);
            Debug.Log(((GameObject)item).name);
        }
        yield return null;
    }
  //加载依赖
    IEnumerator fromManifest(){
        //略
        yield return null;
    }

}

上传服务器

我利用的是Tomcat服务器,将AB包放置在webapps文件夹下,通过url:http://localhost:8080/AB/allobj.u3d可以读取到,另外需要在web.xml里面添加配置

    <mime-mapping>
        <extension>u3d</extension>
        <mime-type>application/octet-stream</mime-type>
    </mime-mapping>

标签:AB,string,Unity,AssetBundle,path,___,ab,加载
From: https://www.cnblogs.com/lxp-blog/p/17469261.html

相关文章

  • 柯里化和闭包 7.1
     ......
  • qrcode模块生成二维码
    安装qrcode模块pipinstallqrcode简单使用importqrcodedata='helloworld'img=qrcode.make(data)#显示二维码图片img.show()#保存二维码图片img.save('qrcode.png')获取二维码#创建QRCode对象qr=qrcode.QRCode(version=1,error_correction=qrcode.con......
  • 八期day03-反编译工具和hook框架
    一反编译工具1.1常见反编译工具常见的反编译工具:jadx(推荐)、jeb、GDA反编译工具依赖于java环境,所以我们按照jdk1.2JDK环境安装#官方地址:(需要注册-最新java21)https://www.oracle.com/java/technologies/downloads/#下载地址链接:https://pan.baidu.com/s/1JxmjfGhW......
  • Linux常用操作
    Linux常用文件操作Linux常用文件操作目录简介cdusr切换到该目录下usr目录cd../切换到上一层目录cd/切换到系统根目录cd~切换到用户主目录cd-切换到上一个所在目录su或suroot进入root上下文su<用户名>......
  • CodeForces 高分段 dp 选做
    选取方式:CF*3000+按通过人数排序。CF1188DMakeEqual记\(cnt(x)\)表示\(x\)二进制下\(1\)的个数,题目等价于求\(x\)使得\[\sum_{x=1}^ncnt(x+a_n-a_i)\]最小。令\(a_i=a_n-a_i\)。从低位到高位考虑,假设当前要决策第\(k\)位,我们需要知道:\(1.\)\(x\)......
  • qt this application failed to start because it could notfoind orloadthe Qt pla
    qt程序报错:thisapplicationfailedtostartbecauseitcouldnotfoind orloadtheQt platform ===================================C:\Users\lenovo>C:\Users\lenovo>C:\Users\lenovo>cdD:\software\Qt\install\Qt5_12_12\5.12.12\msvc2015_64\b......
  • linux内存管理 rsyslog进程占用高内存
    rsyslog进程占用内存巨高发生险情后,立即进行排查,发现有1个节点还没有完全僵死,还能连上,只是非常卡,现象是1、内存被完全耗尽,系统swap被占用超过80%,操作非常卡顿2、负载贼高,16核的机器负载达到120+3、除了业务的进程占用内存高之外,还有一个进程占用内存也很高,rsyslogd4、osd进......
  • MySQL联合索引生效验证
    建表、添加数据,用于测试CREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENT,`gid`varchar(20)NOTNULL,`cid`int(11)DEFAULTNULL,`uid`int(11)DEFAULTNULL,`name`varchar(255)CHARACTERSETutf8COLLATEutf8_general_ciDEFAULTNULL,PRIMARYKEY......
  • 5.Shading(着色与渲染管线)
    着色(shading)定义作用于一个物体的材质。着色不考虑其他物体的存在,所以着色不考虑阴影。布林冯反射模型Blinn-PhongReflectanceModel最基础的反射模型Specularhighlights(镜面反射)Diffusereflection(漫反射)Ambientlighting(环境照明)定义观测基础信息开展研......
  • 4.Rasterization光栅化(反走样,深度缓存)
    走样Aliasing(锯齿)采样的广泛应用采样不仅可以在图片的某个位置,也可以在时间轴上动画就是一组图在时间的采样Artifacts(瑕疵、错误)采样会产生一些Artifacts(瑕疵、错误)例如:锯齿(图像上的采样)摩尔纹(删除图像奇数行,再放大成原大小后可得)”车轮效应“(车轮旋转速度过快,......