Laman

Unity Save and Load Data

In Unity, to save and load data using PlayerPrefs

Static Functions

DeleteAllRemoves all keys and values from the preferences. Use with caution.
DeleteKeyRemoves key and its corresponding value from the preferences.
GetFloatReturns the value corresponding to key in the preference file if it exists.
GetIntReturns the value corresponding to key in the preference file if it exists.
GetStringReturns the value corresponding to key in the preference file if it exists.
HasKeyReturns true if key exists in the preferences.
SaveWrites all modified preferences to disk.
SetFloatSets the value of the preference identified by key.
SetIntSets the value of the preference identified by key.
SetStringSets the value of the preference identified by key.

Example Code:
using UnityEngine;
using System.Collections;

public class Teaser : MonoBehaviour {
 int rnd=0,data=0;
 
 // Use this for initialization
 void Start () {
  rnd= Random.Range(2, 6);
  data= PlayerPrefs.GetInt("Data");
  Debug.Log("Data Loaded "+data);
  Debug.Log("Random Number "+rnd.ToString());
  PlayerPrefs.SetInt("Data", rnd);
  Debug.Log("Data Saved "+rnd.ToString());
 }

}

No comments:

Post a Comment

Silahkan