//得到存储的数据 Grade = PlayerPrefs.GetFloat("mGrade", 0.0f).ToString(); //获取浮点型 Age = PlayerPrefs.GetInt("mAge", 0).ToString(); //获取整型 Name = PlayerPrefs.GetString("mName", ""); //获取字符串 //储存数据 PlayerPrefs.SetString("mName", Name); //设置字符串 PlayerPrefs.SetInt("mAge", int.Parse(Age)); //设置整型 PlayerPrefs.SetFloat("mGrade", float.Parse(Grade)); //设置浮点型
using UnityEngine; using System.Collections; public class Scene1 : MonoBehaviour { //储存持久化数据显示 //可以保存3种类型,浮点型,整型,字符串型 public string Grade; public string Age; public string Name; //图片贴图 public Texture image; void Start() { //得到存储的数据 Grade = PlayerPrefs.GetFloat("mGrade", 0.0f).ToString(); Age = PlayerPrefs.GetInt("mAge", 0).ToString(); Name = PlayerPrefs.GetString("mName", ""); } void OnGUI() { //一张图片贴图 GUI.DrawTexture(new Rect((Screen.width - image.width) / 2, 20, 120, 120), image); //输入姓名 GUILayout.Label("----请输入姓名-----", GUILayout.Height(25)); Name = GUILayout.TextField(Name, GUILayout.Height(25)); //输入年龄 GUILayout.Label("----请输入年龄-----", GUILayout.Height(25)); Age = GUILayout.TextField(Age, GUILayout.Height(25)); //输入成绩 GUILayout.Label("----请输入成绩-----", GUILayout.Height(25)); Grade = GUILayout.TextField(Grade, GUILayout.Height(25)); //提交并跳转至下一个场景 if (GUILayout.Button("提交",GUILayout.Height(25))) { //储存数据 PlayerPrefs.SetString("mName", Name); PlayerPrefs.SetInt("mAge", int.Parse(Age)); PlayerPrefs.SetFloat("mGrade", float.Parse(Grade)); //跳转到场景2 Application.LoadLevel("scene2"); } } }
using UnityEngine; using System.Collections; public class Scene2 : MonoBehaviour { //储存持久化数据显示 //可以保存3种类型,浮点型,整型,字符串型 public string Grade; public string Age; public string Name; //图片贴图 public Texture image; void Start() { //得到存储的数据 Grade = PlayerPrefs.GetFloat("mGrade", 0.0f).ToString(); Age = PlayerPrefs.GetInt("mAge", 0).ToString(); Name = PlayerPrefs.GetString("mName", ""); } void OnGUI() { //一张图片贴图 GUI.DrawTexture(new Rect((Screen.width - image.width) / 2, 20, 120, 120), image); //显示名字 GUILayout.Label("姓名: " + Name, GUILayout.Height(25)); //显示年龄 GUILayout.Label("年龄: " + Age, GUILayout.Height(25)); //显示成绩 GUILayout.Label("成绩: " + Grade, GUILayout.Height(25)); //清除数据 if (GUILayout.Button("清除数据", GUILayout.Height(25))) { PlayerPrefs.DeleteAll(); } if (GUILayout.Button("返回上一页", GUILayout.Height(25))) { //返回场景1 Application.LoadLevel("scene1"); } } }
【Unity3D游戏开发】—— PlayerPrefs类实现本地持久化数据存储,布布扣,bubuko.com
【Unity3D游戏开发】—— PlayerPrefs类实现本地持久化数据存储
原文地址:http://blog.csdn.net/wxk_bigworld/article/details/25723697