时间:2021-07-01 10:21:17 帮助过:3人阅读
数据结构类,相当于数据库表结构类似
using UnityEngine; using System.Collections; public class Persss { public string Name { get; set; } public string Msg { get; set; } public int OID { get; set; } }
数据操作的相对Persss的一个封装类,使数据操作更清晰
using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.Linq; using Sqo; public class MyPManager { public static List<Persss> GetRecentPlayers(int count) { Siaqodb database = MyDBFactory.GetInstance(); IEnumerable<Persss> query = (from Persss player in database orderby player.OID descending select player).Take(count); return query.ToList<Persss>(); } public static Persss LoadPlayer(string name) { Siaqodb database = MyDBFactory.GetInstance(); Persss p = (from Persss player in database where player.Name == name select player).FirstOrDefault<Persss>(); if (p == null)//not exists so create and save { p = new Persss(); p.Name = name; p.Msg = "未找到"; } return p; } public static int TotalPlayers() { Siaqodb database = MyDBFactory.GetInstance(); return database.Count<Persss>(); } public static void SavePlayer(Persss p) { Siaqodb database = MyDBFactory.GetInstance(); database.StoreObject(p); } }
主要的类
using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.IO; using Sqo; using UnityEngine.UI; using System.Linq; public class mySiaqodbDemo : MonoBehaviour { Siaqodb database; public InputField inset_name; public InputField inset_msg; public InputField read_name; public InputField read_msg; public InputField read_Oid; public Text show_text; public InputField up_name; public InputField up_msg; public InputField up_Oid; public InputField del_input; // Use this for initialization void Start () { database = MyDBFactory.GetInstance(); List<Persss> recentPlayers = MyPManager.GetRecentPlayers(MyPManager.TotalPlayers()); Debug.Log("Perss count is " + MyPManager.TotalPlayers() ); foreach (Persss p3 in recentPlayers) { Debug.Log("OID: " +p3.OID.ToString() +" Name:" + p3.Name + " Message:" + p3.Msg +"--end--"); if(p3.OID<410) { //database.Delete(p3); } } } public void BtnInSertData() { Persss p = new Persss(); string n = inset_name.text.Trim(); string ms = inset_msg.text.Trim(); if( n.Length >1 && ms.Length >1 ) { p.Name = inset_name.text; p.Msg = inset_msg.text; MyPManager.SavePlayer(p); } BtnReadAllData(); } public void BtnReadDataByOID() { int ood = Convert.ToInt32(read_Oid.text); Debug.Log(ood); Persss dss = (from Persss p in database where (p.OID == ood) select p).FirstOrDefault(); if (dss != null) { //Persss dssp = dss as Persss; read_name.text = dss.Name; read_msg.text = dss.Msg; read_Oid.text = dss.OID.ToString(); up_Oid.text = dss.OID.ToString(); up_name.text = dss.Name; up_msg.text = dss.Msg; } else { read_Oid.text = "空值"; } } public void BtnReadAllData() { List<Persss> recentPlayers = MyPManager.GetRecentPlayers(MyPManager.TotalPlayers()); Debug.Log("Perss count is " + MyPManager.TotalPlayers() ); show_text.text = ""; foreach (Persss p3 in recentPlayers) { Debug.Log("OID: " +p3.OID.ToString() +" Name:" + p3.Name + " Message:" + p3.Msg +"--end--"); Debug.Log("ReadAllDate"); show_text.text += "OID: " + p3.OID.ToString() + " Name:" + p3.Name + " Message:" + p3.Msg + "--end--"; } } public void BtnUpdateDataByOID() { Persss u = (from Persss p in database where (p.OID == Convert.ToInt32(up_Oid.text)) select p).FirstOrDefault(); Debug.Log( up_Oid.text); if (u != null) { u.Name = up_name.text; u.Msg = up_msg.text; // u.OID =Convert.ToInt32( up_Oid.text ); Debug.Log("UpdataByOID"); database.StoreObject(u); } BtnReadAllData(); } public void BtnDeleteDataByName() { Persss dss = (from Persss p in database where (p.Name == del_input.text ) select p).FirstOrDefault(); if (dss != null) { database.Delete(dss); } BtnReadAllData(); } public void BtnDeleteDataByMsg() { Persss dss = (from Persss p in database where (p.Msg == del_input.text) select p).FirstOrDefault(); if (dss != null) { database.Delete(dss); } BtnReadAllData(); } public void BtnDeleteDataByOID() { Persss dss = (from Persss p in database where (p.OID == Convert.ToInt32 (del_input.text)) select p).FirstOrDefault(); if (dss != null) { database.Delete(dss); } BtnReadAllData(); } void OnApplicationQuit() { database = null; } }
Unity3D主界面
插入数据测试
其它功能类似可用
Unity3D使用Siaqodb基本增删改查Demo
标签: