就是本人做个笔记,防止忘记
一.
新建空androidstudio工程
二。 新建module
module名字随意 ,看个人喜好
三。在新建出的module中添加你的Unity的 classes.jar(获取途径:你自己的unity目录中【Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes】)
(添加到:你的module/libs中)
四,在androidstudio中将导入的jar引入。
在build.gradle中写入
compileOnly files('libs\\classes.jar')
因为导出aar时不需要将classes导入到aar中,所以我们使用compileOnly语法,而不是implementation.
五,新建属于你自己的activity,主要用于写一些原生的方法,或者SDK中控的内容,我用其中一个分享的原生方法举例。自行理解。
1.在你的类名下创建自己的java类,我起名WhiteActivity。
2.我直接将其继承到了UnityPlayerActivity,代码参考如下
package com.dulu.white;
public class WhiteActivity extends UnityPlayerActivity
{
}
3.此时很多同学就报错,因为他们根本没有UnityPlayerActivity可以继承,因为我们到带出aar到unity中使用,而且unity中是必然的有UnityPlayerActivity,所以我们可直接将UnityPlayerActivity导入的我们的包名路劲下(UnityPlayerActivity获取途径:你需要用自己的unity导出一份andorid工程,然后从unityLibrary\src\main\java\com\unity3d\player 此路劲下获取)
4.导入之后我们编写我们的原生方法到我们自建的activity中。例如原生的分享代码
package com.dulu.white;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class WhiteActivity extends UnityPlayerActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void Share(String code){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
//要分享的文本内容,选择某项后会直接把这段文本发送出去,相当于调用选中的应用的接口,并传参
shareIntent.putExtra(Intent.EXTRA_TEXT, "A Zombie Apocalypse FPS Game — Shoot, Run, and Survive as long as you can!My invitation code: "+code);
//需要使用Intent.createChooser,这里我们直接复用。第二个参数并不会显示出来
shareIntent = Intent.createChooser(shareIntent, "A Zombie Apocalypse FPS Game — Shoot, Run, and Survive as long as you can!My invitation code: "+code);
startActivity(shareIntent);
}
}
此时,我们基本上在androidstudio上的编辑算是完成了,我们开始将其带出到aar
5、导出aar到unity,先makemodule
等待make完成之后我们在路劲build/outputs/aar中即可看到对应的aar。
6.直接将其复制到unity中,路劲为(Assets\Plugins\Android\libs)
7.当然这个时候直接打包是无效的,因为程序在执行的时候需要知道开始执行那一条activity。这个时候我们就需要在AndroidManifest 告诉程序。在application 标签中添加
<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools">
<application>
<!--写你的路劲对应的activity名字,不要复制我的-->
<activity android:name="com.dulu.white.WhiteActivity"
android:theme="@style/UnityThemeSelector">
<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>
8,基本上ok了,我们写Unity调用java,即可测试!
9.打包测试