PlayerPrefs学习

来源:互联网 发布:淘宝商品id 编辑:程序博客网 时间:2024/05/18 00:49

1、创建工程,添加两个场景scene0和scene1

2、创建两个C#文件

//Scene0Main.csusing UnityEngine;using System.Collections;public class Scene0Main : MonoBehaviour {public string testStr;public string testInt;public string testFloat;//public GUISkin fontSkin;// Use this for initializationvoid Start () {  testStr = PlayerPrefs.GetString( "testStr", "default");testInt = PlayerPrefs.GetInt("testInt", 0).ToString();testFloat = PlayerPrefs.GetFloat ("testFloat", 0).ToString (); }// Update is called once per framevoid Update () {}void OnGUI(){testStr = GUI.TextField (new Rect(10,200,200,50), testStr, 50);testInt = GUI.TextField (new Rect (10, 250, 200, 50), testInt, 50);testFloat = GUI.TextField( new Rect(10, 300, 200, 50), testFloat, 50);if (GUI.Button(new Rect(220, 200, 150, 100), "commit all"))   {   PlayerPrefs.SetString("testStr", testStr);   PlayerPrefs.SetInt("testInt", int.Parse(testInt));   PlayerPrefs.SetFloat("testFloat", float.Parse(testFloat));      Application.LoadLevel("scene1");   }   }}

//Scene1Mainusing UnityEngine;using System.Collections;public class Scene1Main : MonoBehaviour {public string testStr;   public string testInt;   public string testFloat;   // Use this for initializationvoid Start () {testStr = PlayerPrefs.GetString("testStr", "default");   testInt = PlayerPrefs.GetInt("testInt", 0).ToString();   testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();   }// Update is called once per framevoid Update () {}void OnGUI(){GUI.Label(new Rect(10,150,300,50),"testStr = "+ testStr);   GUI.Label(new Rect(10,200,300,50),"testInt = "+ testInt);   GUI.Label(new Rect(10,250,300,50),"testFloat = "+ testFloat);   if (GUI.Button(new Rect(220, 200, 150, 100), "clean all"))   {   PlayerPrefs.DeleteAll();   Application.LoadLevel("scene0");   }   if (GUI.Button(new Rect(220, 320, 150, 100), "only return"))   {   Application.LoadLevel("scene0");   }   }}

分别将两个脚本挂在对于场景的摄像机上,在File->Build Setting中,添加scene0和scene1两个场景,运行scene0。

0 0
原创粉丝点击