Development Handbook: Interaction between Unity and Android (Unity2019+)

      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
insert image description here
      2. AndroidStudio4.1.1
insert image description here
      Specific process:
      1. Create a new AS empty project
insert image description here      2. Set basic information
insert image description here
      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
insert image description here
      4. Copy the classes.jar file to libs
insert image description here       Copy the classes.jar file of Unity2019 to the as project libs
insert image description here
      5. Copy the UnityPlayerActivity.java file to the src directory
insert image description here       Copy UnityPlayerActivity.java to the as project src directory
insert image description here

      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
insert image description here
      the upper right corner of the as window, click the synchronization project,
insert image description here
      select classes.jar, right-click AddAsLibrary,
      then we can officially start writing code

      7. UnityPlayerActivity add package name
insert image description here
      Open the UnityPlayerActivity add package as follows:

import com.unity3d.player.IUnityPlayerLifecycleEvents;
import com.unity3d.player.UnityPlayer;

      7. MainActivity starts to write code
insert image description here
      to inherit UnityPlayerActivity, press shift+alt+enter to import the package and
insert image description here
      start to write the android functions required by my unity project.
insert image description here
      The specific android functions need to be directly Baidu, here is only a demonstration.

      8. Modify the build.gradle configuration
insert image description here       to:
insert image description here
      change application to library, and comment applicationId "com.Bsti"

      9. Modify the androidmanifest.xml configuration
insert image description here       to:
insert image description here
      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
insert image description here
      , click the upper right corner of the as window to synchronize the project,
insert image description here
      click rebuild project to build the aar file

insert image description here
      The aar file is built

      11. Import the unity project

insert image description here
      Use compression software to open the aar file, just copy these two files to Assets\Plugins\Android
insert image description here

      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
insert image description hereinsert image description here
      and then package

insert image description here       Install to mobile phone, test:
insert image description here
      ok, this is the complete interaction process.

Guess you like

Origin blog.csdn.net/yinhun2012/article/details/124455611