【发布时间】:2018-06-20 10:45:27
【问题描述】:
现在我的想法有点混乱,因为我是 c# 的菜鸟。 顺便一提 。我正在做的是将我的设置保存在我使用切换创建的菜单中。 这是我的代码:
[SerializeField]
GameObject Option;
[SerializeField]
Button btn,btn2;
[SerializeField]
GameObject open, close;
[SerializeField]
GameObject[] opt;
bool startFinding = false;
//settings
bool livestream;
bool rendering;
bool fog;
bool cameraeffect;
int live;
void Start()
{
Option.SetActive(false);
Button popUp = btn.GetComponent<Button>();
Button popUp2 = btn2.GetComponent<Button>();
popUp.onClick.AddListener(PopUpOption);
popUp2.onClick.AddListener(ClosePopUp);
}
void Update()
{
if (startFinding)
{
StartCoroutine(GameOptions());
}
}
IEnumerator GameOptions()
{
if (PlayerPrefs.HasKey("SaveSettings"))
{
ActivateSettings();
Debug.Log("Theres a data");
} else
{
Debug.Log("Oops sorry there was no data");
}
//Get All the tags
opt = GameObject.FindGameObjectsWithTag("MobileOptions");
//fog , camera , livestream, rendering
if (opt[0].GetComponent<Toggle>().isOn == true && opt[1].GetComponent<Toggle>().isOn == true)
{
Debug.Log("Disable first the check box then choose only 1 option between" + "'rendering'"+ "and" + "'livestreaming'");
}
//Livestreaming
if (opt[0].GetComponent<Toggle>().isOn == true)
{
Debug.Log("Livestreaming Activate");
livestream = true;
} else
{
Debug.Log("Livestreaming Deactivate");
livestream = false;
}
//Rendering
if (opt[1].GetComponent<Toggle>().isOn == true)
{
rendering = true;
Debug.Log("Rendering Activate");
} else
{
Debug.Log("Rendering Deactivate");
rendering = false;
}
//Fog
if (opt[2].GetComponent<Toggle>().isOn == true)
{
fog = true;
Debug.Log("Fog Activated");
} else
{
Debug.Log("Fog Deactivated");
fog = false;
}
//Camera Effect
if (opt[3].GetComponent<Toggle>().isOn == true)
{
cameraeffect = true;
Debug.Log("Camera Effect Activated");
} else {
Debug.Log("Camera Effect Deactivated");
cameraeffect = false;
}
SaveSettings();
yield return null;
}
void SaveSettings()
{
try {
//convert the bool to int
live = livestream ? 1 : 0;
PlayerPrefs.SetInt("SaveSettings", live);
PlayerPrefs.Save();
}
catch (Exception ex)
{
Debug.LogError("Exception :" + ex);
}
}
void ActivateSettings()
{
live = PlayerPrefs.GetInt("SaveSettings");
}
我猜我的 playerprefs 不工作,但我的 debug.log 说它有数据。它搞砸了,或者我只是错过了一些关于 playerprefs 的东西。有人可以指出我做错了什么。我不想被困在这里。
【问题讨论】:
-
您正在将 PlayerPrefs 中的数据加载到一个整数中,该整数会立即被
livestream ? 1 : 0覆盖。live不会出现在其他上下文中。 -
@Draco18s 哦,先生。我只是跟着tuts,他正在工作。对不起我的英语不好。
-
我还尝试将
live = livestream ? 1 : 0; PlayerPrefs.SetInt("SaveSettings", live);更改为PlayerPrefs.SetInt("SaveSettings", livestream ? 1:0);和live = PlayerPrefs.GetInt("SaveSettings");更改为livestream = PlayerPrefs.GetInt("SaveSettings") == 1 ? true : false; -
什么时候将 startFinding 设置为 true?无需将 startFinding 设置为 true,只需调用 GameOption 方法即可。另外,我不明白为什么它是协程?
-
live仍然什么都不做。您想从保存文件中读取数据,然后将该数据放在有好处的地方。