· 編輯器插件使用類:
1. SessionState
描述
SessionState 是一種鍵/值存儲(chǔ),旨在存儲(chǔ)和檢索應(yīng)在重新加載程序集期間保留的 Editor 會(huì)話狀態(tài)。
退出 Unity 時(shí),系統(tǒng)將清除 SessionState 中存儲(chǔ)的狀態(tài)信息。用于存儲(chǔ)應(yīng)在使用 EditorPrefs 的 Unity Editor 會(huì)話中持久保留的狀態(tài)信息。
靜態(tài)函數(shù)
| 函數(shù) | 說(shuō)明 |
|---|---|
| EraseBool | 擦除鍵/值存儲(chǔ)中的布爾值條目。 |
| EraseFloat | 擦除鍵/值存儲(chǔ)中的浮點(diǎn)條目。 |
| Eraselnt | 擦除鍵/值存儲(chǔ)中的整數(shù)條目。 |
| EraselntArray | 擦除鍵/值存儲(chǔ)中的整數(shù)條目。 |
| EraseString | 擦除鍵/值存儲(chǔ)中的字符串條目。 |
| EraseVector3 | 擦除鍵/值存儲(chǔ)中的 Vector 3 條目。 |
| GetBool | 檢索布爾值。 |
| GetFloat | 檢索浮點(diǎn)值。 |
| Getlnt | 檢索整數(shù)值。 |
| GetIntArray | 檢索整數(shù)數(shù)組。 |
| GetString | 檢索字符串值。 |
| GetVector3 | 檢索 Vector3。 |
| SetBool | 存儲(chǔ)布爾值。 |
| SetFloat | 存儲(chǔ)浮點(diǎn)值 |
| SetInt | 存儲(chǔ)整數(shù)值。 |
| SetIntArray | 存儲(chǔ)整數(shù)數(shù)組。 |
| SetString | 存儲(chǔ)字符串值。 |
| SetVector3 | 存儲(chǔ)Vector3。 |
官方地址:https://docs.unity3d.com/cn/2020.3/ScriptReference/SessionState.html
2. EditorPrefs
描述
存儲(chǔ)和訪問(wèn) Unity 編輯器偏好設(shè)置。
在 macOS 上,EditorPrefs 存儲(chǔ)在~/Library/Preferences/com.unity3d.UnityEditor5.x.plist。
在 Windows 上,EditorPrefs 存儲(chǔ)在注冊(cè)表中,在 HKCU\Software\Unity Technologies\UnityEditor 5.x 鍵之下。
靜態(tài)函數(shù)
| 函數(shù) | 說(shuō)明 |
|---|---|
| DeleteAll | 從偏好中刪除所有鍵和值。請(qǐng)謹(jǐn)慎使用。 |
| DeleteKey | 從偏好中刪除key及其對(duì)應(yīng)值。 |
| GetBool | 返回偏好設(shè)置文件中與key對(duì)應(yīng)的值(如果存在)。 |
| GetFloat | 返回偏好設(shè)置文件中與key對(duì)應(yīng)的浮點(diǎn)值(如果存在)。 |
| Getlnt | 返回偏好設(shè)置文件中與key對(duì)應(yīng)的值(如果存在): |
| GetString | 返回偏好設(shè)置文件中與key對(duì)應(yīng)的值(如果存在)。 |
| HasKey | 如果偏好設(shè)置文件中存在/key/,則返回true. |
| SetBool | 設(shè)置由key標(biāo)識(shí)的偏好的值。 |
| SetFloat | 設(shè)置由key標(biāo)識(shí)的偏好設(shè)置的浮點(diǎn)值。 |
| SetInt | 將由鍵標(biāo)識(shí)的偏好設(shè)置的值設(shè)置為整數(shù)。 |
| SetString | 設(shè)置由key標(biāo)識(shí)的偏好設(shè)置的值。請(qǐng)注意,EditorPrefs不支持null字符串,而是存儲(chǔ)空字符串。 |
官方地址:https://docs.unity3d.com/cn/2020.3/ScriptReference/EditorPrefs.html
3. EditorUserSettings
描述
項(xiàng)目中可以共享數(shù)據(jù)的保存方法。在這里保存的數(shù)據(jù)都會(huì)被加密,很適合保存?zhèn)€人信息系或是密碼之類的東西。
使用這個(gè) API 保存的數(shù)據(jù)只會(huì)對(duì)自身的Project產(chǎn)生影響。
以 value 加密的形式,保存在項(xiàng)目?jī)?nèi)的文件中。
以 Unity2021 為例,數(shù)據(jù)保存在項(xiàng)目的 UserSettings 目錄下的 EditorUserSettings.asset 。(txt格式)
舊版本存儲(chǔ)在 Library的EditorUserSettings.asset 中。(二進(jìn)制格式)
靜態(tài)函數(shù)
| 函數(shù) | 說(shuō)明 |
|---|---|
| SetConfigValue | 為給定鍵標(biāo)識(shí)的項(xiàng)設(shè)置單個(gè)字符串值。 |
| GetConfigValue | 返回 EditorUserSettings.asset 文件中與 key 對(duì)應(yīng)的值(如果存在)。 |
· 游戲內(nèi)部?jī)?chǔ)存數(shù)據(jù)類
1. PlayerPrefs
描述
PlayerPrefs 是一個(gè)存儲(chǔ)游戲會(huì)話之間玩家偏好的類。 它可以將字符串、浮點(diǎn)數(shù)和整數(shù)值存儲(chǔ)到用戶的平臺(tái)注冊(cè)表中。
Unity 根據(jù)應(yīng)用程序運(yùn)行的操作系統(tǒng)以不同方式存儲(chǔ) PlayerPrefs 數(shù)據(jù)。 本頁(yè)給出的文件路徑中,公司名稱和產(chǎn)品名稱是您在 Unity 的 Player Settings 中設(shè)置的名稱。
獨(dú)立設(shè)備上的儲(chǔ)存位置
- macOS:
PlayerPrefs存儲(chǔ)在~/Library/Preferences/com.ExampleCompanyName.ExampleProductName.plist中。 Unity 對(duì)編輯器和獨(dú)立設(shè)備上的項(xiàng)目使用相同的 .plist 文件。 - Windows:
PlayerPrefs存儲(chǔ)在HKCU\Software\公司名稱\產(chǎn)品名稱鍵中。 - Linux:
PlayerPrefs存儲(chǔ)在~/.config/unity3d/公司名稱/產(chǎn)品名稱中。 - Windows 應(yīng)用商店應(yīng)用程序:
PlayerPrefs存儲(chǔ)在%userprofile%\AppData\Local\Packages\[產(chǎn)品包編號(hào)]\LocalState\playerprefs.dat中。 - Windows Phone 8:Unity 將
PlayerPrefs數(shù)據(jù)存儲(chǔ)在應(yīng)用程序的本地文件夾中。有關(guān)詳細(xì)信息,請(qǐng)參閱Directory.localFolder。 - Android:
PlayerPrefs存儲(chǔ)在/data/data/pkg-name/shared_prefs/pkg-name.v2.playerprefs.xml中。 Unity 將PlayerPrefs數(shù)據(jù)存儲(chǔ)在設(shè)備上的SharedPreferences中。 C#、JavaScript、Android Java 和本機(jī)代碼都可以訪問(wèn) PlayerPrefs 數(shù)據(jù)。 - WebGL:Unity 使用瀏覽器的
IndexedDB API存儲(chǔ)PlayerPrefs數(shù)據(jù)。有關(guān)更多信息,請(qǐng)參閱IndexedDB。
編輯器內(nèi)播放模式存儲(chǔ)位置
- macOS:
PlayerPrefs存儲(chǔ)在/Library/Preferences/[bundle identifier].plist中。 - Windows 上,
PlayerPrefs存儲(chǔ)在HKCU\Software\Unity\UnityEditor\ExampleCompanyName\ExampleProductName鍵中。 Windows 10 使用應(yīng)用程序的PlayerPrefs名稱。 例如,Unity 添加一個(gè)DeckBase字符串并將其轉(zhuǎn)換為DeckBase_h3232628825。 應(yīng)用程序忽略擴(kuò)展名。
Unity 將 PlayerPrefs 存儲(chǔ)在本地注冊(cè)表中,沒(méi)有加密。 不要使用 PlayerPrefs 數(shù)據(jù)來(lái)存儲(chǔ)敏感數(shù)據(jù)。
靜態(tài)函數(shù)
| 函數(shù) | 說(shuō)明 |
|---|---|
| DeleteAll | 從偏好中刪除所有鍵和值。請(qǐng)謹(jǐn)慎使用。 |
| DeleteKey | 從 PlayerPrefs 中刪除給定的鍵。 如果鍵不存在,DeleteKey 沒(méi)有影響。 |
| GetFloat | 返回偏好設(shè)置文件中與 key 對(duì)應(yīng)的值(如果存在)。 |
| GetInt | 返回偏好設(shè)置文件中與 key 對(duì)應(yīng)的值(如果存在)。 |
| GetString | 返回偏好設(shè)置文件中與 key 對(duì)應(yīng)的值(如果存在)。 |
| HasKey | 如果 PlayerPrefs 中存在給定鍵,則返回 true,否則返回 false。 |
| Save | 將所有修改的偏好寫(xiě)入磁盤(pán)。 |
| SetFloat | 為給定鍵標(biāo)識(shí)的首選項(xiàng)設(shè)置單個(gè)浮點(diǎn)值。 您可以使用 PlayerPrefs.GetFloat 來(lái)檢索此值。 |
| SetInt | 為給定鍵標(biāo)識(shí)的首選項(xiàng)設(shè)置單個(gè)整數(shù)值。 您可以使用 PlayerPrefs.GetInt 來(lái)檢索此值。 |
| SetString | 為給定鍵標(biāo)識(shí)的首選項(xiàng)設(shè)置單個(gè)字符串值。 您可以使用 PlayerPrefs.GetString 來(lái)檢索此值。 |
官方地址:https://docs.unity3d.com/cn/2020.1/ScriptReference/PlayerPrefs.html
2.BinaryFormatter 二進(jìn)制序列化
假設(shè)有一個(gè)Player類
[System. Serializable]
public class Player
{
public int health;
public int power;
public Vector3 position;
}
由于BinaryFormatter序列化不支持Unity的Vector3類型,所以我們需要做一下包裝。
public class PlayerData{
public int level;
public int health;
public float[] position;
public PlayerData(Player player)
{
this.level = player.level;
this.health = player.health;
this.position = new float[3];
this.position[0] = player.transform.position.x;
this.position[1] = player.transform.position.y;
this.position[2] = player.transform.position.z;
}
}
我們對(duì)PlayerData進(jìn)行保存和讀取。讀取出來(lái)的PlayerData可以賦給Player。
public static class SaveSystem{
//保存數(shù)據(jù)
public static void SavePlayer(Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath+"/player.fun";
FileStream stream = new FileStream(path,FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream,data);
stream.Close();
}
//讀取數(shù)據(jù)
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath+"/player.fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path,FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}else{
Debug.LogError("找不到保存文件 "+path);
return null;
}
}
}
3. 保存為json格式的文本文件
使用 Unity 自身API JsonUtility。
保存數(shù)據(jù)
public static void SavePlayerJson(Player player)
{
string path = Application.persistentDataPath+"/player.json";
var content = JsonUtility.ToJson(player,true);
File.WriteAllText(path,content);
}
讀取數(shù)據(jù)
public static PlayerData LoadPlayerJson()
{
string path = Application.persistentDataPath+"/player.json";
if(File.Exists(path)){
var content = File.ReadAllText(path);
var playerData = JsonUtility.FromJson<PlayerData>(content);
return playerData;
}else{
Debug.LogError("找不到保存文件 "+path);
return null;
}
}
4. XmlSerializer進(jìn)行序列化
假如有類
public class Entity
{
public Entity()
{
}
public Entity(string c, string f)
{
name = c;
school = f;
}
public string name;
public string school;
}
讀取數(shù)據(jù)
List<Entity> entityList=null;
XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
using (StreamReader sr = new StreamReader(configPath))
{
entityList = xs.Deserialize(sr) as List<Entity>;
}
保存數(shù)據(jù)
List<Entity> entityList=null;
XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));
using (StreamWriter sw = File.CreateText(configPath))
{
xs.Serialize(sw, entityList);
}
對(duì)應(yīng)的xml文件為:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEntity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity>
<Name>Alice</Name>
<School>SJTU</School>
</Entity>
<Entity>
<Name>Cici</Name>
<School>CSU</School>
</Entity>
<Entity>
<Name>Zero</Name>
<School>HIT</School>
</Entity>
</ArrayOfEntity>
5.TextAsset(普通文本讀取)
TextAsset text=(TextAsset)Resources.Load("unity3d");
Debug.Log(text.text);
在Project窗口的根目錄創(chuàng)建Resources文件夾,然后把名字為unity3d.txt的文件夾的文件放在Resources文件夾下就可以讀取到。