Unity热更新系列之 大版本更新应用覆盖安装问题

应用版本迭代有时候我们不得不进行大版本升级,让玩家下载最新的包进行覆盖安装掉原来的包,覆盖安装这个动作是操作系统处理的,所以我们要遵循系统规则才能以正确的姿势覆盖掉之前的应用包,这里面有些需要注意的点

  1. 安卓的覆盖安装涉及到签名以及 versionCode 和 versionName,要正常覆盖安装需要满足以下条件:a.签名一致,即.keystore文件要用同一个。b.versionCode值要比之前的包大(versionCode是给机器看的,versionName虽然是给人看的,但一般值也都比之前包设定的大)
  2. Unity覆盖安装时Application.persistentDataPath目录并不会被删除,这个目录不仅存放Unity的缓存数据,还存放我们的更新资源,所以覆盖安装的时候为了避免读取到旧包更新下来的资源,我从两方面去规避:
    private const string APPLICATION_RESDIR_KEY = "ApplicationResDirKey";
    IEnumerator CheckSecondInstall()
    {
        if (!Application.isMobilePlatform)
        {
            yield break;
        }
    
        yield return new WaitForEndOfFrame();
    
        string localResDir = PlayerPrefs.GetString(APPLICATION_RESDIR_KEY, string.Empty);
        if (string.IsNullOrEmpty(localResDir))
        {
            PlayerPrefs.SetString(APPLICATION_RESDIR_KEY, Util.DataPath);   //记录新版本目录
            PlayerPrefs.Save();
        }
        else
        {
            string curResDir = Util.DataPath;
            if (!localResDir.Equals(curResDir))
            {
                if (!string.IsNullOrEmpty(localResDir) && Directory.Exists(localResDir))
                {
                    Directory.Delete(localResDir, true);   //删除旧资源存放目录
                }
    
                PlayerPrefs.SetString(APPLICATION_RESDIR_KEY, curResDir);
                PlayerPrefs.Save();
            }
        }
    }
    
    //Util.DataPath
    /// <summary>
    /// 数据存放目录
    /// </summary>
    public static string DataPath {
        get {
            string game = AppConst.AppName.ToLower();
            game = game + Application.version;     //目录名关联版本
            if (Application.isMobilePlatform) {
                return string.Format("{0}/{1}/", Application.persistentDataPath, game);
            }
            if (AppConst.DebugMode) {
                return string.Format("{0}/{1}/", Application.dataPath, AppConst.AssetDir);
            }
            if (Application.platform == RuntimePlatform.OSXEditor) {
                int i = Application.dataPath.LastIndexOf('/');
                return Application.dataPath.Substring(0, i + 1) + game + "/";
            }
            return "c:/" + game + "/";
        }
    }

    程序启动的时候去调用协程函数CheckSecondInstall(),这里面删除了旧的更新资源存放目录并记录新的资源存放目录Util.DataPath。Util.DataPath关联了打包时的版本亦可避免去旧的更新资源目录去读取资源。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值