在Godot中,使用C#進行數據持久化可以通過以下幾種方法實現:
首先,安裝Newtonsoft.Json庫,用于處理JSON數據。在Godot項目的packages
文件夾中創建一個名為Newtonsoft.Json
的文件夾,然后將Newtonsoft.Json.dll
文件放入其中。接下來,可以使用以下代碼將數據保存到JSON文件并從JSON文件中加載數據:
using Newtonsoft.Json;
using System.IO;
public class DataPersistence
{
public static void SaveData<T>(string filePath, T data)
{
string json = JsonConvert.SerializeObject(data);
File.WriteAllText(filePath, json);
}
public static T LoadData<T>(string filePath)
{
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<T>(json);
}
return default(T);
}
}
使用示例:
// 保存數據
DataPersistence.SaveData("data.json", myData);
// 加載數據
MyDataType loadedData = DataPersistence.LoadData<MyDataType>("data.json");
首先,安裝System.Xml庫,用于處理XML數據。在Godot項目的packages
文件夾中創建一個名為System.Xml
的文件夾,然后將System.Xml.dll
文件放入其中。接下來,可以使用以下代碼將數據保存到XML文件并從XML文件中加載數據:
using System.Xml.Serialization;
using System.IO;
[XmlRoot("MyData")]
public class MyDataType
{
[XmlElement("Property1")]
public string Property1 { get; set; }
[XmlElement("Property2")]
public int Property2 { get; set; }
}
public class DataPersistence
{
public static void SaveData<T>(string filePath, T data)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StreamWriter writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, data);
}
}
public static T LoadData<T>(string filePath)
{
if (File.Exists(filePath))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StreamReader reader = new StreamReader(filePath))
{
return (T)serializer.Deserialize(reader);
}
}
return default(T);
}
}
使用示例:
// 保存數據
DataPersistence.SaveData("data.xml", myData);
// 加載數據
MyDataType loadedData = DataPersistence.LoadData<MyDataType>("data.xml");
首先,安裝SQLite庫,例如System.Data.SQLite
。接下來,可以使用以下代碼將數據保存到SQLite數據庫并從SQLite數據庫中加載數據:
using System.Data.SQLite;
public class DataPersistence
{
private static string dbPath = "data.db";
public static void InitializeDatabase()
{
using (SQLiteConnection connection = new SQLiteConnection(dbPath))
{
connection.Open();
string createTableQuery = "CREATE TABLE IF NOT EXISTS Data (Id INTEGER PRIMARY KEY AUTOINCREMENT, Property1 TEXT, Property2 INTEGER)";
using (SQLiteCommand command = new SQLiteCommand(createTableQuery, connection))
{
command.ExecuteNonQuery();
}
}
}
public static void SaveData(string property1, int property2)
{
using (SQLiteConnection connection = new SQLiteConnection(dbPath))
{
connection.Open();
string insertQuery = "INSERT INTO Data (Property1, Property2) VALUES (@Property1, @Property2)";
using (SQLiteCommand command = new SQLiteCommand(insertQuery, connection))
{
command.Parameters.AddWithValue("@Property1", property1);
command.Parameters.AddWithValue("@Property2", property2);
command.ExecuteNonQuery();
}
}
}
public static MyDataType LoadData()
{
using (SQLiteConnection connection = new SQLiteConnection(dbPath))
{
connection.Open();
string selectQuery = "SELECT * FROM Data";
using (SQLiteCommand command = new SQLiteCommand(selectQuery, connection))
{
using (SQLiteDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return new MyDataType
{
Property1 = reader["Property1"].ToString(),
Property2 = reader["Property2"].ToInt32()
};
}
}
}
}
return default(MyDataType);
}
}
使用示例:
// 初始化數據庫
DataPersistence.InitializeDatabase();
// 保存數據
DataPersistence.SaveData("Value1", 42);
// 加載數據
MyDataType loadedData = DataPersistence.LoadData();
這些方法可以根據項目需求選擇使用。JSON和XML文件適用于簡單的數據結構,而SQLite數據庫適用于更復雜的數據存儲需求。