c++:

注意x86/x64,vs2015.

 #include "stdafx.h"

 extern "C"
{
int Add(int a, int b) {
return a + b;
}
}

TestPlugin.cpp

 LIBRARY "TestPlugin"

 EXPORTS

 Add @

TestPlugin.def

 public class Test : MonoBehaviour {

     private void Start() {
Debug.Log(PluginDefine.Add(,));
}
}

Test.cs

Unity3d插件开发与SDK对接实战 学习-LMLPHP

jni:

Unity3d插件开发与SDK对接实战 学习-LMLPHPUnity3d插件开发与SDK对接实战 学习-LMLPHPUnity3d插件开发与SDK对接实战 学习-LMLPHPUnity3d插件开发与SDK对接实战 学习-LMLPHP

Unity3d插件开发与SDK对接实战 学习-LMLPHPUnity3d插件开发与SDK对接实战 学习-LMLPHP

java

Unity3d插件开发与SDK对接实战 学习-LMLPHP

Unity3d插件开发与SDK对接实战 学习-LMLPHP

 package com.VoidGame.test;
import android.os.Bundle;
import android.util.Log; import com.unity3d.player.UnityPlayerActivity; public class MyUnityActivity extends UnityPlayerActivity { public static MyUnityActivity Instance; protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Instance = this;
Log.d("OverrideActivity","my android activity called!");
} public int java_add(int a,int b){
return a+b;
}
}

MyUnityActivity

 <?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/> <application
android:theme="@style/UnityThemeSelector"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name="com.VoidGame.test.MyUnityActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>

AndroidManifest

 /*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using UnityEngine.UI;
using System.Collections; namespace VoidGame { public class Test : MonoBehaviour { private void Start() { AndroidJavaClass cls = new AndroidJavaClass("com.VoidGame.test.MyUnityActivity");
AndroidJavaObject instance = cls.GetStatic<AndroidJavaObject>("Instance");
int result = instance.Call<int>("java_add",1,2);
GameObject.Find("Text").GetComponent<Text>().text = result.ToString();
} private void OnGUI() { }
}
}

Test

05-11 14:39