- using UnityEngine;
- using System.Collections;
- using System.IO;
- using Mono.Data.Sqlite;
- public class Test: MonoBehaviour {
- string notice;
- void Start() {
- string p = "project.db";
- //不同平台下,设置不同的路径
- string filepath = "";
- if (Application.isEditor) {
- filepath = Application.dataPath + "/StreamingAssets/" + p;
- } else {
- if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
- filepath = Application.persistentDataPath + "/" + p;
- } else {
- filepath = Application.dataPath + "/StreamingAssets/" + p;
- }
- }
- //判断路径是否存在,安卓和苹果平台,要下载
- if (!File.Exists(filepath)) {
- WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + p);
- while (!loadDB.isDone) {} //这句代码是关键啊,当然,不建议这样写
- File.WriteAllBytes(filepath, loadDB.bytes);
- }
- //连接并且读取数据库信息
- SqliteConnection dbConnection = new SqliteConnection("URI=file:" + filepath);
- dbConnection.Open();
- SqliteCommand dbCommand = dbConnection.CreateCommand();
- dbCommand.CommandText = "select * from item_info";
- SqliteDataReader sqReader = dbCommand.ExecuteReader();
- while (sqReader.Read()) {
- for (int j = 0; j < sqReader.FieldCount; j++) {
- notice = notice + sqReader.GetName(j) + "=" + sqReader.GetValue(j).ToString() + " , ";
- }
- }
- sqReader.Close();
- dbConnection.Close();
- }
- void OnGUI() {
- GUI.Label(new Rect(Screen.width / 2 - 300, 0, 600, 600), notice);
- }
- }
来源: http://lib.csdn.net/snippet/vr/42112