123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using UnityEngine;
- using System;
- using System.Collections;
- /// <summary>
- /// This component will ask the server what is the latest version. The client will send the app version, platform and language,
- /// and the server will reply accordingly.
- /// If there is an update available, it will fire a ClientUpdateAvailable notification, with VersionInfo
- /// The VersionInfo will contain the version number, what's new and if it is a forced update.
- ///
- /// This component should be placed on an active gameObject in the hierarchy
- /// </summary>
- public class VersionChecker : MonoBehaviour
- {
- //DO NOT FILL THIS MANUALLY. THIS IS AUTOMATICALLY READ FROM PlayerSettings.BundleVersion
- //THROUGH AV/Build
- public const string BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS = "1.0";
- private const string CACHED_VERSION_KEY = "cachedVersion";
- static float _localVersion = -1f;
- public static float LocalVersion
- {
- get
- {
- if (_localVersion == -1f)
- {
- if (!float.TryParse(BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS, out _localVersion))
- {
- _localVersion = 1;
- AVDebug.LogError("Error parsing BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS, must be a float: "+BUNDLE_VERSION_NUMBER_FROM_PLAYER_SETTINGS);
- }
- }
- return _localVersion;
- }
- }
- public void Check(string language)
- {
- StartCoroutine(CheckCoroutine(language));
- }
- IEnumerator CheckCoroutine(string language)
- {
- string platform;
- #if UNITY_EDITOR || DEBUG
- {
- platform = "Testing";
- }
- #elif UNITY_WEBPLAYER
- {
- platform = "Web";
- }
- #elif UNITY_IPHONE
- {
- platform = "iOS";
- }
- #elif UNITY_ANDROID
- {
- platform = "Android";
- }
- #else
- {
- AVDebug.LogError("Unsupported platform");
- yield break;
- }
- #endif
- WWW www = new WWW(string.Format("{0}version.php?platform={1}&client={2}&language={3}", GameConstants.SERVER_BASE_URL, platform, LocalVersion, language));
- yield return www;
-
- if (www.error == null)
- {
- PlayerPrefs.SetString(CACHED_VERSION_KEY, www.text);
- PlayerPrefs.Save();
- ParseVersionUpdate(www.text);
- } else
- {
- AVDebug.LogWarning("Error while retrieving game version from server (using cached version) : " + www.error);
- if (PlayerPrefs.HasKey(CACHED_VERSION_KEY))
- {
- ParseVersionUpdate(PlayerPrefs.GetString(CACHED_VERSION_KEY));
- }
- }
- }
- void ParseVersionUpdate(string s)
- {
- string[] tokens = s.Split('\n');
- VersionInfo versionInfo;
- if (!float.TryParse(tokens[0], out versionInfo.version))
- {
- AVDebug.LogError("Error parsing remote version, must be a float : "+tokens[0]);
- }
- if (LocalVersion >= versionInfo.version)
- {
- //client is up to date
- return;
- }
- versionInfo.forceUpdate = tokens[1] == "ForceUpdate";
- versionInfo.whatsNew = tokens[2];
- CoreNotificationCenter.Post(CoreNotificationType.ClientUpdateAvailable, versionInfo);
- }
- }
|