I managed to get my FPS counter saved whether it was toggled on or off in the main menu when loading into the level scene, but for some reason my timer and collectible counter don't get the same justice when loading into the next scene, here is my code, much help would be appreciated, prepare for a long post as this I'm doing each ui element in separate code files.
我设法让我的FPS计数器保存,无论它是在主菜单中打开或关闭时加载到关卡场景,但出于某种原因,我的计时器和可收集的计数器在加载到下一个场景时没有得到相同的公正,这是我的代码,很多帮助将不胜感激,准备一个很长的帖子,因为我在不同的代码文件中做每个UI元素。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class FPSToggleOnLoad : MonoBehaviour
{
public string startScene = "Tutorial01";
public bool newValue;
//UnityEngine.UI.Toggle toggle = null;
public Toggle toggle;
public TMP_Text fpstext;
void Start()
{
if (PlayerPrefs.HasKey("tutorial"))
{
if (PlayerPrefs.GetInt("tutorial") == 1)
{
newValue = true;
toggle.isOn = true;
}
else
{
newValue = false;
toggle.isOn = false;
}
}
else
{
newValue = true;
}
}
public void SceneTransitioner()
{
//SceneManager.LoadScene(startScene);
}
public void Toggle_Changed(bool newValue)
{
if (newValue == true)
{
fpstext.enabled = true;
startScene = "Tutorial01";
PlayerPrefs.SetInt("tutorial", 1);
}
else
{
fpstext.enabled = false;
startScene = "Level01";
PlayerPrefs.SetInt("tutorial", 0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class FlowerToggleOnLoad : MonoBehaviour
{
public string startScene = "Tutorial01";
public bool newValue;
//UnityEngine.UI.Toggle toggle = null;
public Toggle toggle;
public Image flowertextimage;
public Text flowertext;
void Start()
{
if (PlayerPrefs.HasKey("tutorial"))
{
if (PlayerPrefs.GetInt("tutorial") == 1)
{
newValue = true;
toggle.isOn = true;
}
else
{
newValue = false;
toggle.isOn = false;
}
}
else
{
newValue = true;
}
}
public void SceneTransitioner()
{
//SceneManager.LoadScene(startScene);
}
public void Toggle_Changed(bool newValue)
{
if (newValue == true)
{
flowertext.enabled = true;
flowertextimage.enabled = true;
startScene = "Tutorial01";
PlayerPrefs.SetInt("tutorial", 1);
}
else
{
flowertext.enabled = false;
flowertextimage.enabled = false;
startScene = "Level01";
PlayerPrefs.SetInt("tutorial", 0);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class TimerLoadOnToggle : MonoBehaviour
{
public string startScene = "Tutorial01";
public bool newValue;
//UnityEngine.UI.Toggle toggle = null;
public Toggle toggle;
public TMP_Text timertext;
void Start()
{
if (PlayerPrefs.HasKey("tutorial"))
{
if (PlayerPrefs.GetInt("tutorial") == 1)
{
//fpstext.enabled = true;
newValue = true;
toggle.isOn = true;
}
else
{
//fpstext.enabled = false;
newValue = false;
toggle.isOn = false;
}
}
else
{
newValue = true;
}
}
public void SceneTransitioner()
{
//SceneManager.LoadScene(startScene);
}
public void Toggle_Changed(bool newValue)
{
if (newValue == true)
{
timertext.enabled = true;
startScene = "Tutorial01";
PlayerPrefs.SetInt("tutorial", 1);
}
else
{
timertext.enabled = false;
startScene = "Level01";
PlayerPrefs.SetInt("tutorial", 0);
}
}
}
I was expecting the same thing to work with my other UI elements considering it worked with the fps counter just fine especially considering all three of these things are basically just text or TextMeshPro but it didn't for some reason.
考虑到它与fps计数器一起工作得很好,我本以为我的其他UI元素也会有同样的效果,特别是考虑到这三个元素基本上都只是文本或TextMeshPro,但由于某些原因,它并不能。
更多回答
优秀答案推荐
It's normal behavior. You're setting values, not saving them.
这是正常的行为。你是在设定值,而不是保存它们。
Because you seem to want a realtime effect, after using any of the PlayerPrefs.Set*()
functions you need to call PlayerPrefs.Save()
in order to make them persistent.
因为您似乎想要实时效果,所以在使用任何PlayerPrefs.Set*()函数后,您需要调用PlayerPrefs.Save()以使其持久。
According to the documentation: Note: Since writing the PlayerPrefs can cause hiccups, it is recommended to not call this function during gameplay..
根据文档:注意:由于编写PlayerPrefs会导致打嗝,建议在游戏过程中不要调用该函数。
So I don't recommend calling it after every Set*()
function, but rather either after you have some sort of game workflow or before loading the next scene. Or even better, add a "Save" button for it. Setting it in Update()
or any other function that is called often might degrade performance.
因此,我不建议在每个set*()函数之后调用它,而是在您有某种游戏工作流之后或在加载下一个场景之前调用它。或者更好的是,为它添加一个“保存”按钮。在Update()或任何其他经常调用的函数中设置它可能会降低性能。
You are forget to save PlayerPrefs values. After PlayerPrefs.SetInt();
you should add this line: PlayerPrefs.Save()
您忘记了保存PlayerPrefs值。在PlayerPrefs.SetInt()之后;您应该添加此行:PlayerPrefs.Save()
更多回答
我是一名优秀的程序员,十分优秀!