U3D学习13-数据存储
生活随笔
收集整理的這篇文章主要介紹了
U3D学习13-数据存储
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、SQLLite
要注意Plguins的三個dll資源
2、利用ScriptableObject將數(shù)據(jù)存儲為資源(小規(guī)模數(shù)據(jù))
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ItemSet : ScriptableObject {
#if UNITY_EDITOR
[UnityEditor.MenuItem ("innyo/Create item set")]
public static void CreateItemSet ()
{
var objSet = CreateInstance<ItemSet> ();
string savePath = EditorUtility.SaveFilePanel (
"save",
"Assets/",
"ItemAsset",
"asset"
);
if (savePath != "") {
savePath ="Assets/"+ savePath.Replace (Application.dataPath, "");
UnityEditor.AssetDatabase.CreateAsset (objSet, savePath);
UnityEditor.AssetDatabase.SaveAssets ();
}
}
#endif
public Item[] items;
}
3、數(shù)據(jù)庫
//各平臺下數(shù)據(jù)庫存儲的絕對路徑(通用)
//PC:sql = new SQLiteHelper("data source=" + Application.dataPath + "/sqlite4unity.db");
//Mac:sql = new SQLiteHelper("data source=" + Application.dataPath + "/sqlite4unity.db");
//Android:sql = new SQLiteHelper("URI=file:" + Application.persistentDataPath + "/sqlite4unity.db");
//iOS:sql = new SQLiteHelper("data source=" + Application.persistentDataPath + "/sqlite4unity.db");
//PC平臺下的相對路徑
//sql = new SQLiteHelper("data source="sqlite4unity.db");
//編輯器:Assets/sqlite4unity.db
//編譯后:和AppName.exe同級的目錄下,這里比較奇葩
//當然可以用更隨意的方式sql = new SQLiteHelper("data source="D://SQLite//sqlite4unity.db");
//確保路徑存在即可否則會發(fā)生錯誤
//如果是事先創(chuàng)建了一份數(shù)據(jù)庫
//可以將這個數(shù)據(jù)庫放置在StreamingAssets目錄下然后再拷貝到
//Application.persistentDataPath + "/sqlite4unity.db"路徑即可
3.1、在Unity3D編輯器下生成數(shù)據(jù)庫文件(.db)默認位于和Assets目錄同級的位置,即項目的工程文件夾中。我們可以通過修改路徑在改變數(shù)據(jù)庫文件的存儲位置,具體來講:
Windows平臺:data source=Application.dataPath/數(shù)據(jù)庫名稱.db
IOS平臺:data source=Application.persistentDataPath/數(shù)據(jù)庫名稱.db
Android平臺:URL=file:Application.persistentDataPath/數(shù)據(jù)庫名稱.db(我想說Android平臺就是個奇葩,搞什么特殊化嘛)
3.2、確保Unity3D編輯器中的.NET版本和MonoDevelop中的.NET版本都為2.0版本,在Unity3D中打包導出的程序可能不會保留數(shù)據(jù)庫文件,因此需要手動將數(shù)據(jù)庫文件拷貝到相應的位置,當然更加合理的方案是將數(shù)據(jù)庫文件存放到StreamingAssets文件夾下,然后在第一次加載游戲的時候?qū)?shù)據(jù)庫文件復制到對應平臺上的存放位置。
3。3、在使用InsertValues方法時請參考SQLite中字段類型與C#中數(shù)據(jù)類型的對應關(guān)系,博主目前測試了int類型和string類型都沒有什么問題,更多類型的數(shù)據(jù)請大家自行測試然后告訴博主測試的結(jié)果,如果大家有興趣擴展這個輔助類的話可以自行去擴展哦,嘿嘿!
總結(jié)
以上是生活随笔為你收集整理的U3D学习13-数据存储的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hihoCoder #1183 : 连
- 下一篇: 如何用ajax提交多组同样的数据(数组)