UpdateChecker.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace ParrelSync.Update
  5. {
  6. /// <summary>
  7. /// A simple update checker
  8. /// </summary>
  9. public class UpdateChecker
  10. {
  11. //const string LocalVersionFilePath = "Assets/ParrelSync/VERSION.txt";
  12. public const string LocalVersion = "1.5.0";
  13. [MenuItem("ParrelSync/Check for update", priority = 20)]
  14. static void CheckForUpdate()
  15. {
  16. using (System.Net.WebClient client = new System.Net.WebClient())
  17. {
  18. try
  19. {
  20. //This won't work with UPM packages
  21. //string localVersionText = AssetDatabase.LoadAssetAtPath<TextAsset>(LocalVersionFilePath).text;
  22. string localVersionText = LocalVersion;
  23. Debug.Log("Local version text : " + LocalVersion);
  24. string latesteVersionText = client.DownloadString(ExternalLinks.RemoteVersionURL);
  25. Debug.Log("latest version text got: " + latesteVersionText);
  26. string messageBody = "Current Version: " + localVersionText +"\n"
  27. +"Latest Version: " + latesteVersionText + "\n";
  28. var latestVersion = new Version(latesteVersionText);
  29. var localVersion = new Version(localVersionText);
  30. if (latestVersion > localVersion)
  31. {
  32. Debug.Log("There's a newer version");
  33. messageBody += "There's a newer version available";
  34. if(EditorUtility.DisplayDialog("Check for update.", messageBody, "Get latest release", "Close"))
  35. {
  36. Application.OpenURL(ExternalLinks.Releases);
  37. }
  38. }
  39. else
  40. {
  41. Debug.Log("Current version is up-to-date.");
  42. messageBody += "Current version is up-to-date.";
  43. EditorUtility.DisplayDialog("Check for update.", messageBody,"OK");
  44. }
  45. }
  46. catch (Exception exp)
  47. {
  48. Debug.LogError("Error with checking update. Exception: " + exp);
  49. EditorUtility.DisplayDialog("Update Error","Error with checking update. \nSee console for more details.",
  50. "OK"
  51. );
  52. }
  53. }
  54. }
  55. }
  56. }