It is necessary to adjust the native functions of android again, this time using the unity2019+ version and the as4+ version. It is different from the previous Unity2018+AS3+ interaction , so record it again.
I will write the operation in detail, and the logo will be very detailed.
Preparation tools:
1. Unity2019.4.22f1
2. AndroidStudio4.1.1
Specific process:
1. Create a new AS empty project
2. Set basic information
Here we only need to configure com.CompanyName, and select the minimum api26 I need (according to your own needs)
3. Expand the project directory
4. Copy the classes.jar file to libs
Copy the classes.jar file of Unity2019 to the as project libs
5. Copy the UnityPlayerActivity.java file to the src directory
Copy UnityPlayerActivity.java to the as project src directory
This step is a very critical step, because the new version of unity classes.jar file no longer contains the UnityPlayerActivity class, so it must be manually copied to the as project.
6. Synchronize the as project and add library references in
the upper right corner of the as window, click the synchronization project,
select classes.jar, right-click AddAsLibrary,
then we can officially start writing code
7. UnityPlayerActivity add package name
Open the UnityPlayerActivity add package as follows:
import com.unity3d.player.IUnityPlayerLifecycleEvents;
import com.unity3d.player.UnityPlayer;
7. MainActivity starts to write code
to inherit UnityPlayerActivity, press shift+alt+enter to import the package and
start to write the android functions required by my unity project.
The specific android functions need to be directly Baidu, here is only a demonstration.
8. Modify the build.gradle configuration
to:
change application to library, and comment applicationId "com.Bsti"
9. Modify the androidmanifest.xml configuration
to:
Red is the required item, and sky blue is the added permission (according to the specific needs of your own functions)
10. Synchronize the project, build the project
, click the upper right corner of the as window to synchronize the project,
click rebuild project to build the aar file
The aar file is built
11. Import the unity project
Use compression software to open the aar file, just copy these two files to Assets\Plugins\Android
12. Writing c# code call
still uses AndroidJavaClass, AndroidJavaObject to call, as follows:
private void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 200, 200), "Toast"))
{
AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity");
ajo.Call("ShowToast", "Unity Message");
}
if (GUI.Button(new Rect(300, 0, 200, 200), "Lanuch"))
{
AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity");
ajo.Call("LanuchApp", "com.Bsti.WS1");
}
}
Make sure that the method name string does not make mistakes.
13. Package unity apk test
release configuration
and then package
Install to mobile phone, test:
ok, this is the complete interaction process.