AndroidStudio2.0
Unity4.68
Module选择Android Library,起名为unitylib
把unity classes.jar直接放入新建module文件下的libs中
把classes.jar引入,具体过程为
选择unitylib,点击+号,确定引入了classes.jar后点击ok保存,退出Project Structure界面
想删除新建的module,需要在ProjectStructure中先点-号,然后回到把unitylib delete调,然后在回到工程中把unitylib整个delete掉
这里要打包一个MainActivity,所以在module中新建一个MainActivity.Java
MainActivity内容为
[java] view plain copy package com.n.unitylib; import android.app.Activity; import android.content.res.Configuration; import android.graphics.PixelFormat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; import com.unity3d.player.UnityPlayer; public class MainActivity extends Activity { protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code // Setup activity layout @Override protected void onCreate (Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); getWindow().takeSurface(null); getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy mUnityPlayer = new UnityPlayer(this); if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true)) getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(mUnityPlayer); mUnityPlayer.requestFocus(); } // Quit Unity @Override protected void onDestroy () { mUnityPlayer.quit(); super.onDestroy(); } // Pause Unity @Override protected void onPause() { super.onPause(); mUnityPlayer.pause(); } // Resume Unity @Override protected void onResume() { super.onResume(); mUnityPlayer.resume(); } // This ensures the layout will be correct. @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mUnityPlayer.configurationChanged(newConfig); } // Notify Unity of the focus change. @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); mUnityPlayer.windowFocusChanged(hasFocus); } // For some reason the multiple keyevent type is not supported by the ndk. // Force event injection by overriding dispatchKeyEvent(). @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MULTIPLE) return mUnityPlayer.injectEvent(event); return super.dispatchKeyEvent(event); } // Pass any events not handled by (unfocused) views straight to UnityPlayer @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); } /*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); } public static void Test() { Log.e("nafio","success!"); } } AndroidManifest.xml内容为
[html] view plain copy <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.n.unitylib" 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="@android:style/Theme.NoTitleBar.Fullscreen" android:label="@string/app_name" > <activity android:name=".MainActivity" android:label="@string/app_name"> <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> </manifest>
在module的build.gradle最后添加
[html] view plain copy task makeJar(type: Copy) { delete 'build/libs/unitylib.jar' from('build/intermediates/bundles/release/') into('build/libs/') include('classes.jar') rename('classes.jar', 'unitylib.jar') } makeJar.dependsOn(build)
然后在Terminal中输入gradlew makejar
第一次运行Androidstudio可能会更新gradle,20-30分钟,更新完会自动打jar包
jar包位置在
module用来打包,如果要调试
把module中的build.gradle中
apply plugin: 'com.android.library' 改为 apply plugin: 'com.android.application'然后同步下gradle module就是普通工程了
遇到问题记录
Lint found errors in the project; aborting build 解决办法 文件build.gradle,添加如下信息 android { lintOptions { abortOnError false } }