栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity资源加载之Assetbundle(一)

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity资源加载之Assetbundle(一)

1、将资源打包成AssetBundle

步骤一:要自己写扩展编辑器,要在Editor文件夹中进行,继承Editor类
 

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

public class CreateEditor : Editor
{

    [MenuItem("Tools/CreatAssetBundle")]
    public static void CreatAssetBundle()
    {
        string filePath = "AssetBundle"; //  文件名为“AsssetBundle”
        if (Directory.Exists(filePath) == false)  // 判断是否存在,不存在创建
        {
            Directory.CreateDirectory(filePath); // 根据文件夹路径创建文件夹
        }
        // 可以使用这个ApI                     (打包输出的路径,压缩格式(枚举),编译平台)
        BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

注意:打包路径必须再Assets文件夹外边,不然打包会崩溃(不知道别人的是不是这样,本人使用的2019.4.10版本,只要打包到Assets文件夹下就崩,有解决办法请告诉我!!!!)

对于BuildPipeline.BuildAssetBundles的参数详见:

构建 AssetBundle - Unity 手册

步骤二:选择要打包的资源,如模型

 前边框是前缀,后边框是后缀,选择new即可自行改变

步骤三:Tools->CreatAssetBundle(在工具栏,因为写了扩展编辑器出现的!)

选择位置,等待即可。完成后如下:

2、加载assetbundle资源包

方法一:本地加载

         //本地加载---只适用pc
        string path = @"F:/AssetBundle/cube.unity";

        //加载模型
        AssetBundle ab = AssetBundle.LoadFromFile(path);
        GameObject go = ab.LoadAsset("cube");
        GameObject cube = Instantiate(go);

        Debug.Log("success!");

注意:path要写之前保存的地址;加载模型时,要写模型的名字,我的模型就叫cube,根据自己的更改,完成之后,控制台输出success!

方法二:通过UnityWebRequest加载

步骤一:将打包好的asset bundle放到服务器上,最基本的IIs服务器就可以,如果有问题请百度一下。(记得打开目录浏览,不然可能会出错)

步骤二:脚本加载(通过www加载的都是耍流氓,基本上不用看了,www新版本不兼容,会提醒过时)

IEnumerator Load()
    {
        string path = @"http://172.16.20.133:8084/AssetBundle/cube.unity";
        UnityWebRequest request = UnityWebRequest.Get(path);
        yield return request.SendWebRequest();
        //AssetBundle ab = (request.downloadHandler as             DownloadHandlerAssetBundle).assetBundle;
        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

        byte[] results = request.downloadHandler.data;
        AssetBundle ab = AssetBundle.LoadFromMemory(results);
        GameObject go = ab.LoadAsset("cube");
        GameObject cube = Instantiate(go);
    }

IP改成自己的IP和端口。

下边说的一定注意!!!!

//AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

        byte[] results = request.downloadHandler.data;
        AssetBundle ab = AssetBundle.LoadFromMemory(results);

这个部分,前两个注释掉的都有问题,找不到,必须要用下边那种,基本上提到www加载的都是用的前两种,可以不用看了!!!

然后就可以运行,发现原本不在场景中的模型,可以加载进去了!

以上是PC版做法,以下是web端做法

只需要注意以下几点:

1)打包成ab包的时候选择合适的打包方式和平台

BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.WebGL);

webgl支持LZ4压缩方式!!

2)自己改的后缀必须在MIME中添加映射(不会的自行百度)

application/octet-stream

打包发布测试成功!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号