Unity 最基础的热更流程

当下游戏开发少不热更流程,作为游戏开发,我们必须熟悉几个关键的目录
Unity中几个特殊路径的说明:

[dataPath} :返回程序的数据文件所在的文件夹的路径(只读)。返回路径为相对路径,一般是 相对于程序安装目录的位置。不同游戏平台的数据文件保存路径不同。
StreamingAssetsPath: 此属性用于返回数据流的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。(只读)

[PersistentDataPath]:返回一个持久化数据存储目录的路径(可读可写),可以在此路径下存储一些持久化的数据文件。对应同一平台,在不同程序中调用此属性时,其返回值是相同的,但是在不同的运行平台下,其返回值会不一样。

[temporaryCachePath] :此属性用于返回一个临时数据的缓冲目录(可读可写)。对于同一平台,在不同程序中调用此属性时,其返回值是相同的,但是在不同的运行平台下,其返回值是不一样的。

persistentDataPath和temporaryCachePath的返回值一般是程序所在平台的固定位置,适合程序在运行过程中产生的数据文件。

IOS:
Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches

Android:
Application.dataPath : /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath : /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath : /data/data/xxx.xxx.xxx/cache

Windows:
Application.dataPath : /Assets
Application.streamingAssetsPath : /Assets/StreamingAssets
Application.persistentDataPath : C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName
Application.temporaryCachePath : C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName

Mac:
Application.dataPath : /Assets
Application.streamingAssetsPath : /Assets/StreamingAssets
Application.persistentDataPath : /Users/xxxx/Library/Caches/CompanyName/Product Name
Application.temporaryCachePath : /var/folders/57/6b4_9w8113x2fsmzx_yhrhvh0000gn/T/CompanyName/Product Name

热更流程

步骤一、在Resources目录下新建一个文本,名称是bundle_list(后缀是.txt),内容如下: {“id”:0,“version”:“1.0”,“manifest”:“android”,“resource”:{xxxxx : hashMd5}},当然您可以根据自己项目 实际情况来设计json格式。资源服务器上也会有一份格式相同的bundle_list

步骤二、如果是第一次进入游戏,Application.persistentDataPath目录下还没有bundle_list文件,这 时候就需要用Resources.Load方法从Resources目录中加载出来。否则 加载Application.persistentDataPath目录下的bundle_list

步骤三、从资源服务器下载bundle_list文件

步骤四、获取本地bundle_list的id和资源服务器下载的bundle_list中的id,做对比,如果前者等于后者,则不需要更新,如果前者小于后者,则需要更新。

步骤五、分别解析出本地和资源服务器bundle_list中的资源路径名称,名称相同的,对比hash值,相同 则不需要更新,反之,更新。如果资源服务器有的名称本地没有,则表示是新增资源,需要 下载到本地。

步骤六、把资源服务器的bundle_list覆盖本地bundle_list。热更新完成。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.IO;
//using LitJson;

/**  * 资源增量更新  

* 1.检查本地Application.persistentDataPath目录中是否有bundle_list文件  

* 2.如果没有,则从Resources目录中读取bundle_list文件

 * 3.从服务器上下载bundle_list文件,判断版本是否一致,如果一致就不用更新

 * 4.版本不一致,需要更新,更新  

* 5.将最新的bundle_list存入Application.persistentDataPath目录中

 **/

public class TestHot : MonoBehaviour
{
   
    private static readonly string VERSION_FILE = "bundle_list";

    private string SERVER_RES_URL = "";

    private string LOCAL_RES_URL = "";

    private string LOCAL_RES_PATH = "";

    /// 本地版本json对象

    private JsonData jdLocalFile;

    /// 服务端版本json对象
    private JsonData jdServerFile;

    /// 本地资源名和路径字典
    private Dictionary<string, string> LocalBundleVersion;

    /// 服务器资源名和路径字典

    private Dictionary<string, string> ServerBundleVersion;

    /// 需要下载的文件List

    private List<string> NeedDownFiles;

    /// 是否需要更新本地版本文件

    private bool NeedUpdateLocalVersionFile = false;

    /// 下载完成委托

    public delegate void HandleFinishDownload(WWW www);

    /// 本次一共需要更新的资源数

    int totalUpdateFileCount = 0;


    void Start()

    {
   

#if UNITY_EDITOR && UNITY_ANDROID

SERVER_RES_URL = "file:///" + Application.streamingAssetsPath + "/android/";

LOCAL_RES_URL = "file:///" + Application.persistentDataPath + "/res/";

LOCAL_RES_PATH = Application.persistentDataPath + "/res/";

#elif UNITY_EDITOR && UNITY_IOS

SERVER_RES_URL 
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Debug_尚尚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值