u3d数据存储

转载 : http://blog.csdn.net/w88193363/article/details/21018389


所有的游戏开发都离不开数据存储的操作,Unity3D也不例外,下面向大家介绍一下Unity3D相关的数据存储方法。

一、PlayerPrefsUnity系统自带的一种最简单的存储方式,以plist键值对方式存放,pc存放在注册表中,ios存放在plist中,而android存放在data文件夹/中。

使用例子如下:

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. PlayerPrefs.SetInt("keyInt",100);  
  2. PlayerPrefs.SetFloat("keyFloat",100.02f);  
  3. PlayerPrefs.SetString("keyString","100");  
  4. PlayerPrefs.GetInt("keyInt",0);  
  5. PlayerPrefs.GetFloat("keyFloat",0);  
  6. PlayerPrefs.GetString("keyString","0");  
  7. PlayerPrefs.DeleteAll();  
  8. PlayerPrefs.DeleteKey("keyInt");  
  9. bool hasKeyInt = PlayerPrefs.HasKey("keyInt");  


二、读取普通文本资源:TextAsset

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. TextAsset text = (TextAsset)Resources.Load("unity3d");  
  2. Debug.Log(text.text);  


Project窗口的根目录创建Resources文件夹,然后把名字为unity3d.txt的文件放在Resources文件夹下就可以读取到.

三、读取json格式文本资源,需要一个Json-LitJson.dll动态链接库

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. TextAsset ta = (TextAsset)Resources.Load("unity3d");  
  2. JsonData jsonData = JsonMapper.ToObject(ta.text);  

就以下面的json数据为例说明:

int arrLength = jsonData.Count;//长度

string str = jsonData[0][1];//字符串,应该得到的是"http:www.u3dblog.com"



四、sqlite数据库存储,需要sqliteclient_and_data_dlls,以下示例代码包含了创建连接,打开连接,关闭连接,查询单个对象,查询对象列表和更新表格等方法;

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Reflection;  
  5. using System.IO;  
  6. using System.Text;  
  7. using System.Data;  
  8. using Mono.Data.Sqlite;  
  9. public class DBManager {  
  10.         private static string connection;  
  11.     private static IDbConnection dbcon;  
  12.     private static IDbCommand dbcmd;  
  13.         private static IDataReader reader;  
  14.         static string datapath = "Journey.db";  
  15.         static void CreateConnect(){  
  16.                 if(connection==null){  
  17.                         Debug.Log("path:"+Application.persistentDataPath+"/"+datapath);  
  18.                         if(!File.Exists(Application.persistentDataPath+"/"+datapath)){  
  19.                 Debug.LogError("Path:"+Application.persistentDataPath+"/"+datapath+"\nDatabase File not exists!");  
  20.                                 return;  
  21.                         }  
  22.                         connection = "URI=file:" + Application.persistentDataPath+"/"+datapath; // we set the connection to our database  
  23.                     dbcon = new SqliteConnection(connection);  
  24.                 }  
  25.         }  
  26.         public static void OpenDB(){  
  27.                 CreateConnect();  
  28.                 if(dbcon.State != ConnectionState.Open)  
  29.                     dbcon.Open();  
  30.         }  
  31.         public static void CloseDB(){  
  32.                 try{  
  33.                         if(reader!=null){  
  34.                                 reader.Dispose();  
  35.                                 reader.Close();  
  36.                                 reader = null;  
  37.                         }  
  38.                         if(dbcmd!=null){  
  39.                                 dbcmd.Dispose();  
  40.                                 dbcmd = null;  
  41.                         }  
  42.                         if(dbcon!=null){  
  43.                                 dbcon.Dispose();  
  44.                                 dbcon.Close();  
  45.                                 dbcon = null;  
  46.                         }  
  47.                 }catch{  
  48.                         Debug.LogWarning("Fail to Close Database!");  
  49.                 }  
  50.         }  
  51.         static int fieldCount = 0;  
  52.         static System.Type fieldtype;  
  53.         static System.Type ttype;  
  54.         public static T QuerySingle<T>(string sql){  
  55.                 OpenDB();  
  56.                 dbcmd = dbcon.CreateCommand();  
  57.                 dbcmd.CommandText = sql;  
  58.                 try{  
  59.                         Debug.Log("Execute QuerySingle Sql:"+sql);  
  60.                         reader = dbcmd.ExecuteReader();  
  61.                 }catch{  
  62.                         return default(T);  
  63.                 }  
  64.                 T t;  
  65.                 if(!reader.Read())  
  66.                         return default(T);  
  67.                 t = System.Activator.CreateInstance<T>();  
  68.                 ttype = typeof(T);  
  69.                 fieldCount = reader.FieldCount;  
  70.                 for(int i=0;i<fieldCount;i++){  
  71.                         fieldtype = reader.GetFieldType(i);  
  72.                         if(fieldtype.Equals(typeof(System.Int64))){  
  73.                                 ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt64(i));  
  74.                         }else if(fieldtype.Equals(typeof(System.Int32))){  
  75.                                 ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt32(i));  
  76.                         }else if(fieldtype.Equals(typeof(System.Int16))){  
  77.                                 ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt16(i));  
  78.                         }else if(fieldtype.Equals(typeof(string))){  
  79.                                 ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetString(i));  
  80.                         }else if(fieldtype.Equals(typeof(float))){  
  81.                                 ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetFloat(i));  
  82.                         }  
  83.                 }  
  84.                 return t;  
  85.         }  
  86.         public static List<T> QueryList<T>(string sql){  
  87.                 OpenDB();  
  88.                 List<T> list = new List<T>();  
  89.                 dbcmd = dbcon.CreateCommand();  
  90.                 dbcmd.CommandText = sql;  
  91.                 try{  
  92.                         Debug.Log("Execute Sql:"+sql);  
  93.                         reader = dbcmd.ExecuteReader();  
  94.                 }catch{  
  95.                         Debug.Log("Execute Sql Exception!");  
  96.                         return null;  
  97.                 }  
  98.                 T t;  
  99.                 while(reader.Read()){  
  100.                         t = System.Activator.CreateInstance<T>();  
  101.                         ttype = typeof(T);  
  102.                         fieldCount = reader.FieldCount;  
  103.                         for(int i=0;i<fieldCount;i++){  
  104.                                 if(ttype.GetField(reader.GetName(i))==null)continue;  
  105.                                 fieldtype = reader.GetFieldType(i);  
  106.                                 if(fieldtype.Equals(typeof(System.Int64))){  
  107.                                         ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt64(i));  
  108.                                 }else if(fieldtype.Equals(typeof(System.Int32))){  
  109.                                         ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt32(i));  
  110.                                 }else if(fieldtype.Equals(typeof(System.Int16))){  
  111.                                         ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt16(i));  
  112.                                 }else if(fieldtype.Equals(typeof(string))){  
  113.                                         ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetString(i));  
  114.                                 }else if(fieldtype.Equals(typeof(double))){  
  115.                                         ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetFloat(i));  
  116.                                 }  
  117.                         }  
  118.                         list.Add(t);  
  119.                 }  
  120.                 return list;  
  121.         }  
  122.         public static int UpdateTable(string sql){  
  123.                 OpenDB();  
  124.                 try{  
  125.                         dbcmd = dbcon.CreateCommand();  
  126.                         dbcmd.CommandText = sql;  
  127.                         int rows = 0;  
  128.                         rows = dbcmd.ExecuteNonQuery();  
  129.                         return rows;  
  130.                 }catch{  
  131.                         return 0;  
  132.                 }  
  133.         }  
  134. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值