Unity策略模式实现通用版本检测框架

本篇博客根据liangxiegame.com小班直播内容的总结  

unity热更新会对比服务器版本和本地版本 如果本地版本比较低可以选择性的更新

本节课的代码是根据QFramework框架和UNIRx插件编写 ,使用前确保QFramework和Unirx环境配置好

演示

版本更新,资源更新  目前只在PC端模拟了加载 路径 ,主要用到了匿名方法Action事件这些知识点。

使用策略模式,可扩展其他端的更新加载 实现,目前实现了PC,可以自己扩展安卓 ios

我先上一个代码

IVersionCeckStrategy 接口 和代码实现

using System;
using System.IO;
using UnityEngine;
using UniRx;
namespace QF
{
    public interface IVersionCeckStrategy
    {
        string ServerUrl { get; }
        void LocalVersionGetter(Action<int> onLocalVersionGot);
        void ServerVersionGetter(Action<int> onLocalVersionGot);
        void Update(Action Update);
    }

    public class SimulateVrtsionCheckStarategy : IVersionCeckStrategy
    {
        public string ServerUrl
        {
            get { return Path.Combine(Application.dataPath, "../SimulateServer/"); }
        }

        public void LocalVersionGetter(Action<int> onLocalVersionGot)
        {
            string versionFile = Application.streamingAssetsPath + "/Version.txt";
            if (NewBehaviourScript.Updateed)
            {
                versionFile = Application.persistentDataPath + "/Version.txt";
            }
            ObservableWWW.Get(versionFile).Subscribe((version) =>
            {
                Debug.Log("本地版本" + version);
                onLocalVersionGot(int.Parse(version));
            });
        }

        public void ServerVersionGetter(Action<int> onServerVersionGot)
        {
            string versionFile = ServerUrl + "Version.txt";
            ObservableWWW.Get(versionFile).Subscribe((version) =>
            {
                Debug.Log("服务器版本" + version);
                onServerVersionGot(int.Parse(version));
            });
        }
        public void Update(Action Update)
        {
            var serverVersionFile = ServerUrl + "Version.txt";
            var serverResFile = ServerUrl + "Res.txt";
            var LocalVersionFile = Application.persistentDataPath + "/Version.txt";
            var LocalResFile = Application.persistentDataPath + "/Res.txt";
            File.Copy(serverVersionFile, LocalVersionFile);
            File.Copy(serverResFile, LocalResFile);
            NewBehaviourScript.Updateed = true;
            Update();
        }
    }
}

扩展 版本通用类

using System;
using UnityEngine;

namespace QF
{
    public interface IVersionCheckDialog
    {
        Action onUpdate { get; set; }
        Action onCancle { get; set; }
    }
    public class VersionCheckKit
    {
        private static IVersionCeckStrategy versionCeckStrategy = null;
        public static void SetStrategy(IVersionCeckStrategy strategy)
        {
            versionCeckStrategy = strategy;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="onVersionChecked"> 是否要更新,本地版本,服务器版本</param>
        public static void CheckVersion(Action<bool, int, int> onVersionChecked, Action gameLaunch, IVersionCheckDialog versionCheckDialog)
        {
            versionCeckStrategy.LocalVersionGetter((localVersion) =>
            {
                versionCeckStrategy.ServerVersionGetter((serverVersion) =>
                {
                    if (localVersion < serverVersion)
                    {
                        var selectUpdate = false;
                        versionCheckDialog.onUpdate = () =>
                        {
                            versionCeckStrategy.Update(() =>
                            {
                                Debug.Log("下载资源");
                                Debug.Log("替换资源");
                                gameLaunch();
                            });
                        };
                        versionCheckDialog.onCancle = () =>
                       {
                           gameLaunch();
                       };
                        onVersionChecked(selectUpdate, localVersion, serverVersion);
                    }
                    else
                    {
                        gameLaunch();
                    }
                });

            });
        }
    }
}

版本策略的使用

using System;
using System.Collections;
using System.Collections.Generic;
using UniRx;
using UnityEditor;
using UnityEngine;
namespace QF
{


    public class NewBehaviourScript : MonoBehaviour
    {
        public class Dialog : IVersionCheckDialog
        {
            public Action onUpdate { get; set; }
            public Action onCancle { get; set; }
            public int localVersion { get; set; }
            public int ServerVersion { get; set; }
            public void Open()
            {
                if (EditorUtility.DisplayDialog("服务器有新版本", "本地版本" + localVersion + "\n服务器版本" + ServerVersion, "更新", "取消"))
                {
                    onUpdate();
                }
                else
                {
                    onCancle();
                }
            }
        }
        public static bool Updateed
        {
            get { return PlayerPrefs.GetInt("Update", 0) == 1 ? true : false; }
            set
            {
                PlayerPrefs.SetInt("Update", value == true ? 1 : 0);
            }
        }
        void Start()
        {
            var checkDialog = new Dialog();
            //策略模式 更新PC端
            VersionCheckKit.SetStrategy(new SimulateVrtsionCheckStarategy());
            //扩展安卓版本   集成 IVersionCeckStrategy接口 自己扩展就好
            //VersionCheckKit.SetStrategy(new SimulateAndroidVrtsionCheckStarategy());
            VersionCheckKit.CheckVersion((hasNewVersion, localversion, serverVersion) =>
            {
                checkDialog.localVersion = localversion;
                checkDialog.ServerVersion = serverVersion;
                checkDialog.Open();
            }, GameStart, checkDialog);
        }
        public void GameStart()
        {
            Debug.Log("游戏启动");
            var resFile = Application.streamingAssetsPath + "/Res.txt";
            if (Updateed)
            {
                resFile = Application.persistentDataPath + "/Res.txt";
            }
            ObservableWWW.Get(resFile).Subscribe((contetn) =>
            {
                Debug.Log(contetn);
            });
        }
    }
}

运行实现该代码  自己扩展安卓 ios 都是没问题的

猜你喜欢

转载自blog.csdn.net/qq_36848370/article/details/105370475