- Home /
How to create log by PlayerPrefs?
I have dice roller app. When dice is stoped i detect the value like this:
IEnumerator Counter(){
yield return new WaitForSeconds (0.1f);
if (speed > 0) {
check = true;
gui.fontSize = -50000;
} else {
gui.fontSize = 30;
}
if (speed == 0) {
float maxY = 0f;
Transform upFace = null;
foreach (Transform face in faces) {
if (face.position.y > maxY) {
maxY = face.position.y;
upFace = face;
}
}
if (check == true) {
//Debug.Log (upFace.name);
result = upFace.name;
gui.text = "You are rolled: " + result;
PlayerPrefs.SetString ("DiceResult",Time.time +" You rolled " +result);
consoleResult = PlayerPrefs.GetString ("DiceResult");
Debug.Log (consoleResult);
check = false;
yield return new WaitForSeconds (1f);
}
}
}
But now it`s not a log. I just overwrite the value after each roll. I want create a log. After each roll the value must been save in PlayerPrefs and if you want you can see all of your result.
Question 1: How create log by PlayerPrefs?
Question 2: How output the values? ( Many empty GUI.Text components?)
PlayerPrefs work like a Dictionary<String,String>(can hold int and float but they can be converted to string), for this reason if you don't want overwrite a value you need use a diferent key, but PlayerPrefs is used to hold simple data, to save complex data you need use serialization.
Answer by TonyLi · May 19, 2017 at 06:59 PM
Create a Unity UI Text element inside a Scroll View. (Watch Unity's UI Tutorials if you don't know how to do this.)
Each time you roll, add the result to the Text element's text variable:
public UnityEngine.UI.Text rollsText; // ASSIGN IN INSPECTOR.
IEnumerator Counter() {
...
if (check == true) {
...
rollsText.text += "\nYou rolled " + result;
yield return new WaitForSeconds(1f);
}
}