Hello,
I have wrote a simple update program in Unity. The system will download a zip file of my server. This should then be automatically extracted. For the extraction I use an external libary, named Ionic.Zip. Its very simple to work with Ionic. The program works fine in Unity self, but when I export it to an exe-file, it doesn’t work. The software stopps, at the extraction.
Here is my code:
using UnityEngine;
using System;
using System.IO;
using System.Net;
using System.IO.Compression;
using System.Diagnostics;
using Ionic.Zip;
public class Updater : MonoBehaviour
{
public GUISkin centerSkin;
public GUISkin progressSkin;
public Texture2D progressBar;
internal string currentVersion;
private bool foundUpdate = false;
private bool haveNewest = false;
private bool downloaded = false;
private bool unzip = false;
private int progress = 0;
private int zipFiles = 0;
private int extracted = 0;
void Start()
{
string appdataLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string gameLocation = appdataLocation + "\\FrozenShard\\";
string versionLocation = gameLocation + "version.txt";
if( File.Exists(versionLocation) )
currentVersion = File.ReadAllText(versionLocation);
else
currentVersion = "-1";
string newestVersion = connectToServer("http://link.de/games/frozenshard/version.php");
if( !newestVersion.Equals(currentVersion) )
foundUpdate = true;
else
haveNewest = true;
if( foundUpdate )
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(downloadComplete);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(changedProgress);
webClient.DownloadFileAsync(new Uri("http://link.de/games/frozenshard/Game.zip"), @gameLocation + "Game.zip");
}
else if( haveNewest )
{
downloaded = true;
unzip = true;
foundUpdate = true;
}
}
void OnGUI()
{
int w = Screen.width;
if( !foundUpdate )
GUI.Label(new Rect(0, 10, w, 100), "Checking for updates", centerSkin.label);
else if( foundUpdate )
{
if( !downloaded )
GUI.Label(new Rect(0, 10, w, 100), "Downloading update", centerSkin.label);
else if( downloaded !unzip )
GUI.Label(new Rect(0, 10, w, 100), "Unzipping game", centerSkin.label);
else
GUI.Label(new Rect(0, 10, w, 100), "Ready to play", centerSkin.label);
if( !downloaded )
{
float onePercent = Screen.width / 100.0f;
float progressWidth = onePercent * progress;
GUI.DrawTexture(new Rect(0, Screen.height - 60, progressWidth, 15), progressBar);
GUI.Label(new Rect((w / 2) - 50, Screen.height - 90, 100, 20), "Progress: " + progress + "%", progressSkin.label);
}
else if( downloaded unzip )
{
if( GUI.Button(new Rect((Screen.width / 2) - 100, Screen.height - 60, 200, 30), "PLAY!") )
{
string appdataLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string gameLocation = appdataLocation + "\\FrozenShard\\";
Process.Start(gameLocation + "\\Secret of the Frozen Shard.exe");
Application.Quit();
}
}
}
}
private string connectToServer(string link)
{
WebClient client = new WebClient();
return client.DownloadString(link);
}
private void downloadComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
downloaded = true;
openZIP();
}
private void changedProgress(object sender, DownloadProgressChangedEventArgs e)
{
progress = e.ProgressPercentage;
}
private void openZIP()
{
string appdataLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string gameLocation = appdataLocation + "\\FrozenShard\\";
using( ZipFile zip1 = ZipFile.Read(gameLocation + "Game.zip") )
{
progress = 0;
zipFiles = zip1.Count;
foreach( ZipEntry e in zip1 )
{
e.Extract(gameLocation, ExtractExistingFileAction.OverwriteSilently);
extracted++;
}
}
refreshVersion();
}
private void refreshVersion()
{
string appdataLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string gameLocation = appdataLocation + "\\FrozenShard\\";
string newestVersion = connectToServer("http://link.de/games/frozenshard/version.php");
File.WriteAllText(gameLocation + "\\version.txt", newestVersion);
deleteZIPFile();
}
private void deleteZIPFile()
{
string appdataLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string gameLocation = appdataLocation + "\\FrozenShard\\";
File.Delete(gameLocation + "\\Game.zip");
unzip = true;
}
}
Please help me.
Jordan