Home > Enterprise >  Unity - is there a way to show the popup in my game only once per game session?
Unity - is there a way to show the popup in my game only once per game session?

Time:05-16

As the title says, I'm working on a mobile game and I just implemented a Check Update popup.

The thing is, the popup is displayed every time the menu scene is loaded, and this is very annoying because if the player goes to the weapon menu and then comes back to the main menu 10 times, he will see the popup 10 times. The goal is to have the popup appear only once per game session, Here is what I tried for the moment but without success:

  • Using a bool variable
  • Destroy the gameobject to which the checkupdate script is attached when the user presses the "No Thank's" button

Here is the code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class CheckUpdate : MonoBehaviour
{
    public string versionUrl = "";
    public string currentVersion;
    private string latestVersion;
    public GameObject newVersionAvailable;

    void Start()
    {
        StartCoroutine(LoadTxtData(versionUrl));
    }

    private IEnumerator LoadTxtData(string url)
    {
        UnityWebRequest loaded = new UnityWebRequest(url);
        loaded.downloadHandler = new DownloadHandlerBuffer();
        yield return loaded.SendWebRequest();

        latestVersion = loaded.downloadHandler.text;
        CheckVersion();
    }

    private void CheckVersion()
    {
        Debug.Log("currentVersion = "   currentVersion);
        Debug.Log("latestVersion = "   latestVersion);

        Version versionDevice = new Version(currentVersion);
        Version versionServer = new Version(latestVersion);
        int result = versionDevice.CompareTo(versionServer);

        if ((latestVersion != "") && (result < 0))
        {
            newVersionAvailable.SetActive(true);
        }
    }

    public void ClosePopUp(GameObject obj)
    {
        obj.SetActive(false);
        Destroy(this);
    }

    public void OpenURL(string url)
    {
        Application.OpenURL(url);
    }
}

CodePudding user response:

,,Menu scene''? It sound some suspicious. If you load and unload whole new scene as menu, state as bool field in MonoBehaviours or modifing the scene structure (destory, create object) will not work beacuse allways will be loaded new scene from scratch. ;)

If you have to have menu on seperated scene use someting other to manage the state. For example: DontDestoryOnLoad objects. Or some C# static class or singleton (not a MonoBehaviour).

CodePudding user response:

You can add the following to your code:

private static bool hasBeenChecked = false;
public static bool GetChecked()
{
    if (hasBeenChecked)
    {
        return hasBeenChecked
    }
        
    hasBeenChecked = true;
    return false;
}


private void CheckVersion()
{

    if (GetChecked())
        return;
        
    ...
}
  • Related