游戏资源打包(1)

将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新。服务器上包含以下资源列表:
(1)游戏内容资源assetbundle
(2)资源维护列表,包含每个资源的名字(完整路径名)和对应的版本号[资源名,版本号],如下表所示(VersionNum.xml):

复制代码
<VersionNum>
  <File FileName="Assets.Resources.BigLevelTexture.TestLevel.assetbundle" Num="1" />
  <File FileName="Assets.Resources.EquipmentTexture.Test001.assetbundle" Num="1" />
  <File FileName="Assets.Resources.EquipmentTexture.Test002.assetbundle" Num="1" />
  <File FileName="Assets.Resources.EquipmentTexture.Test003.assetbundle" Num="1" />
  <File FileName="Assets.Resources.EquipmentTexture.Test004.assetbundle" Num="1" />
  <File FileName="Assets.Resources.PetTexture.Empty.assetbundle" Num="1" />
</VersionNum>
复制代码

 

那么本地客户端的资源打包编辑器就需要完成以下工作:将资源打包、生成版本号。
我们采用通过MD5码对比的方式来对版本号进行管理,如果某资源的MD5码变更了,则将其版本号+1,否则不变。那么,可以将编辑器的具体任务划分如下:
(1)将资源打包成assetbundle,并放到指定目录下
(2)为每个assetbund生成最新MD5码,用于检查资源是否有修改
(3)比较新旧MD5码列表,产生资源变更列表,对于每个变更的资源,将其版本号+1
(4)将变更列表文件也打包成assetbundle

各个平台使用的资源包时各自独立的,打包资源代码时的选项不一样,编辑器界面如下,图2中的4个按钮每个对应上述的一步操作:

 

 

最终生成的资源目录结构如下所示:

 编辑器代码如下所示(包括菜单项和窗口):

复制代码
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;

public class AssetBundleController : EditorWindow
{
    public static AssetBundleController window;
    public static UnityEditor.BuildTarget buildTarget = BuildTarget.StandaloneWindows;

    [MenuItem("XiYouEditor/AssetBundle/AssetBundle For Windows32", false, 1)]
    public static void ExecuteWindows32()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.StandaloneWindows;
        window.Show();
    }

    [MenuItem("XiYouEditor/AssetBundle/AssetBundle For IPhone", false, 2)]
    public static void ExecuteIPhone()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.iPhone;
        window.Show();
    }

    [MenuItem("XiYouEditor/AssetBundle/AssetBundle For Mac", false, 3)]
    public static void ExecuteMac()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.StandaloneOSXUniversal;
        window.Show();
    }

    [MenuItem("XiYouEditor/AssetBundle/AssetBundle For Android", false, 4)]
    public static void ExecuteAndroid()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.Android;
        window.Show();
    }

    [MenuItem("XiYouEditor/AssetBundle/AssetBundle For WebPlayer", false, 5)]
    public static void ExecuteWebPlayer()
    {
        if (window == null)
        {
            window = (AssetBundleController)GetWindow(typeof(AssetBundleController));
        }
        buildTarget = UnityEditor.BuildTarget.WebPlayer;
        window.Show();
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10f, 10f, 200f, 50f), "(1)CreateAssetBundle"))
        {
            CreateAssetBundle.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (1) Completed", "OK");
        }

        if (GUI.Button(new Rect(10f, 80f, 200f, 50f), "(2)Generate MD5"))
        {
            CreateMD5List.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (2) Completed", "OK");
        }

        if (GUI.Button(new Rect(10f, 150f, 200f, 50f), "(3)Compare MD5"))
        {
            CampareMD5ToGenerateVersionNum.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (3) Completed", "OK");
        }

        if (GUI.Button(new Rect(10f, 220f, 200f, 50f), "(4)Build VersionNum.xml"))
        {
            CreateAssetBundleForXmlVersion.Execute(buildTarget);
            EditorUtility.DisplayDialog("", "Step (4) Completed", "OK");
        }
    }

    public static string GetPlatformPath(UnityEditor.BuildTarget target)
    {
        string SavePath = "";
        switch (target)
        {
            case BuildTarget.StandaloneWindows:
                SavePath = "Assets/AssetBundle/Windows32/";
                break;
            case BuildTarget.StandaloneWindows64:
                SavePath = "Assets/AssetBundle/Windows64/";
                break;
            case BuildTarget.iPhone:
                SavePath = "Assets/AssetBundle/IOS/";
                break;
            case BuildTarget.StandaloneOSXUniversal:
                SavePath = "Assets/AssetBundle/Mac/";
                break;
            case BuildTarget.Android:
                SavePath = "Assets/AssetBundle/Android/";
                break;
            case BuildTarget.WebPlayer:
                SavePath = "Assets/AssetBundle/WebPlayer/";
                break;
            default:
                SavePath = "Assets/AssetBundle/";
                break;
        }

        if (Directory.Exists(SavePath) == false)
            Directory.CreateDirectory(SavePath);

        return SavePath;
    }

    public static string GetPlatformName(UnityEditor.BuildTarget target)
    {
        string platform = "Windows32";
        switch (target)
        {
            case BuildTarget.StandaloneWindows:
                platform = "Windows32";
                break;
            case BuildTarget.StandaloneWindows64:
                platform = "Windows64";
                break;
            case BuildTarget.iPhone:
                platform = "IOS";
                break;
            case BuildTarget.StandaloneOSXUniversal:
                platform = "Mac";
                break;
            case BuildTarget.Android:
                platform = "Android";
                break;
            case BuildTarget.WebPlayer:
                platform = "WebPlayer";
                break;
            default:
                break;
        }
        return platform;
    }

}
复制代码

 PS:每个操作的具体实现,见下一篇讲解.

### 回答1: 打包HTML小游戏代码是为了方便在其他设备或环境中运行游戏。以下是打包HTML小游戏代码的步骤: 1. 打开你的HTML小游戏代码文件夹,并确保所有的游戏文件都在同一个文件夹内,包括HTML文件、CSS文件、JavaScript文件和所有游戏资源文件。 2. 使用压缩软件(如WinRAR或7-Zip)将该文件夹压缩成一个ZIP文件。 3. 将ZIP文件上传到一个文件分享网站(如Google Drive、Dropbox或OneDrive),并生成一个下载链接。 4. 发送下载链接给其他人,或者将该链接嵌入到你的网站或社交媒体平台上,以供其他人下载和玩游戏。 如此,其他人就可以从网上下载并玩你的HTML小游戏了。同时,你也可以将该ZIP文件通过USB设备或其他可移动储存设备分享给其他设备运行。 ### 回答2: 首先,需要将所有的 HTML、CSS 和 JavaScript 文件整合到一个文件夹中,以便于进行后续的打包操作。 然后,可以使用打包工具,如 webpack、gulp 等,对这些文件进行打包打包的过程中,可以使用一些插件来压缩代码、混淆代码、去除注释等操作,以提高代码的性能和安全性。 在打包的过程中,可以对不同的浏览器进行兼容性处理,以确保游戏能够在各种浏览器中正常运行。 最后,需要将打包后的代码上传到服务器或者存储在云端,以供用户进行访问和游戏。在上传的过程中,可以对文件进行压缩、加密等操作,以提高文件的安全性和下载速度。 总的来说,HTML 小游戏代码打包是一个比较复杂的过程,需要掌握一定的前端开发知识和打包工具的使用。但是,通过打包可以大大提高代码的性能和安全性,为用户带来更好的游戏体验。 ### 回答3: HTML小游戏的代码打包可以使用多种工具完成。一种常用的工具是webpack,它可以将多个文件打包成一个文件,减少加载时间和带宽使用。打包前,需要创建一个webpack的配置文件,指定需要打包的入口文件和输出文件的路径。在配置文件中指定需要使用的loader和plugin,例如babel-loader将ES6的语法转换为ES5的语法,uglifyjs-webpack-plugin可以对打包后的文件进行压缩和混淆。 使用webpack打包时,可以将所有的HTML、CSS和JavaScript文件都打包在一起,也可将它们分开打包。一般而言,将CSS和JavaScript文件分开打包可以提高加载速度。另外,为了提高用户的体验,可以使用Webpack Dev Server工具,在开发阶段中实时更新页面。 除了webpack,还有其他的工具可以实现HTML游戏的代码打包,例如gulp和grunt。这些工具使用起来有些差异,但是都提供了很好的自动化构建和打包功能,进而让开发者专注于游戏本身而不是一些琐碎的任务。为了保证游戏质量和用户体验,在代码打包之前,应该要进行充分的测试和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值