記錄多個(gè)物體的不同屬性江咳,主要是屬性不同意峭火,使用SQLite數(shù)據(jù)庫的話爱致,不同屬性的物體就需要建不同的表烤送,只能使用鍵值對(duì)類型來存儲(chǔ)。網(wǎng)上查了查糠悯,在unity本地可以使用的noSQL數(shù)據(jù)庫寥寥無幾(如果有朋友知道帮坚,可以推薦給我啊)互艾,而這個(gè)IBoxDB多平臺(tái)運(yùn)行试和,是NoSQL類型的,正好滿足要求纫普。給出官方網(wǎng)址鏈接阅悍。,還有這位朋友的注意事項(xiàng)局嘁。
代碼中注釋很清楚溉箕,就不啰嗦了。先是簡單的工具類
using UnityEngine;
using System.Collections;
using iBoxDB.LocalServer;
using System.Collections.Generic;
using System;
/// <summary>
/// 實(shí)現(xiàn)一個(gè)Save和Read方法
/// </summary>
public class MyIBoxDB
{
public const string TABLE_BED_SCENE = "TABLE_BED_SCENE";
private static MyIBoxDB _Instance;
private DB db = null;
private DB.AutoBox autoBox = null;//該對(duì)象可以進(jìn)行CRUD操作悦昵,使用完畢不需要釋放對(duì)象
private MyIBoxDB()
{
if (db == null)
{
DB.Root(Application.persistentDataPath);//數(shù)據(jù)庫存儲(chǔ)路徑
db = new DB(1);//數(shù)據(jù)庫地址肴茄,或者說ID
db.GetConfig().EnsureTable<BaseObject>(TABLE_BED_SCENE, "ObjectName(20)");//先建表后使用,并且表的主鍵為ObjectName,長度20
}
if (autoBox == null)
autoBox = db.Open();
}
public static MyIBoxDB GetInstance()
{
if (_Instance == null)
_Instance = new MyIBoxDB();
return _Instance;
}
/// <summary>
/// 刪除數(shù)據(jù)庫但指,IBoxDB中沒有直接刪除一個(gè)表的接口寡痰,所以這個(gè)方法顯得格外親切~
/// 注意:刪除數(shù)據(jù)庫之前要關(guān)閉該數(shù)據(jù)庫
/// </summary>
/// <param name="address">數(shù)據(jù)庫地址</param>
public void DeleteDataBase(int address)
{
iBoxDB.DBDebug.DDebug.DeleteDBFiles(address);
}
/// <summary>
/// 存儲(chǔ)一個(gè)列表的數(shù)據(jù)
/// </summary>
/// <param name="tableName">向哪個(gè)表存數(shù)據(jù)</param>
/// <param name="data">要存儲(chǔ)的數(shù)據(jù)集合</param>
public void Save(string tableName, List<BaseObject> data)
{
IBox iBox = _Instance.GetBox();
Binder binder = iBox.Bind(tableName);//綁定表
foreach (BaseObject ob in data)
{
//如果表中之前有過該記錄抗楔,則Update;沒有則Insert
if (_Instance.autoBox.SelectCount("from " + tableName + " where ObjectName == ?", ob.ObjectName) <= 0)
binder.Insert(ob);
else
binder.Update(ob);
}
iBox.Commit();
iBox.Dispose();
}
/// <summary>
/// 存儲(chǔ)一個(gè)對(duì)象的數(shù)據(jù)
/// </summary>
/// <param name="tableName">向哪個(gè)表存數(shù)據(jù)</param>
/// <param name="ob">數(shù)據(jù)</param>
public void Save(string tableName, BaseObject ob)
{
if (_Instance.autoBox.SelectCount("from " + tableName + " where ObjectName == ?", ob.ObjectName) <= 0)
_Instance.autoBox.Insert<BaseObject>(tableName, ob);
else
_Instance.autoBox.Update<BaseObject>(tableName, ob);
}
/// <summary>
/// 獲取數(shù)據(jù)
/// </summary>
/// <param name="QL">QL語句</param>
/// <param name="param">QL參數(shù)</param>
/// <returns></returns>
public IBEnumerable<BaseObject> Get(string QL, string param)
{
return _Instance.autoBox.Select<BaseObject>(QL, param);
}
/// <summary>
/// IBox可以進(jìn)行數(shù)據(jù)庫的事務(wù)操作
/// </summary>
private IBox GetBox()
{
return _Instance.autoBox.Cube();
}
}
//基本數(shù)據(jù)類型
public class BaseObject : Dictionary<string, object>
{
public string ObjectName
{
get
{
return (string)base["ObjectName"];
}
set
{
if (value.Length > 20)
{
throw new ArgumentOutOfRangeException();
}
base["ObjectName"] = value;
}
}
}
然后是測試類拦坠。
using UnityEngine;
using System.Collections;
using iBoxDB.LocalServer;
using System.Collections.Generic;
//Insert必須表里沒有相同ID的記錄连躏;Update必須表里有相同ID的記錄
public class Test : MonoBehaviour
{
void Start ()
{
//TestUsingAutoBox();
TestUsingBox();
ShowResult();
}
private void TestUsingBox()
{
List<BaseObject> data = new List<BaseObject>();
BaseObject item = new BaseObject() { ObjectName = "Player8" };
item["position_x"] = 2;
item["rotation_x"] = 34.2f;
item["enable"] = false;
item["tag"] = "item1_tag";
BaseObject item2 = new BaseObject() { ObjectName = "Player9" };
item2["position_x"] = 23;
item2["rotation_x"] = 45.2f;
item2["enable"] = false;
item2["tag"] = "item1_tag";
data.Add(item);
data.Add(item2);
MyIBoxDB.GetInstance().Save(MyIBoxDB.TABLE_BED_SCENE, data);
}
private void TestUsingAutoBox()
{
BaseObject item = new BaseObject() { ObjectName = "Player6" };
item["position_x"] = 34;
item["rotation_x"] = 89.5f;
item["enable"] = true;
item["tag"] = "item1_tag";
MyIBoxDB.GetInstance().Save(MyIBoxDB.TABLE_BED_SCENE, item);
}
private void ShowResult()
{
foreach (BaseObject mItem in MyIBoxDB.GetInstance().Get("from " + MyIBoxDB.TABLE_BED_SCENE + " where ObjectName == ?", "Player8"))//只支持傳參
{
int position_x = (int)mItem["position_x"];
float rotation_x = (float)mItem["rotation_x"];
bool enable = (bool)mItem["enable"];
string tag = mItem["tag"] as string;
string s = "position_x = " + position_x + " rotation_x = " + rotation_x + " enable = " + enable + " tag = " + tag;
print(s);
}
}
}