Search Unity

Check for game update

Discussion in 'Scripting' started by glgamesforfun, Sep 25, 2021.

  1. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Hi,
    is there a way to automatically check for new updates? When a new update is available, you can go to the Play Store with a link and download it. Is that possible? Thanks in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    If you need to force your own update you probably need to host your own notion of what is the necessary version number (such as on your own website) and have your app check each time it starts.

    While fairly simple, it is a lot of work and moving parts to maintain ove rtime, and if you don't need it, just leave it because most people (well over 95%) have their phones set to automatic update and they will get an update within a day or two.

    How much work do you want to do to reduce that tiny timespan?
     
    tsukimi likes this.
  3. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    I want to make it like the men in the video here:

    And my question is: Can i make a short short query like if (application.version < newVersion)
    (i know that the example does not work. Just for the idea.)
     
  4. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    I want to make the ui, ... myself and i only need a short query (if it is possible)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Yes, but you still need to get the newVersion value to your old application in the field. See hosting it on your own webserver as I listed above.
     
  6. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Ok, and what if i change the newVersion Value manuelly, every time I launch a new Update? Just an idea
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    How would the old program(s) get that value?
     
  8. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    That's my question. With Application.version you can get the oldVersion on which the player is playing atm. But i don't know how to write (example: 1.2.56) and in which operator.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
  10. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Thank you. Very helpful. I'll test the code.
     
  11. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Works fine! I added it to my Loading Scene and check if the player can do an upgrade.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. using System;
    7.  
    8. public class LoadingScript : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     private Image progressBar;
    12.  
    13.     [SerializeField]
    14.     private Text percentLoadText;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         StartCoroutine(LoadingAsyncOperation());
    20.         Version();
    21.     }
    22.  
    23.     IEnumerator LoadingAsyncOperation()
    24.     {
    25.         yield return null;
    26.         AsyncOperation loadGame = SceneManager.LoadSceneAsync(1);
    27.         loadGame.allowSceneActivation = false;
    28.  
    29.         while (!loadGame.isDone)
    30.         {
    31.             progressBar.fillAmount = loadGame.progress;
    32.             percentLoadText.text = (loadGame.progress * 100).ToString("F0") + "%";
    33.  
    34.             if (loadGame.progress >= 0.9f)
    35.             {
    36.                 progressBar.fillAmount = 1f;
    37.                 percentLoadText.text = "100%";
    38.             }
    39.  
    40.             yield return null;
    41.         }
    42.     }
    43.  
    44.     static int VersionCompare(string v1, string v2)
    45.     {
    46.         // vnum stores each numeric
    47.         // part of version
    48.         int vnum1 = 0, vnum2 = 0;
    49.  
    50.         // loop until both string are
    51.         // processed
    52.         for (int i = 0, j = 0; (i < v1.Length || j < v2.Length);)
    53.         {
    54.  
    55.             // storing numeric part of
    56.             // version 1 in vnum1
    57.             while (i < v1.Length && v1[i] != '.')
    58.             {
    59.                 vnum1 = vnum1 * 10 + (v1[i] - '0');
    60.                 i++;
    61.             }
    62.  
    63.             // storing numeric part of
    64.             // version 2 in vnum2
    65.             while (j < v2.Length && v2[j] != '.')
    66.             {
    67.                 vnum2 = vnum2 * 10 + (v2[j] - '0');
    68.                 j++;
    69.             }
    70.  
    71.             if (vnum1 > vnum2)
    72.                 return 1;
    73.             if (vnum2 > vnum1)
    74.                 return -1;
    75.  
    76.             // if equal, reset variables and
    77.             // go for next numeric part
    78.             vnum1 = vnum2 = 0;
    79.             i++;
    80.             j++;
    81.         }
    82.         return 0;
    83.     }
    84.  
    85.     static void Version()
    86.     {
    87.         string versionNew = "1.0.5";
    88.         string versionOld = Application.version;
    89.  
    90.         if (VersionCompare(versionNew, versionOld) < 0)
    91.             Debug.Log("Up-to-date!");
    92.         else if (VersionCompare(versionNew, versionOld) > 0)
    93.             Debug.Log("Update Game!");
    94.         else
    95.             Debug.Log("Up-to-date!");
    96.     }
    97. }
     
  12. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Hi, I found a problem with the Script. For example: Player has version 1.0.0 and install 1.0.1 . In this version there is the string (versionNew) 1.0.1 too. If I launch a new update, the value (versionNew) is still 1.0.1 .
    This Script don't work for this thing.
    What can i do instead of this? @Kurt-Dekker ?