マルチプラットフォームデータストレージプラグインIBoxDB学習
5705 ワード
複数の物体の異なる属性を記録し、主に属性が同意しないため、SQLiteデータベースを使用すると、異なる属性の物体は異なるテーブルを構築する必要があり、キー値対タイプでしか記憶できない.ネットで調べたところ、unityローカルで使えるnoSQLデータベースはごくわずか(友人が知っていればお勧めしてくれるでしょう)でしたが、このIBoxDBマルチプラットフォームは、NoSQLタイプで、ちょうど要件を満たしています.公式サイトへのリンクを提供します.この友達の注意事項もあります.
IBOXDBダウンロードアドレス
コードに注釈がはっきりしているので、くどくどしません.まず簡単なツールクラス
IBOXDBダウンロードアドレス
コードに注釈がはっきりしているので、くどくどしません.まず簡単なツールクラス
using UnityEngine;
using System.Collections;
using iBoxDB.LocalServer;
using System.Collections.Generic;
using System;
///
/// Save Read
///
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(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;
}
///
/// ,IBoxDB , ~
/// :
///
///
public void DeleteDataBase(int address)
{
iBoxDB.DBDebug.DDebug.DeleteDBFiles(address);
}
///
///
///
///
///
public void Save(string tableName, List 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();
}
///
///
///
///
///
public void Save(string tableName, BaseObject ob)
{
if (_Instance.autoBox.SelectCount("from " + tableName + " where ObjectName == ?", ob.ObjectName) <= 0)
_Instance.autoBox.Insert(tableName, ob);
else
_Instance.autoBox.Update(tableName, ob);
}
///
///
///
/// QL
/// QL
///
public IBEnumerable Get(string QL, string param)
{
return _Instance.autoBox.Select(QL, param);
}
///
/// IBox
///
private IBox GetBox()
{
return _Instance.autoBox.Cube();
}
}
//
public class BaseObject : Dictionary
{
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 data = new List();
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);
}
}
}