转载 : http://blog.csdn.net/w88193363/article/details/21018389
所有的游戏开发都离不开数据存储的操作,Unity3D也不例外,下面向大家介绍一下Unity3D相关的数据存储方法。
一、PlayerPrefs是Unity系统自带的一种最简单的存储方式,以plist键值对方式存放,pc存放在注册表中,ios存放在plist中,而android存放在data文件夹/中。
使用例子如下:
- PlayerPrefs.SetInt("keyInt",100);
- PlayerPrefs.SetFloat("keyFloat",100.02f);
- PlayerPrefs.SetString("keyString","100");
- PlayerPrefs.GetInt("keyInt",0);
- PlayerPrefs.GetFloat("keyFloat",0);
- PlayerPrefs.GetString("keyString","0");
- PlayerPrefs.DeleteAll();
- PlayerPrefs.DeleteKey("keyInt");
- bool hasKeyInt = PlayerPrefs.HasKey("keyInt");
二、读取普通文本资源:TextAsset
- TextAsset text = (TextAsset)Resources.Load("unity3d");
- Debug.Log(text.text);
在Project窗口的根目录创建Resources文件夹,然后把名字为unity3d.txt的文件放在Resources文件夹下就可以读取到.
三、读取json格式文本资源,需要一个Json-LitJson.dll动态链接库
- TextAsset ta = (TextAsset)Resources.Load("unity3d");
- 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,以下示例代码包含了创建连接,打开连接,关闭连接,查询单个对象,查询对象列表和更新表格等方法;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.IO;
- using System.Text;
- using System.Data;
- using Mono.Data.Sqlite;
- public class DBManager {
- private static string connection;
- private static IDbConnection dbcon;
- private static IDbCommand dbcmd;
- private static IDataReader reader;
- static string datapath = "Journey.db";
- static void CreateConnect(){
- if(connection==null){
- Debug.Log("path:"+Application.persistentDataPath+"/"+datapath);
- if(!File.Exists(Application.persistentDataPath+"/"+datapath)){
- Debug.LogError("Path:"+Application.persistentDataPath+"/"+datapath+"\nDatabase File not exists!");
- return;
- }
- connection = "URI=file:" + Application.persistentDataPath+"/"+datapath; // we set the connection to our database
- dbcon = new SqliteConnection(connection);
- }
- }
- public static void OpenDB(){
- CreateConnect();
- if(dbcon.State != ConnectionState.Open)
- dbcon.Open();
- }
- public static void CloseDB(){
- try{
- if(reader!=null){
- reader.Dispose();
- reader.Close();
- reader = null;
- }
- if(dbcmd!=null){
- dbcmd.Dispose();
- dbcmd = null;
- }
- if(dbcon!=null){
- dbcon.Dispose();
- dbcon.Close();
- dbcon = null;
- }
- }catch{
- Debug.LogWarning("Fail to Close Database!");
- }
- }
- static int fieldCount = 0;
- static System.Type fieldtype;
- static System.Type ttype;
- public static T QuerySingle<T>(string sql){
- OpenDB();
- dbcmd = dbcon.CreateCommand();
- dbcmd.CommandText = sql;
- try{
- Debug.Log("Execute QuerySingle Sql:"+sql);
- reader = dbcmd.ExecuteReader();
- }catch{
- return default(T);
- }
- T t;
- if(!reader.Read())
- return default(T);
- t = System.Activator.CreateInstance<T>();
- ttype = typeof(T);
- fieldCount = reader.FieldCount;
- for(int i=0;i<fieldCount;i++){
- fieldtype = reader.GetFieldType(i);
- if(fieldtype.Equals(typeof(System.Int64))){
- ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt64(i));
- }else if(fieldtype.Equals(typeof(System.Int32))){
- ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt32(i));
- }else if(fieldtype.Equals(typeof(System.Int16))){
- ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt16(i));
- }else if(fieldtype.Equals(typeof(string))){
- ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetString(i));
- }else if(fieldtype.Equals(typeof(float))){
- ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetFloat(i));
- }
- }
- return t;
- }
- public static List<T> QueryList<T>(string sql){
- OpenDB();
- List<T> list = new List<T>();
- dbcmd = dbcon.CreateCommand();
- dbcmd.CommandText = sql;
- try{
- Debug.Log("Execute Sql:"+sql);
- reader = dbcmd.ExecuteReader();
- }catch{
- Debug.Log("Execute Sql Exception!");
- return null;
- }
- T t;
- while(reader.Read()){
- t = System.Activator.CreateInstance<T>();
- ttype = typeof(T);
- fieldCount = reader.FieldCount;
- for(int i=0;i<fieldCount;i++){
- if(ttype.GetField(reader.GetName(i))==null)continue;
- fieldtype = reader.GetFieldType(i);
- if(fieldtype.Equals(typeof(System.Int64))){
- ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt64(i));
- }else if(fieldtype.Equals(typeof(System.Int32))){
- ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt32(i));
- }else if(fieldtype.Equals(typeof(System.Int16))){
- ttype.GetField(reader.GetName(i)).SetValue(t,(int)reader.GetInt16(i));
- }else if(fieldtype.Equals(typeof(string))){
- ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetString(i));
- }else if(fieldtype.Equals(typeof(double))){
- ttype.GetField(reader.GetName(i)).SetValue(t,reader.GetFloat(i));
- }
- }
- list.Add(t);
- }
- return list;
- }
- public static int UpdateTable(string sql){
- OpenDB();
- try{
- dbcmd = dbcon.CreateCommand();
- dbcmd.CommandText = sql;
- int rows = 0;
- rows = dbcmd.ExecuteNonQuery();
- return rows;
- }catch{
- return 0;
- }
- }
- }