现在我的思绪有些混乱,因为玩家优先选择我是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的东西。有人能指出我做错了什么。我不想被困在这里。
答案 0 :(得分:0)
正如您之前提到的here,您在StartCoroutine(GameOptions())
方法中调用了Update
这是错误的!那是因为你一遍又一遍地调用SaveSettings()
因为PlayerPrefs.SetInt("SaveSettings", live)
在Update
方法中调用了太多次
答案 1 :(得分:0)
我做的是这个,我知道这个协程并不是真正的大问题。
[SerializeField]
Button openButton, closeButton;
[SerializeField]
GameObject OPTION_UI;//parent
[SerializeField]
GameObject open_option_button, close_option_button;
[SerializeField]
GameObject[] settings;
[SerializeField]
Toggle[] toggle;
bool Found = false;
void Start()
{
OPTION_UI.SetActive(false);
Button OPENBUTTON = openButton.GetComponent<Button>();
Button CLOSEBUTTON = closeButton.GetComponent<Button>();
OPENBUTTON.onClick.AddListener(OpenUI);
CLOSEBUTTON.onClick.AddListener(CloseUI);
toggle[2].isOn = true;
//fog
if (PlayerPrefs.GetInt("SaveFog") == 1)
{
toggle[0].isOn = true;
}
else
{
toggle[0].isOn = false;
}
//camera
if (PlayerPrefs.GetInt("SaveCamera") == 1)
{
toggle[1].isOn = true;
}
else
{
toggle[1].isOn = false;
}
//livestreaming
if (PlayerPrefs.GetInt("SaveLiveStream") == 1)
{
toggle[2].isOn = true;
}
else
{
toggle[2].isOn = false;
}
//rendering
if (PlayerPrefs.GetInt("SaveRendering") == 1)
{
toggle[3].isOn = true;
}
else
{
toggle[3].isOn = false;
}
}
void Update()
{
if (Found)
{
StartCoroutine(GameOptions());
} else
{
StopCoroutine(GameOptions());
}
}
IEnumerator GameOptions()
{
/*
* fog , camera
* livestream, rendering
* */
//get the options
settings = GameObject.FindGameObjectsWithTag("MobileOption");
//fog
if (toggle[0].isOn == true)
{
PlayerPrefs.SetInt("SaveFog", 1);
} else
{
PlayerPrefs.SetInt("SaveFog", 0);
}
//camera
if (toggle[1].isOn == true)
{
PlayerPrefs.SetInt("SaveCamera", 1);
} else
{
PlayerPrefs.SetInt("SaveCamera", 0);
}
//livestream
if (toggle[2].isOn == true)
{
PlayerPrefs.SetInt("SaveLiveStream" , 1);
} else
{
PlayerPrefs.SetInt("SaveLiveStream", 0);
}
//rendering
if (toggle[3].isOn == true)
{
PlayerPrefs.SetInt("SaveRendering", 1);
} else
{
PlayerPrefs.SetInt("SaveRendering", 0);
}
yield return null;
}
//activate UI
void OpenUI()
{
Found = true;
open_option_button.SetActive(false);
close_option_button.SetActive(true);
OPTION_UI.SetActive(true);
}
//deactivate UI
void CloseUI()
{
Found = false;
open_option_button.SetActive(true);
close_option_button.SetActive(false);
OPTION_UI.SetActive(false);
}