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 都是沒問題的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章