欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity3D游戲開(kāi)發(fā)數(shù)據(jù)持久化PlayerPrefs的用法詳解

 更新時(shí)間:2019年08月20日 08:28:22   投稿:laozhang  
在本篇文章里小編給大家整理了關(guān)于Unity3D游戲開(kāi)發(fā)之?dāng)?shù)據(jù)持久化PlayerPrefs的使用的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。

小編今天研究了在Unity3D中的數(shù)據(jù)持久化問(wèn)題。數(shù)據(jù)持久化在任何一個(gè)開(kāi)發(fā)領(lǐng)域都是一個(gè)值得關(guān)注的問(wèn)題,小到一個(gè)應(yīng)用中配置文件的讀寫(xiě),大到數(shù)據(jù)庫(kù)的管理維護(hù),都可以看到數(shù)據(jù)持久化的身影。小編在《C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB》這篇文章中曾介紹了博主在寒假期間開(kāi)發(fā)的Xml4DB框架,這是一個(gè)基于Xml的輕量級(jí)數(shù)據(jù)持久化框架,可以采用面向?qū)ο蟮姆绞絹?lái)處理數(shù)據(jù)。數(shù)據(jù)持久化從某種意義上來(lái)說(shuō),就是序列化和反序列化化的過(guò)程。在.NET中我們可以將對(duì)象序列化為Xml、Json、二進(jìn)制。然后通過(guò)反序列化重新獲得對(duì)象。同樣,在Android中我們可以通過(guò)使用Preferences來(lái)存儲(chǔ)鍵值型數(shù)據(jù)來(lái)實(shí)現(xiàn)數(shù)據(jù)持久化(當(dāng)然還有其它的方式,這里只是為了強(qiáng)調(diào)鍵值型數(shù)據(jù))。那么,在Unity3D中如何實(shí)現(xiàn)數(shù)據(jù)持久化呢?請(qǐng)大家跟隨我一起走進(jìn)今天的文章:[Unity3D]Unity3D游戲開(kāi)發(fā)之?dāng)?shù)據(jù)持久化PlayerPrefs的使用

首先我們來(lái)看兩段Unity3D中實(shí)現(xiàn)數(shù)據(jù)讀寫(xiě)的簡(jiǎn)單代碼吧:

 //保存數(shù)據(jù)
 PlayerPrefs.SetString("Name",mName);
 PlayerPrefs.SetInt("Age",mAge);
 PlayerPrefs.SetFloat("Grade",mGrade)
 //讀取數(shù)據(jù)
 mName=PlayerPrefs.GetString("Name","DefaultValue");
 mAge=PlayerPrefs.GetInt("Age",0);
 mGrade=PlayerPrefs.GetFloat("Grade",0F);

通過(guò)上面兩段代碼,我們可以發(fā)現(xiàn)兩點(diǎn):

1、Unity3D中的數(shù)據(jù)持久化是以鍵值的形式存儲(chǔ)的,可以看作是一個(gè)字典。

2、Unity3D中值是通過(guò)鍵名來(lái)讀取的,當(dāng)值不存在時(shí),返回默認(rèn)值。

目前,在Unity3D中只支持int、string、float三種數(shù)據(jù)類型的讀取,所以我們可以使用這三種數(shù)據(jù)類型來(lái)存儲(chǔ)簡(jiǎn)單的數(shù)據(jù)。目前Unity3D中用于數(shù)據(jù)持久化的類為layerPrefs,主要的類方法有:

 static function DeleteAll(): void
 描述:從設(shè)置文件中移除所有鍵和值,謹(jǐn)慎的使用它們。
 
 static function DeleteKey(key: string): void
 描述:從設(shè)置文件中移除key和它對(duì)應(yīng)的值。
 
 static function GetFloat(key: string, defaultValue: float=OF): float
 描述:如果存在,返回設(shè)置文件中key對(duì)應(yīng)的值.如果不存在,它將返回defaultValue。
 
 static function GetInt(key: string, defaultValue: int): int
 描述:返回設(shè)置文件中key對(duì)應(yīng)的值,如果存在.如果不存在,它將返回defaultValue。
 
 static function GetString(key: string, defaultValue: string=**): string
 描述:返回設(shè)置文件中key對(duì)應(yīng)的值,如果存在.如果不存在,它將返回defaultValue.
 
 static function HasKey(key: string): bool
 描述:在設(shè)置文件如果存在key則返回真.
 
 static function SetFloat(key: string, value: float): void
 描述:設(shè)置由key確定的值.
 
 static function SetInt(key: string, value: int): void
 描述:設(shè)置由key確定的值.
 
 static function SetString(key: string, value: string): void
 描述:設(shè)置由key確定的值.

好了,在了解layerPrefs的主要方法后,我們以一個(gè)具體的例子來(lái)學(xué)習(xí)Unity3D中數(shù)據(jù)持久化的實(shí)現(xiàn),我們希望實(shí)現(xiàn)在一個(gè)場(chǎng)景中輸入信息以便在新場(chǎng)景中讀取信息。我們直接創(chuàng)建兩個(gè)場(chǎng)景,分別命名為Scene0、Scene1(據(jù)說(shuō)程序員數(shù)數(shù)都是從0開(kāi)始的,哈哈),場(chǎng)景中保留主攝像機(jī)即可。接下來(lái)我們分別為兩個(gè)場(chǎng)景編寫(xiě)腳本:

第一個(gè)場(chǎng)景的腳本:

using UnityEngine;
using System.Collections;
 
public class Scene1Script : MonoBehaviour {
	
	//姓名
	private string mName="路人甲";
	//年齡
	private int mAge=20;
	//成績(jī)
	private float mGrade=75.5F;
	
	void OnGUI()
	{
		GUILayout.Label("Unity3D數(shù)據(jù)存儲(chǔ)示例程序",GUILayout.Height(25));
		//姓名
		GUILayout.Label("請(qǐng)輸入姓名:",GUILayout.Height(25));
	 mName=GUILayout.TextField(mName,GUILayout.Height(25));
		//年齡
		GUILayout.Label("請(qǐng)輸入年齡:",GUILayout.Height(25));
	 mAge=int.Parse(GUILayout.TextField(mAge.ToString(),GUILayout.Height(25)));
		//成績(jī)
		GUILayout.Label("請(qǐng)輸入成績(jī):",GUILayout.Height(25));
	 mGrade=float.Parse(GUILayout.TextField(mGrade.ToString(),GUILayout.Height(25)));
		
		//提交數(shù)據(jù)
		if(GUILayout.Button("提交數(shù)據(jù)",GUILayout.Height(25)))
		{
		 //保存數(shù)據(jù)
		 PlayerPrefs.SetString("Name",mName);
		 PlayerPrefs.SetInt("Age",mAge);
		 PlayerPrefs.SetFloat("Grade",mGrade);
		 
		 //切換到新場(chǎng)景
			Application.LoadLevel("Scene1");
		}
	}
}

第二個(gè)場(chǎng)景的腳本:

using UnityEngine;
using System.Collections;
 
public class Scene2Script : MonoBehaviour {
 
	private string mName;
	private int mAge;
	private float mGrade;
	
	void Start () 
	{
	 //讀取數(shù)據(jù)
	 mName=PlayerPrefs.GetString("Name","DefaultValue");
	 mAge=PlayerPrefs.GetInt("Age",0);
	 mGrade=PlayerPrefs.GetFloat("Grade",0F);
	}
	
	void OnGUI()
	{
	 GUILayout.Label("Unity3D數(shù)據(jù)存儲(chǔ)示例程序",GUILayout.Height(25));
	 //姓名
	 GUILayout.Label("姓名:"+mName,GUILayout.Height(25));
	 //年齡
	 GUILayout.Label("年齡:"+mAge,GUILayout.Height(25));
	 //成績(jī)
	 GUILayout.Label("成績(jī):"+mGrade,GUILayout.Height(25));
		
	 //刪除數(shù)據(jù)
	 if(GUILayout.Button("清除數(shù)據(jù)",GUILayout.Height(25)))
	 {
   PlayerPrefs.DeleteAll();
	 }
		
	 //返回Scene0
	 if(GUILayout.Button("返回場(chǎng)景",GUILayout.Height(25)))
	 {
   Application.LoadLevel("Scene0");
	 }
		
	}
}

我們這里直接將腳本綁定到攝像機(jī)上,然后將項(xiàng)目編譯,注意將兩個(gè)場(chǎng)景放入編譯序列,我們運(yùn)行程序:

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論