1、创建工程,添加两个场景scene0和scene1
2、创建两个C#文件
//Scene0Main.cs
using UnityEngine;
using System.Collections;
public class Scene0Main : MonoBehaviour {
public string testStr;
public string testInt;
public string testFloat;
// public GUISkin fontSkin;
// Use this for initialization
void Start ()
{
testStr = PlayerPrefs.GetString( "testStr", "default");
testInt = PlayerPrefs.GetInt("testInt", 0).ToString();
testFloat = PlayerPrefs.GetFloat ("testFloat", 0).ToString ();
}
// Update is called once per frame
void 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");
}
}
}
//Scene1Main
using UnityEngine;
using System.Collections;
public class Scene1Main : MonoBehaviour {
public string testStr;
public string testInt;
public string testFloat;
// Use this for initialization
void Start ()
{
testStr = PlayerPrefs.GetString("testStr", "default");
testInt = PlayerPrefs.GetInt("testInt", 0).ToString();
testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();
}
// Update is called once per frame
void 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。