1. Create an AndroidStudio project
Note the following Package Name
2. Enter the Android Studio project
Select the Project Flies option and find app-> src-> main-> Next is your own folder-
find the last folder mine is unitytoandroid Right click to create a Java class (if there is a Java class here-it may be your AndroidStudio Before downloading, see if AndroidStudio is downloaded in the lower right corner.) After downloading, you can create a Java class.
Name it yourself
3. Start writing code
4. Packed to the most important step
(1)-find the build.gradle file to open
(2) -It is like this after opening
(3) -Modified to the following figure
apply plugin: 'com.android.library' 加入
task makeJar(type: Copy) {
//删除存在的
delete 'build/libs/Test.jar'
//设置拷贝的文件
from('build/intermediates/aar_main_jar/release/')
//打进jar包后的文件目录
into('build/libs/')
//将classes.jar放入build/libs/目录下
//include ,exclude参数来设置过滤
//(我们只关心classes.jar这个文件)
include('classes.jar')
//重命名
rename ('classes.jar', 'Test.jar')
}
makeJar.dependsOn(build)
(3) -After editing, click the Sync Now button in the upper right corner
(4) -After waiting for the run to succeed, we can see that there is a play button in front of the task method and click the play button to run
(5) -After the operation is over
You can see that there is an extra build folder (if it runs successfully-there is no such folder-go to the app directory to create a build / libs folder)
to find the directory, copy this file and copy it to the Unity Plugins / Android / libs / under
5. Create a script
using UnityEngine;
using UnityEngine.UI;
//在电脑上运行可能出不来效果 需要打包到手机上测试
public class TestUnity : MonoBehaviour
{
public Text text;
public AndroidJavaObject androidJavaobject;
private void Start()
{
androidJavaobject = new AndroidJavaObject("imsdk.u3d.unitytoandroid.Test");//这个是androidStudio创建的包名加上自己创建的脚本
}
public void intClick()
{
text.text = androidJavaobject.Call<int>("testInt").ToString();
Debug.Log(androidJavaobject.Call<int>("testInt"));
}
public void stringClick()
{
text.text = (androidJavaobject.Call<string>("testString"));
Debug.Log(androidJavaobject.Call<string>("testString"));
}
public void setStringClick()
{
text.text = androidJavaobject.Call<string>("testSetString", "sss");
Debug.Log(androidJavaobject.Call<string>("testSetString", "sss"));
}
}