PlayerPrefs存儲數(shù)據(jù)
可理解為可持久化存儲,還可以理解為游戲存檔,玩RPG游戲的時候肯定有游戲游戲存檔,存檔中記錄玩家以前游戲的過程,這些都是以數(shù)據(jù)的形式存在PlayerPrefs中。
On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder,in a dile named untiy.[company name].[product name].plist,where copmpany and product name are the names set up in project Settings.The same.plist file id used for both projects run in the Editor and standalone players.
在Mac OS X上存儲在~/Library/PlayerPrefs文件夾,名為unity.[company name].[product name].plist,這里company和product名是在project Setting中設置的,相同的plist用于在編輯器中運行的工程和獨立模式。
On Windows standalone plauers ,playerPrefs are stored in the registry under HKCU\Software\[compang name]\[product name]key,where company and product names are the names set up in project Settings;
在windows獨立模式下,playerPrefs被存儲在注冊的HKCU\Software\[company name]\[product name]鍵下,這里company和product名是在project setting中設置的。
On Web Players,Playerprefs are stored in binary files unfer~/Library/Prefaerences/untiy/webPlayerprefs on Mac Os X and %APPDATA%\Untiy\WebPayerPrefs on Windows. There is one Preference file per Web Player URL and the file size is limited to 1 megabute.If this limit would be exceeded, SetInt,SetFloat and SetString will not store the value and threow a PlayerPrefsException.
在web模式,PlayerPrefs存儲在Mac OS ?X的二進制文件~/Library/Preferences/Untiy/WebPlayerPrefs中和Windows的%APPARA%\Unity\WebPlayerPrefs中,一個游戲存檔文件對應一個web播放器URL并且文件大小被限制為1MB。如果超出這個限制,SeTInt、SetFloat和SetString將不會存儲值并拋出一個PlayerPrefsException。
On Linux PlayerPrefs are stored in ~/.config/Unity3d/[CompanyName]/[ProductName] folder.
在Linux上存儲在~/.config/Unity3d/[CompanyName]/[ProductName]文件中.
示例:存儲玩家姓名 年齡 成績(簡化)
通過UI界面中的InputFiled控件獲取Text值 輸出另一個場景中 用UGUI寫出
1.先引入兩個命名空間
using UnityEngin.UI;
using UnityEngine.SceneManagement;
2.定義公共類參數(shù) ?分別為name_ft,age_ft,score_ft;
public InputField name_ft;
public InputField age_ft;
public InputField score_ft;
3.寫一個button事件方法
public void StoreData()
{
//獲取數(shù)據(jù)
string name=name_ft.text;
string age=age_ft.text;
string score=score_ft.text;
//存儲數(shù)據(jù)
PlayerPrefs.SetString("Name",name);
PlayerPrefs.SetInt("age",int.Parse(age))
PlayerPrefs.SetFloat("Score",flaot.Parse(score));
//跳轉場景 顯示數(shù)據(jù)
SceneManager.LoadScene(1);
}
//場景2
//定義變量 用來接收數(shù)據(jù)
string unname;
int unage;
float unscore;
//使用OnGUI的方法
void OnGUI()
{
GUILayout.Label(unname);
GUILayout.Lable(unage.TOString());
GUILayout.Lable(unscore.ToString());
}
場景如圖1,2。


XML數(shù)據(jù)生成和解析
XML指看擴展標記語言(Extensible Markup Language)
XML設計宗旨是傳輸數(shù)據(jù),而非顯示數(shù)據(jù)
XML繼承結構
-System.Object
?-System.Xml.XmlNode(表示XML節(jié)點)
? ? +System.Xml.XmlDocument(表示XML文檔)
? ? +System.Xml.XmlAttribute(表示XML屬性)
? ? -System.Xml.XmlLinkedNode
? ? ? ?+System.Xml.XmlElement(表示XML元素)
注:上面的 + 相當于文件的未打開 -文件打開的后




JSON數(shù)據(jù)生成和解析
JOSN是純文本
JOSN是輕量級的數(shù)據(jù)交換
JOSN是具有層級結構(值中存在值)
JOSN語法
數(shù)據(jù)在鍵值對
數(shù)據(jù)由逗號分隔
花括號保存對象
方括號保存數(shù)組
使用前需要將 System.Json(便于json生成)和LitJson.dll(便于解析json)放入到Assets文件下
示例
//創(chuàng)建json對象,
JsonObject jsonTransform=new Jsonobject();
//創(chuàng)建一個json值對象,存儲一個value
JsonValue jsonPosition="10,20,30";
//json對象,添加一個key:value
jsontrasnform.Add("position",jsontransform);
//打印結果
Debug.Log(jsonTrasnform.ToString(0);
結果顯示;"position“:10,20,30
ListJson.JsonMapper
把對象轉化成JSON格式字符串:JsonMaooer.ToJson
把JSON格式字符串轉化成對象:JsonMapper.Toobject

這次就寫到這里。如果有錯留個消息給我就好,我會修改修改。