自己记录用,版本unity2017.1.1f1,Androidstudio3.0,vuforia
单独的写好的unity程序,注意横竖屏,如果不对应会闪退
创建Androidstudio工程,然后复制jar包进入,我的unity版本的jar包路径为D:\unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes
复制class.jar到工程的libs里,然后复制vuforia里面的两个jar包。unity工程打包apk,之后解压,复制里面的assets和libs里的armeabi-v7a到工程里
jar包选中右键添加到工程,然后修改mainactivity。
public class MainActivity extends UnityPlayerNativeActivity {
private LinearLayout scan;
protected UnityPlayer mUnityPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
// UnityPlayer.init()方法需要在将view附加到layout之前调用。它将会调用native code
mUnityPlayer.init(glesMode, trueColor8888);
scan=(LinearLayout)findViewById(R.id.scan);
View view = mUnityPlayer.getView();
scan.addView(view);
}
继承unity里的activity,然后oncreate里配置的全屏之类的东西,不是很懂
再之后修改xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.djzz.app.unitytest"
android:installLocation="preferExternal">
<!-- 权限请求开始 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<!-- 权限请求结束 -->
<uses-feature android:name="android.hardware.camera" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
android:persistent="true"
android:allowBackup="true">
<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" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity android:name=".SecondActivity"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:persistent="true"
android:allowBackup="true">
</activity>
<activity android:name="com.unity3d.player.VideoPlayer"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
<uses-library android:name="com.ti.s3d" android:required="false" />
<uses-library android:name="com.osterhoutgroup.api.ext" android:required="false" />
</application>
</manifest>
权限4个都要写,是vuforia必备的,不写能调起程序但是vuforia不好使,下面的activity是直接复制过来的videoplayer看着是红色的,感觉不是必要的,下面两个library的需要添加上,横竖屏要注意,接下来是互相的调用了。
先写unity调用java的
java代码
public void test(String name)
{
String str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + name;
System.out.println(str);
}
unity代码,传了一个参数
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("test","测试测试");
接下来是java调用unity
java代码,参数是gameobject物体名,方法名,方法参数
public void callUnityFunc(String _objName , String _funcStr, String _content)
{
UnityPlayer.UnitySendMessage(_objName, _funcStr, _content);
}
callUnityFunc("gameControl" , "test", "ceshi");
unity代码
public void test(string str){
text.text = str;
}