日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Unity 程序升级(PC版非热更新)

發(fā)布時間:2024/3/13 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity 程序升级(PC版非热更新) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用C# Winform做升級程序,在winform啟動程序中進(jìn)行升級信息的檢測,最后啟動unity程序。

Winform升級程序項目介紹

在博客園找到一個比較合適的升級程序 WINFORM自動更新程序 文章中有源碼放在碼云上可以下載。按照文章進(jìn)行配置即可,這里我遇到一些坑,大家可以注意一下。
目錄結(jié)構(gòu)

AutoUpdate與AutoUpdateGUI這兩個都是自動升級程序,AutoUpdate是靜默升級沒有升級界面,AutoUpdateGUI有程序的升級界面可以看到升級程序的下載進(jìn)度及下載文件的信息,如圖

  • AutoUpdateLib是升級程序的類庫。默認(rèn)編譯生成.exe的可執(zhí)行文件,可以將輸出類型改為類庫。
  • 每個項目里都會有一個說明文件,介紹每個項目如何配置與使用。
  • 項目編譯生成目錄 AutoUpdate-master\AutoUpdate\bin下 將編譯成 AutoUpdate.exe,AutoUpdateLib.dll,并與你的主程序放在同一個目錄。
  • 本地配置文件updatelist.xml,userconfig.xml 必須存在。服務(wù)器配置文件limit.xml,updatelist.xml 必須存在。
  • 限制的做法:服務(wù)端配置文件 limit.xml LimitKey,本地配置文件 UserConfig.xml UPDATE_KEY這一點要注意,key不一致不會升級。
  • 升級autoupdate.exe的做法:下載服務(wù)端的limit.xml文件看本在的updatelist.xml Autoupdate.exe的版本
    是否與limit.xml中AutoupdateVersion的值一致,不一致就下載autoupdate.exe文件
  • updatelist.xml這里的配置屬性要重點弄明白。
  • 如何啟動主程序參照 WINFORM自動更新程序 文章。

Window升級程序本地與服務(wù)器配置

使用IIS建立服務(wù)器升級站點,存放升級文件及配置信息。

所使用的配置文件信息

  • limit.xml
<?xml version="1.0" encoding="utf-8" ?> <Config><LimitKey>AGD</LimitKey><AutoupdateVersion>2.01.001</AutoupdateVersion> </Config>
  • UserConfig.xml
<?xml version="1.0" standalone="yes"?> <Config><UPDATE_KEY>AGD</UPDATE_KEY><IS_UPDATE_AGENT>NO</IS_UPDATE_AGENT><FIXXED_UPDATE_URL>http://193.168.1.88/UnityUpdate</FIXXED_UPDATE_URL><SHARE_FOLDER>http://193.168.1.88/UnityUpdate</SHARE_FOLDER><ONLY_KEEP_ME>YES</ONLY_KEEP_ME><USE_BAT UseBat="NO" StopBat="stop.bat" StartBat="start.bat" AlwaysExecStartBat="Yes" /> </Config>

說明:limit.xml中的LimitKey與UserConfig.xml中的UPDATE_KEY一致。UserConfig.xml中的 USE_BAT為NO。

  • 本地updatelist.xml
<?xml version="1.0" encoding="gb2312"?> <AutoUpdater><description>iPOS AutoUpdate</description><Updater><Url>http://193.168.1.88/UnityUpdate</Url><NeedUpdate>YES</NeedUpdate><RequiredVersion>9.5.0</RequiredVersion><LastUpdateTime>2016-10-20</LastUpdateTime><Continue>YES</Continue><CheckMd5>NO</CheckMd5><SilentMode>NO</SilentMode><DownloadTimeout>5000</DownloadTimeout></Updater><Application applicationId="CoalStart"><EntryPoint>CoalStart.exe</EntryPoint><Location>.</Location><Version>2.06.029</Version><VersionSort>Professional</VersionSort><KillPrograms>CoalStart.exe</KillPrograms></Application><Files><File IsKeyUpt="1" Ver="1.01.002" Name="CoalStart.exe" /><File IsKeyUpt="1" Ver="2.01.001" Name="BTLFAutoUpdate.exe" /><File IsKeyUpt="1" Ver="1.01.000" Name="build.rar" /></Files> </AutoUpdater>

說明:limit.xml中AutoupdateVersion與本地updatelist.xml中BTLFAutoUpdate的版本一致不需要進(jìn)行更新。(這里我修改了源碼中的升級程序的名稱)。升級文件build.rar的版本為1.01.000。

  • 服務(wù)器端updatelist.xml的配置信息
<?xml version="1.0" encoding="gb2312"?> <AutoUpdater><description>iPOS AutoUpdate</description><Updater><Url>http://193.168.1.88/UnityUpdate</Url><!-- ftp://yishion:yishion@localhost/test/ --><NeedUpdate>YES</NeedUpdate><RequiredVersion>9.5.0</RequiredVersion><LastUpdateTime>2016-10-20</LastUpdateTime><Continue>YES</Continue><CheckMd5>NO</CheckMd5><SilentMode>NO</SilentMode><DownloadTimeout>5000</DownloadTimeout></Updater><Application applicationId="CoalStart"><EntryPoint>CoalStart.exe</EntryPoint><Location>.</Location><Version>2.06.029</Version><VersionSort>Professional</VersionSort><KillPrograms>CoalStart.exe</KillPrograms></Application><Files><File IsKeyUpt="1" Ver="1.01.002" Name="CoalStart.exe" /><File IsKeyUpt="1" Ver="2.01.001" Name="BTLFAutoUpdate.exe" /><File IsKeyUpt="1" Ver="1.01.001" Name="build.rar" /></Files> </AutoUpdater>

說明:服務(wù)器端的升級文件build.rar的版本號為1.01.001比本地文件的版本號高,在打開主程序就會檢測需要更新文件。

升級程序文件目錄
  • 服務(wù)器目錄結(jié)構(gòu)
  • 本地目錄結(jié)構(gòu)

Window升級程序改寫

  • 修改啟動的主程序
    新建項目CoalStart,在Program.cs類中主程序啟動改寫為AutoUpdate.InvokeUpdate("CoalStart.exe", "CoalStart", StartMain, args);
  • 修改升級程序的名稱為BTLFAutoUpdate。
  • 修改項目程序集名稱
  • 修改AutoUpdateLib類庫中InvokeUpdate.cs的InvokeUpdate方法,改為Common.ProgramRunned(Common.CombineDir(Common.AppDir, “BTLFAutoUpdate.exe”), true, false);
  • 修改AutoUpdateLib類庫中的Autoupdating.cs的啟動類的名稱。

    說道這,感覺源碼有個小bug ,Autoupdate.exe大小寫不一致。這個坑我蹲了好久。
  • 修改AutoUpdateGUI項目的FrmUpdate文件中的CopyFile方法將AUTOUPDATE改為BTLFAUTOUPDATE。
  • 最后就是配置文件中有關(guān)AutoUpdate.exe改為BTLFAutoUpdate.exe就可以了。
    • 加入解壓縮功能
      由于我的升級文件是一個unity的構(gòu)建之后自己壓縮的一個壓縮包,在升級下載后需要進(jìn)行自動的文件解壓。改寫源碼,我使用的更新模式非靜默更新,因此找到AutoUpdateGUI項目中的FrmUpdate的類文件中DownUpdateFilePart方法加入如下代碼。

    lbState.Text = "解壓文件中!";string path = System.IO.Directory.GetCurrentDirectory();if (!Exists()){MessageBox.Show("不支持RAR解壓縮");return;}//解a壓1try{unCompressRAR(path + "\\setup", path+ "\\temp\\Downloads", "build.rar", false);//MessageBox.Show("解壓完成!");}catch (Exception ex){MessageBox.Show(ex.Message);}lbState.Text = "文件下載并解壓完成!";

    壓縮包解壓方法,需要WinRAR.exe電腦上安裝一個吧,(為啥不用ICSharpCode.SharpZipLib.dll,zip解壓出來少文件,頭疼!!)

    /// <summary>/// 解壓縮文件t/// </summary>/// <param name="unRarPatch">解壓縮后的文件所要存放的路徑?</param>/// <param name="rarPatch">rar文件所在的路徑</param>/// <param name="rarName">rar文件名</param>/// <param name="deleteFlag"></param>/// <returns></returns>public static string unCompressRAR(string unRarPatch, string rarPatch, string rarName, bool deleteFlag){try{RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");string str = key.GetValue("").ToString();key.Close();if (!System.IO.Directory.Exists(unRarPatch)){System.IO.Directory.CreateDirectory(unRarPatch);}string str2 = "x \"" + rarName + "\" \"" + unRarPatch + "\"" + " -y";ProcessStartInfo info = new ProcessStartInfo();info.FileName = str;info.Arguments = str2;info.WindowStyle = ProcessWindowStyle.Normal;info.WorkingDirectory = rarPatch;Process process = new Process();process.StartInfo = info;process.Start();process.WaitForExit();process.Close();if (deleteFlag && System.IO.File.Exists(rarPatch + @"\" + rarName)){System.IO.File.Delete(rarPatch + @"\" + rarName);}}catch (Exception exception){throw exception;}return unRarPatch;}/// <summary>/// 判讀是否支持RAR自動解壓縮/// </summary>/// <returns></returns>public static bool Exists(){return !string.IsNullOrEmpty(Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe").GetValue("").ToString());}

    Winform主程序調(diào)用unity程序

    Winform升級程序負(fù)責(zé)文件的檢測與unity程序的調(diào)用,這時候就需要進(jìn)行邏輯判斷。

    • 美化主啟動程序

    • 改寫代碼

    FrmMain_Shown中開啟線程,延時1秒進(jìn)行升級程序的檢測。存在則隱藏主程序,否則開啟unity可執(zhí)行程序。
    開啟程序使用引入[DllImport("shell32.dll")] public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
    調(diào)用unity可執(zhí)行程序 ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("Coal.exe"), new StringBuilder(""), new StringBuilder(@"" + System.IO.Directory.GetCurrentDirectory() + "\\setup\\build"), 1);

    public partial class FrmMain : Form{SynchronizationContext _syncContext = null;public FrmMain(){InitializeComponent();_syncContext = SynchronizationContext.Current;}[DllImport("shell32.dll")]public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);/// <summary>/// 取去除文件擴(kuò)展名之后的文件字符串/// </summary>/// <param name="Prog"></param>/// <returns></returns>private static string GetShortExec(string Prog){int Pos = Prog.LastIndexOf(".");return (Pos > 0 ? Prog.Substring(0, Pos) : Prog);}/// <summary>/// 取程序名(去除路徑,擴(kuò)展名)/// </summary>/// <param name="fileName"></param>/// <returns></returns>private static string GetFileNameOnly(string fileName){return GetShortExec(new System.IO.FileInfo(fileName).Name);}/// <summary>/// 返回進(jìn)程id, c:\windows\system32\Notepad.exe,notepad.exe/// </summary>/// <param name="ProgFullName"></param>/// <param name="Kill"></param>/// <returns></returns>public static int ProgramRunned(string ProgName, bool isFullName = false, bool Kill = false){string Prog = isFullName ?GetFileNameOnly(ProgName): GetShortExec(ProgName);System.Diagnostics.Process[] mProcs =System.Diagnostics.Process.GetProcessesByName(Prog);if (0 == (mProcs?.Length ?? 0)) return 0;int pid = 0;for (int i = 0, Count = mProcs.Length; i < Count; i++){System.Diagnostics.Process proc = mProcs[i];bool exists = false;if (isFullName)exists = ProgName.Equals(proc.MainModule.FileName, StringComparison.OrdinalIgnoreCase);elseexists = proc.ProcessName.Equals(ProgName, StringComparison.OrdinalIgnoreCase);if (exists){pid = proc.Id;if (Kill){proc.Kill();}}}return pid;}public static void Delay(int milliSecond){int start = Environment.TickCount;while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒{continue;}}void checkOpenForm() {Delay(1000);int updatecount = ProgramRunned("BTLFAutoUpdate");if (updatecount > 0){Thread demoThread = new Thread(new ThreadStart(threadMethod));demoThread.IsBackground = true;demoThread.Start();//啟動線程}else{ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("Coal.exe"), new StringBuilder(""), new StringBuilder(@"" + System.IO.Directory.GetCurrentDirectory() + "\\setup\\build"), 1);ProgramRunned("CoalStart", false, true);}}private void threadMethod(){_syncContext.Post(SetFormHide, "");//子線程中通過UI線程上下文更新UI }private void SetFormHide(object obj){this.Hide();}private void FrmMain_Shown(object sender, EventArgs e){Thread tOpenForm;tOpenForm = new Thread(checkOpenForm);tOpenForm.Start();}}

    最終效果

    unity PC端啟動升級程序

    總結(jié)

    以上是生活随笔為你收集整理的Unity 程序升级(PC版非热更新)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。