[英]Unity - is there a way to show the popup in my game only once per game session?
[編輯] 現在一切正常! 感謝@MrMoonMan!
正如標題所說,我正在開發一款手機游戲,我剛剛實現了一個檢查更新彈出窗口。
問題是,每次加載菜單場景時都會顯示彈出窗口,這很煩人,因為如果玩家進入武器菜單然后回到主菜單 10 次,他會看到 10 次彈出窗口。 目標是讓彈出窗口在每個游戲會話中只出現一次,這是我目前嘗試但沒有成功的方法:
這是代碼:
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;
}
}
好的,所以您提到每次重新加載場景時它仍然會發生。 那是因為對象是在加載場景時創建的。 它不會在場景之間保存。 要正確處理這個問題,您需要告訴它在卸載場景時不要被破壞,並且這樣做很簡單,那就是添加 DontDestroyOnAwake。 以下是代碼示例:
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);
}
}
,,菜單場景''? 聽起來有些可疑。 如果您將整個新場景作為菜單加載和卸載,MonoBehaviours 中的狀態為布爾字段或修改場景結構(destory,create object)將不起作用,因為總是會從頭開始加載新場景。 ;)
如果您必須在單獨的場景中有菜單,請使用其他方式來管理狀態。 例如: DontDestoryOnLoad對象。 或者一些 C# 靜態類或單例(不是 MonoBehaviour)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.