Unity3D研究院之在Unity中打开第三方数据库配合Android开发(三十二)

来源:互联网 发布:开通服务器端口 编辑:程序博客网 时间:2024/06/06 20:14

如果大家对Unity中如何使用数据库还不是很了解那么请看我之前的这篇文章。Unity3D研究院之使用C#语言建立本地数据库(二十三)本篇文章我们讨论如何在Unity中打开一个第三方数据库配合Android与编辑器进行同步开发。如下图所示,这个是我目前工程的结构,为了方便调试MOMO使用预定义标签将编辑器与Android平台区分开,方便编辑器与Android平台同时调试。

下图如果看的不清楚,点击图片可查看大图。 不仅在Unity编辑器中可以显示数据库读取的内容,直接编译在真机中也可以显示数据库读取的内容。

Unity3D研究院之在Unity中打开第三方数据库配合Android开发(三十二) - 雨松MOMO程序研究院 - 1

 

按照上图所示我们把第三方数据库放在Plugins->Android->assets中,切记必须放在这里,否无无效,然后是代码。这个第三方数据库是MOMO以前做测试一个号码归属地的时候制作的。所以数据库的内容还是比较大的,蛤蛤。

DbAccess.cs

 

然后是Test.cs直接把它挂在摄像机对象身上。

 

上面代码中我们使用到了预定于标签,用于编译时区分游戏平台与版本。这么一来我们既可以在编辑器中操作数据库也可以在Android中操作数据库一举两得,主要是是Android平台比较特殊,不能从Unity的Assets资源路径中读取二进制文件。说道二进制文件,这里不仅读取第三方数据库还可以读取其它文件,二进制文件都可以读取,比如文本文件、图片、资源等等。

需要注意的是你得把你的二进制文件放在Plugins->Android->assets中,然后根据下面的路径就可以在Android中读取。

string Path  = jar:file://” + Application.dataPath + “!/assets/” + “你的文件“;

 

另外,使用这种方法读取地方放数据库后,是可以继续向数据库执行插入、删除、修改 、查询、等操作,用起来还是比较方便的。

 

Unity预定义标签的种类还有很多,不仅可以区分平台还可以区分版本。

  UNITY_EDITORDefine for calling Unity Editor scripts from your game code.UNITY_STANDALONE_OSXPlatform define for compiling/executing code specifically for Mac OS (This includes Universal, PPC and Intel architectures).UNITY_DASHBOARD_WIDGETPlatform define when creating code for Mac OS dashboard widgets.UNITY_STANDALONE_WINUse this when you want to compile/execute code for Windows stand alone applications.UNITY_WEBPLAYERPlatform define for web player content (this includes Windows and Mac Web player executables).UNITY_WIIPlatform define for compiling/executing code for the Wii console.UNITY_IPHONEPlatform define for compiling/executing code for the iPhone platform.UNITY_ANDROIDPlatform define for the Android platform.UNITY_PS3Platform define for running PlayStation 3 code.UNITY_XBOX360Platform define for executing Xbox 360 code.UNITY_NACLPlatform define when compiling code for Google native client (this will be set additionally to UNITY_WEBPLAYER).UNITY_FLASHPlatform 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_6Platform define for the major version of Unity 2.6.UNITY_2_6_1Platform define for specific version 1 from the major release 2.6.UNITY_3_0Platform define for the major version of Unity 3.0.UNITY_3_0_0Platform define for the specific version 0 of Unity 3.0.UNITY_3_1Platform define for major version of Unity 3.1.UNITY_3_2Platform define for major version of Unity 3.2.UNITY_3_3Platform define for major version of Unity 3.3.UNITY_3_4Platform define for major version of Unity 3.4.UNITY_3_5Platform define for major version of Unity 3.5. 

 详细内容可以查看官网http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html

用法就不解释了,地球人都知道。

雨松MOMO希望和大家一起进步,加油~~~最后文本的源码下载:http://vdisk.weibo.com/s/ac3xI

雨松MOMO祝大家学习愉快,哇咔咔,晚安啦啦啦~~

0 0
原创粉丝点击