zoukankan      html  css  js  c++  java
  • 【转】 [Unity3D]手机3D游戏开发:场景切换与数据存储(PlayerPrefs 类的介绍与使用)

    http://blog.csdn.net/pleasecallmewhy/article/details/8543181

    在Unity中的数据存储和iOS中字典的存储基本相同,是通过关键字实现数据存储与调用。

    下面来介绍一下Unity用来存储数据的PlayerPrefs 类。

    使用PlayerPrefs可以在在游戏会话中保持并访问玩家偏好设置。

    在Mac OS X上PlayerPrefs存储在-/Library/PlayerPrefs文件夹,

    名文unity/[companyname][product name].plist,这里company和product是在Project Setting中设置的,相同

    的plist用于在编辑器中运行的工程和独立模式.

    在Windows独立模式下,PlayerPrefs被存储在注册表的HKCU Software[companyname][product name]键下,这里company和product是在Project Setting中设置的.

    在Web模式下,PlayerPrefs存储在Mac OS X的二进制文件-/Library/Preferences/Unity/WebPlayerPrefs中和Windows的%APPDATA%UnityWebPlayerPrefs中,一个偏好设置文件对应一个web播放器URL并且文件大小被限制为1兆。如果超出这个限制,SetInt,SetFloat和SetString将不会存储值并相处一个PlayerPrefsException.

    类方法

    ◆ static function DeleteAll(): void
    描述:从设置文件中移除所有键和值,谨慎的使用它们。

    ◆ static function DeleteKey(key: string): void
    描述:从设置文件中移除key和它对应的值。

    ◆ static function GetFloat(key: string, defaultValue: float=OF): float
    描述:如果存在,返回设置文件中key对应的值.如果不存在,它将返回defaultValue。

    print(PlayerPrefs.GetFlat("Player score"));

    ◆ static function GetInt(key: string, defaultValue: int): int
    描述:返回设置文件中key对应的值,如果存在.如果不存在,它将返回defaultValue。
    print(PlayerPrefs.GetInt("Player score"));

    ◆ static function GetString(key: string, defaultValue: string=**): string
    描述:返回设置文件中key对应的值,如果存在.如果不存在,它将返回defaultValue.
    print(PlayerPrefs.GetString("Player Name"));

    ◆ static function HasKey(key: string): bool
    描述:在设置文件如果存在key则返回真.

    ◆ static function SetFloat(key: string, value: float): void
    描述:设置由key确定的值.
    print(PlayerPrefs.SetFloat("Player Score", 10.0));

    ◆ static function SetInt(key: string, value: int): void
    描述:设置由key确定的值.
    PlayerPrefs.SetInt("Player Score", 10);

    ◆ static function SetString(key: string, value: string): void
    描述:设置由key确定的值.
    PlayerPrefs.Setstring("Player Name", "Foobar");

     

    下面通过一个案例简单的演示一下。

    首先创建两个场景用来实现场景的跳转,命名为CSDNPrefabs1和CSDNPrefabs2来做实验。

    基本需求是在第一个场景中创建一些数据并且跳转到第二个场景中显示出来。

    创建一个Scene1.cs脚本:

     

    [csharp] view plain copy
     
    1. using UnityEngine;  
    2. using System.Collections;  
    3.   
    4. public class Scene0Main : MonoBehaviour {  
    5.   
    6.     //储存数据的显示  
    7.     public string testStr;  
    8.     public string testInt;  
    9.     public string testFloat;  
    10.       
    11.     //GUI皮肤 为上面我们添加的皮肤  
    12.     //在外面用鼠标拖动上为它赋值  
    13.     public GUISkin fontSkin;  
    14.     //显示的图片  
    15.     public Texture Imagetexture;  
    16.        
    17.     // Use this for initialization  
    18.     void Start () {  
    19.         //读取key的值  
    20.         testStr = PlayerPrefs.GetString("testStr", "default");  
    21.         testInt = PlayerPrefs.GetInt("testInt", 0).ToString();  
    22.         testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();  
    23.           
    24.     }  
    25.       
    26.     // Update is called once per frame  
    27.     void Update () {  
    28.       
    29.     }  
    30.       
    31.       
    32.     void OnGUI() {  
    33.           
    34.         //将GUI的皮肤设置为我们创建的皮肤  
    35.         GUI.skin = fontSkin;  
    36.           
    37.         //贴上图片  
    38.         GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture);  
    39.           
    40.         //添加输入框让用户输入信息,这里面我没有捕获异常,因为用户有可能输入一个不合法的数值  
    41.         testStr = GUI.TextField (new Rect(10, 200, 200, 50), testStr, 50);  
    42.         testInt = GUI.TextField (new Rect(10, 250, 200, 50), testInt, 50);  
    43.         testFloat = GUI.TextField (new Rect(10, 300, 200, 50), testFloat, 50);  
    44.           
    45.         //点击按钮保存所有数据  
    46.         if (GUI.Button(new Rect(220, 200, 150, 100), "commit all"))  
    47.         {  
    48.               
    49.             PlayerPrefs.SetString("testStr", testStr);  
    50.             PlayerPrefs.SetInt("testInt", int.Parse(testInt));  
    51.             PlayerPrefs.SetFloat("testFloat", float.Parse(testFloat));  
    52.             //切换场景到scene1  
    53.             Application.LoadLevel("scene1");  
    54.         }  
    55.     }  
    56.       
    57.       
    58. }  


    创建一个Scene2.cs脚本:

     

     

    [csharp] view plain copy
     
    1. using UnityEngine;    
    2. using System.Collections;    
    3.     
    4. public class scene1Main : MonoBehaviour {    
    5.     
    6.     public string testStr;    
    7.     public string testInt;    
    8.     public string testFloat;    
    9.         
    10.     public GUISkin fontSkin;    
    11.     public Texture Imagetexture;    
    12.          
    13.     // Use this for initialization    
    14.     void Start () {    
    15.         testStr = PlayerPrefs.GetString("testStr", "default");    
    16.         testInt = PlayerPrefs.GetInt("testInt", 0).ToString();    
    17.         testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();    
    18.             
    19.     }    
    20.         
    21.         
    22.     void OnGUI() {    
    23.         GUI.skin = fontSkin;    
    24.             
    25.         GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture);    
    26.             
    27.         //显示label    
    28.         GUI.Label(new Rect(10,150,300,50),"testStr = "+ testStr);    
    29.         GUI.Label(new Rect(10,200,300,50),"testInt = "+ testInt);    
    30.         GUI.Label(new Rect(10,250,300,50),"testFloat = "+ testFloat);    
    31.             
    32.         if (GUI.Button(new Rect(220, 200, 150, 100), "clean all"))    
    33.         {    
    34.             //删除所有键值    
    35.             PlayerPrefs.DeleteAll();    
    36.             // 返回场景0    
    37.             Application.LoadLevel("scene0");    
    38.         }    
    39.             
    40.         if (GUI.Button(new Rect(220, 320, 150, 100), "only return"))    
    41.         {    
    42.             // 返回场景0    
    43.             Application.LoadLevel("scene0");    
    44.         }    
    45.     }    
    46. }    


    然后添加相关贴图的GUISkin完成部署:

     

    此时运行会报错,因为这两个场景需要Build:

    这是再运行就可以看到这个简单的Demo了。

    在这里输入完毕之后点击commit all:

    在另一个场景中便也能显示了。

  • 相关阅读:
    7、shell函数
    5、shell分支
    6、shell循环
    4、shell中的test命令
    3、shell中引号
    2、shell变量
    1、建立和运行shell
    awk命令简介
    18、异步IO
    Python模块:sys
  • 原文地址:https://www.cnblogs.com/mimime/p/6800712.html
Copyright © 2011-2022 走看看