Saltar al contenido

ejemplo de código playerpref unity

Siéntete en la libertad de divulgar nuestro espacio y códigos en tus redes sociales, ayúdanos a ampliar esta comunidad.

Ejemplo 1: cómo usar las preferencias del jugador en la unidad

SAVING
 
 PlayerPrefs.SetInt("score",5);
 PlayerPrefs.SetFloat("volume",0.6f);
 PlayerPrefs.SetString("username","John Doe");
 PlayerPrefs.Save();// If you need boolean value:bool val =true;
 PlayerPrefs.SetInt("PropName", val ?1:0);
 PlayerPrefs.Save();

 READING

 int score = PlayerPrefs.GetInt("score");float volume = PlayerPrefs.GetFloat("volume");string player = PlayerPrefs.GetString("username");bool val = PlayerPrefs.GetInt("PropName")==1?true:false;

Ejemplo 2: playerprefs unity

//Variables of different typeint number =18;floatdecimal=1.84f;string word ="Jeff";//Setting each PlayerPref to a Variable, with .Set and the type of Variable its set too (.SetFloat)
	PlayerPrefs.SetInt("Age", number);
	PlayerPrefs.SetFloat("Height",decimal);
	PlayerPrefs.SetString("Name", word);//Displaying the PlayerPref in Console, with .Get and its type of data (.GetString)
	Debug.Log(PlayerPrefs.GetInt("Age"));
	Debug.Log(PlayerPrefs.GetFloat("Height",1.5));// Adding a default Value incase the PlayerPref isn't storing any data
	Debug.Log(PlayerPrefs.GetString("Name"));/*Desplayed in Console:
	18
	1.84
	Jeff
*///Reseting data in one PlayerPref
	PlayerPrefs.DeleteKey("Height");//Reseting data in all PlayerPrefsP	layerPrefs.DeleteAll();

Ejemplo 3: unit playerprefs

//Variables of different typeint number =18;floatdecimal=1.84f;string word ="Jeff";//Setting each PlayerPref to a Variable, with .Set and the type of Variable its set too (.SetFloat)
	PlayerPrefs.SetInt("Age", number);
	PlayerPrefs.SetFloat("Height",decimal);
	PlayerPrefs.SetString("Name", word);//Displaying the PlayerPref in Console, with .Get and its type of data (.GetString)
	Debug.Log(PlayerPrefs.GetInt("Age"));
	Debug.Log(PlayerPrefs.GetFloat("Height",1.5));// Adding a default Value incase the PlayerPref isn't storing any data
	Debug.Log(PlayerPrefs.GetString("Name"));/*Desplayed in Console:
	18
	1.84
	Jeff
*///Reseting data in one PlayerPref
	PlayerPrefs.DeleteKey("Height");//Reseting data in all PlayerPrefsP	layerPrefs.DeleteAll();

Comentarios y valoraciones del artículo

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *