本人在项目中遇到这种情况:记录多个物体的不同属性,主要是属性不同意,使用SQLite数据库的话,不同属性的物体就需要建不同的表,只能使用键值对类型来存储。网上查了查,在unity本地可以使用的noSQL数据库寥寥无几(如果有朋友知道,可以推荐给我啊),而这个IBoxDB多平台运行,是NoSQL类型的,正好满足要求。给出官方网址链接。,还有这位朋友的注意事项。
一、目录
如下图,是这个Demo的目录,Plugins下面是插件的dll和官方案例。Scripts下是本人写的小Demo。
二、直接上代码
代码中注释很清楚,就不啰嗦了。先是简单的工具类
using UnityEngine;
using System.Collections;
using iBoxDB.LocalServer;
using System.Collections.Generic;
using System;
/// <summary>
/// 实现一个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;//该对象可以进行CRUD操作,使用完毕不需要释放对象
private MyIBoxDB()
{
if (db == null)
{
DB.Root(Application.persistentDataPath);//数据库存储路径
db = new DB(1);//数据库地址,或者说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>
/// 删除数据库,IBoxDB中没有直接删除一个表的接口,所以这个方法显得格外亲切~
/// 注意:删除数据库之前要关闭该数据库
/// </summary>
/// <param name="address">数据库地址</param>
public void DeleteDataBase(int address)
{
iBoxDB.DBDebug.DDebug.DeleteDBFiles(address);
}
/// <summary>
/// 存储一个列表的数据
/// </summary>
/// <param name="tableName">向哪个表存数据</param>
/// <param name="data">要存储的数据集合</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>
/// 存储一个对象的数据
/// </summary>
/// <param name="tableName">向哪个表存数据</param>
/// <param name="ob">数据</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>
/// 获取数据
/// </summary>
/// <param name="QL">QL语句</param>
/// <param name="param">QL参数</param>
/// <returns></returns>
public IBEnumerable<BaseObject> Get(string QL, string param)
{
return _Instance.autoBox.Select<BaseObject>(QL, param);
}
/// <summary>
/// IBox可以进行数据库的事务操作
/// </summary>
private IBox GetBox()
{
return _Instance.autoBox.Cube();
}
}
//基本数据类型
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);
}
}
}
原文链接:https://blog.csdn.net/u011597114/article/details/54346286