添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Toggle

Unity3d插件简介

Unity3D底层是c++实现的,用户用c#进行开发,不过是用mono开源,开源的 .NET 开发框架。在实际应用中,我们总有一些代码是其他编程语言实现,历史遗留。插件可以是C、C++、Objective-C等语言实现的。

1、插件代码

1、百度云盘:里面有SimplestPluginExample-4.0和boost asio打开串口的例子 http://pan.baidu.com/s/1mgy47jU

2、目录jni的MyClass.h是关于boost asio打开串口设备代码

3、下面代码来自官方SimplestPluginExample-4.0

#if _MSC_VER // this is defined when compiling with Visual Studio
#define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this
#else
#define EXPORT_API // XCode does not need annotating exported functions, so define is empty
#endif
// ------------------------------------------------------------------------
// Plugin itself
// Link following functions C-style (required for plugins)
extern "C"
// The functions we will call from Unity.
const EXPORT_API char*  PrintHello(){
    return "Hello";
int EXPORT_API PrintANumber(){
    return 5;
int EXPORT_API AddTwoIntegers(int a, int b) {
    return a + b;
float EXPORT_API AddTwoFloats(float a, float b) {
    return a + b;
} // end of export C block

2、构建Windows插件

打开SimplestPluginExample-4.0SimplestPluginExampleVS2008PluginVisualStudioASimplePlugin.sln

3、构建Android插件

  • 本机环境windows7
  • 下载NDK,将Androidandroid-ndk-r9d加到系统环境变量
  • 进入目录Unity3dPlugin-boostAsioboost-asio
  • 执行:ndk-build
  • 注意:一定要动态libgnustl_shared.so

     Unity3d插件boost asio应用

    1、拷贝库

    将编译好的dll和so文件放对应入AssetsPlugins目录下的Android、x86

    2、添加到unity3d工程代码

    using System.Runtime.InteropServices;
    [DllImport("paokunet")]
    private static extern void startNet();
    [DllImport("paokunet")]
    private static extern void stopNet();
    [DllImport("paokunet")]
    private static extern bool getNetStats();

     3、windows应用

    boost库地址:http://sourceforge.net/projects/boost/files/boost-binaries/

    没什么好说的

    4、安卓应用

  • boost 安卓库 里面有说明编译,如不想看就结尾有现成库。
  • unity3d导出安卓项目记得导出地方对google android打勾
  • boost asio是因动态加载stl才能工作正常
  • 1、添加一个java文件类似这样的

    import com.unity3d.player.UnityPlayerActivity;
    import android.os.Bundle;
    import android.util.Log;
    public class OverrideExample extends UnityPlayerNativeActivity {
        static
            //加载.so文件
          Log.d("OverrideActivity", "paokunet freeyun ???????");
              System.loadLibrary("gnustl_shared");
              System.loadLibrary("paokunet");
    

    2、AndroidManifest.xml类似这样

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Unity3d.u3d" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
      <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
      <application android:theme="@android:style/Theme.NoTitleBar" android:icon="@drawable/app_icon" android:label="@string/app_name" >
        <activity android:label="@string/app_name" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.hj.u3d.OverrideExample">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
          </intent-filter>
          <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
          <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
        </activity>
      </application>
      <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="22" />
      <uses-feature android:glEsVersion="0x00020000" />
      <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
      <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
      <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
    </manifest>

    只是用用boost aiso串口代码,注意串口的设备打开是不一样的,安卓和Linux设备类似“”/dev/ttyS1”,windows类“COM8”如果需要用tcp udp通信也是可以的。