http://www.xuanyusong.com/archives/831
http://www.xuanyusong.com/archives/1454
如果大家对Unity中如何使用数据库还不是很了解那么请看我之前的这篇文章。Unity3D研究院之使用C#语言建立本地数据库(二十三)本篇文章我们讨论如何在Unity中打开一个第三方数据库配合Android与编辑器进行同步开发。如下图所示,这个是我目前工程的结构,为了方便调试MOMO使用预定义标签将编辑器与Android平台区分开,方便编辑器与Android平台同时调试。
下图如果看的不清楚,点击图片可查看大图。 不仅在Unity编辑器中可以显示数据库读取的内容,直接编译在真机中也可以显示数据库读取的内容。
按照上图所示我们把第三方数据库放在Plugins->Android->assets中,切记必须放在这里,否无无效,然后是代码。这个第三方数据库是MOMO以前做测试一个号码归属地的时候制作的。所以数据库的内容还是比较大的,蛤蛤。
DbAccess.cs
004 | using System.Collections; |
005 | using Mono.Data.Sqlite; |
007 | public class DbAccess |
011 | private SqliteConnection dbConnection; |
013 | private SqliteCommand dbCommand; |
015 | private SqliteDataReader reader; |
017 | public DbAccess ( string connectionString) |
021 | OpenDB (connectionString); |
029 | public void OpenDB ( string connectionString) |
034 | dbConnection = new SqliteConnection (connectionString); |
036 | dbConnection.Open (); |
038 | Debug.Log ( "Connected to db" ); |
042 | string temp1 = e.ToString(); |
048 | public void CloseSqlConnection () |
052 | if (dbCommand != null ) { |
054 | dbCommand.Dispose (); |
060 | if (reader != null ) { |
068 | if (dbConnection != null ) { |
070 | dbConnection.Close (); |
076 | Debug.Log ( "Disconnected from db." ); |
080 | public SqliteDataReader ExecuteQuery ( string sqlQuery) |
084 | dbCommand = dbConnection.CreateCommand (); |
086 | dbCommand.CommandText = sqlQuery; |
088 | reader = dbCommand.ExecuteReader (); |
094 | public SqliteDataReader ReadFullTable ( string tableName) |
098 | string query = "SELECT * FROM " + tableName; |
100 | return ExecuteQuery (query); |
104 | public SqliteDataReader InsertInto ( string tableName, string [] values) |
108 | string query = "INSERT INTO " + tableName + " VALUES (" + values[0]; |
110 | for ( int i = 1; i < values.Length; ++i) { |
112 | query += ", " + values[i]; |
118 | return ExecuteQuery (query); |
122 | public SqliteDataReader UpdateInto ( string tableName, string []cols, string []colsvalues, string selectkey, string selectvalue) |
125 | string query = "UPDATE " +tableName+ " SET " +cols[0]+ " = " +colsvalues[0]; |
127 | for ( int i = 1; i < colsvalues.Length; ++i) { |
129 | query += ", " +cols[i]+ " =" + colsvalues[i]; |
132 | query += " WHERE " +selectkey+ " = " +selectvalue+ " " ; |
134 | return ExecuteQuery (query); |
137 | public SqliteDataReader Delete( string tableName, string []cols, string []colsvalues) |
139 | string query = "DELETE FROM " +tableName + " WHERE " +cols[0] + " = " + colsvalues[0]; |
141 | for ( int i = 1; i < colsvalues.Length; ++i) { |
143 | query += " or " +cols[i]+ " = " + colsvalues[i]; |
146 | return ExecuteQuery (query); |
149 | public SqliteDataReader InsertIntoSpecific ( string tableName, string [] cols, string [] values) |
153 | if (cols.Length != values.Length) { |
155 | throw new SqliteException ( "columns.Length != values.Length" ); |
159 | string query = "INSERT INTO " + tableName + "(" + cols[0]; |
161 | for ( int i = 1; i < cols.Length; ++i) { |
163 | query += ", " + cols[i]; |
167 | query += ") VALUES (" + values[0]; |
169 | for ( int i = 1; i < values.Length; ++i) { |
171 | query += ", " + values[i]; |
177 | return ExecuteQuery (query); |
181 | public SqliteDataReader DeleteContents ( string tableName) |
185 | string query = "DELETE FROM " + tableName; |
187 | return ExecuteQuery (query); |
191 | public SqliteDataReader CreateTable ( string name, string [] col, string [] colType) |
195 | if (col.Length != colType.Length) { |
197 | throw new SqliteException ( "columns.Length != colType.Length" ); |
201 | string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0]; |
203 | for ( int i = 1; i < col.Length; ++i) { |
205 | query += ", " + col[i] + " " + colType[i]; |
211 | return ExecuteQuery (query); |
215 | public SqliteDataReader SelectWhere ( string tableName, string [] items, string [] col, string [] operation, string [] values) |
219 | if (col.Length != operation.Length || operation.Length != values.Length) { |
221 | throw new SqliteException ( "col.Length != operation.Length != values.Length" ); |
225 | string query = "SELECT " + items[0]; |
227 | for ( int i = 1; i < items.Length; ++i) { |
229 | query += ", " + items[i]; |
233 | query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' " ; |
235 | for ( int i = 1; i < col.Length; ++i) { |
237 | query += " AND " + col[i] + operation[i] + "'" + values[0] + "' " ; |
241 | return ExecuteQuery (query); |
然后是Test.cs直接把它挂在摄像机对象身上。
02 | using System.Collections; |
03 | using System.Collections.Generic; |
05 | using Mono.Data.Sqlite; |
06 | public class Test :MonoBehaviour |
09 | string number = "null" ; |
11 | string location = "null" ; |
28 | string appDBPath = Application.dataPath + "/Plugins/Android/assets/" + "location.db" ; |
29 | DbAccess db = new DbAccess( "URI=file:" + appDBPath); |
34 | string appDBPath = Application.persistentDataPath + "/" + "location.db" ; |
37 | if (!File.Exists(appDBPath)) |
41 | WWW loadDB = new WWW( "jar:file://" + Application.dataPath + "!/assets/" + "location.db" ); |
44 | File.WriteAllBytes(appDBPath, loadDB.bytes); |
49 | DbAccess db = new DbAccess( "URI=file:" + appDBPath); |
53 | using (SqliteDataReader sqReader = db.SelectWhere( "location_date" , new string []{ "number" , "city" , "location" }, new string []{ "_id" }, new string []{ "=" }, new string []{ "25" })) |
56 | while (sqReader.Read()) |
59 | number = sqReader.GetString(sqReader.GetOrdinal( "number" )); |
60 | city = sqReader.GetString(sqReader.GetOrdinal( "city" )); |
61 | location = sqReader.GetString(sqReader.GetOrdinal( "location" )) ; |
63 | Debug.Log( "number =" + number + " city =" + city + " location =" + location ); |
70 | db.CloseSqlConnection(); |
78 | GUILayout.Box( "number =" + number); |
79 | GUILayout.Box( "city =" + city); |
80 | GUILayout.Box( "location =" + location); |
上面代码中我们使用到了预定于标签,用于编译时区分游戏平台与版本。这么一来我们既可以在编辑器中操作数据库也可以在Android中操作数据库一举两得,主要是是Android平台比较特殊,不能从Unity的Assets资源路径中读取二进制文件。说道二进制文件,这里不仅读取第三方数据库还可以读取其它文件,二进制文件都可以读取,比如文本文件、图片、资源等等。
需要注意的是你得把你的二进制文件放在Plugins->Android->assets中,然后根据下面的路径就可以在Android中读取。
string Path = jar:file://” + Application.dataPath + “!/assets/” + “你的文件“;
另外,使用这种方法读取地方放数据库后,是可以继续向数据库执行插入、删除、修改 、查询、等操作,用起来还是比较方便的。
Unity预定义标签的种类还有很多,不仅可以区分平台还可以区分版本。
| |
UNITY_EDITOR | Define for calling Unity Editor scripts from your game code. |
UNITY_STANDALONE_OSX | Platform define for compiling/executing code specifically for Mac OS (This includes Universal, PPC and Intel architectures). |
UNITY_DASHBOARD_WIDGET | Platform define when creating code for Mac OS dashboard widgets. |
UNITY_STANDALONE_WIN | Use this when you want to compile/execute code for Windows stand alone applications. |
UNITY_WEBPLAYER | Platform define for web player content (this includes Windows and Mac Web player executables). |
UNITY_WII | Platform define for compiling/executing code for the Wii console. |
UNITY_IPHONE | Platform define for compiling/executing code for the iPhone platform. |
UNITY_ANDROID | Platform define for the Android platform. |
UNITY_PS3 | Platform define for running PlayStation 3 code. |
UNITY_XBOX360 | Platform define for executing Xbox 360 code. |
UNITY_NACL | Platform define when compiling code for Google native client (this will be set additionally to UNITY_WEBPLAYER). |
UNITY_FLASH | Platform define when compiling code for Adobe Flash. |
Note: These defines were introduced at version 3.0.
Also you can compile code selectively depending on the version of the engine you are working on. Currently the supported ones are:
UNITY_2_6 | Platform define for the major version of Unity 2.6. |
UNITY_2_6_1 | Platform define for specific version 1 from the major release 2.6. |
UNITY_3_0 | Platform define for the major version of Unity 3.0. |
UNITY_3_0_0 | Platform define for the specific version 0 of Unity 3.0. |
UNITY_3_1 | Platform define for major version of Unity 3.1. |
UNITY_3_2 | Platform define for major version of Unity 3.2. |
UNITY_3_3 | Platform define for major version of Unity 3.3. |
UNITY_3_4 | Platform define for major version of Unity 3.4. |
UNITY_3_5 | Platform define for major version of Unity 3.5. |
详细内容可以查看官网http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html
用法就不解释了,地球人都知道。
雨松MOMO希望和大家一起进步,加油~~~最后文本的源码下载:http://vdisk.weibo.com/s/ac3xI
StreamingAssets 是一个只读的文件夹, 把二进制文件可以提前放在这里面!
4.1.2测试失败,后把db放到StreamingAssets下面搞定