简体   繁体   中英

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

[EDIT] Everything is working now! Thank's to @MrMoonMan!

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;
    private static bool hasBeenChecked = false;

    void OnAwake()
    {
        DontDestroyOnLoad(this);
        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);
        }

        if (GetChecked())
            return;
    }

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

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

    public static bool GetChecked()
    {
        if (hasBeenChecked)
        {
            return hasBeenChecked;
        }

        hasBeenChecked = true;
        return false;
    }
}

EDIT

Ok so you mentioned that it still happens every time the scene is reloaded. That is because the object is created when the scene is loaded. It isn't saved between scenes. To handle that properly you need to tell it to not get destroyed when the scene is unloaded and to do that is simple, and that is by Adding DontDestroyOnAwake. Here is an example of the code:

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

    void OnAwake()
    {
        DontDestroyOnLoad(this);
    }

    void OnStart() 
    {
        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;
    }

    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);
    }

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

,,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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM