Interaction between Android and Unity--access Android native SDK

Due to technical updates, I found that the previous articles were a bit outdated, and some places could not keep up with the current progress, resulting in a lot of problems in the development, so I sorted it out with reference to some previous articles, hoping to help everyone.

tool

  • Unity2020
  • AndroidStudio 2021

AndroidStudio operation

1. Still create a new empty project, click Next.
insert image description here
2. Set the project name and package name according to your own needs, remember the API level of MinSDK here, and then set it in sync with Unity when it is released. Then click Finish.
insert image description here
3. Created and that's it. Click the Android drop-down list and select Project. These two can be deleted, which is useless (I don't know if it is useful), but deleting it has no effect.
insert image description here
4. Then find the build.gradle file and double-click to open it. Change arrow 1 to: id 'com.android.library'. Arrow 2 points to delete. Click Sync Now to compile.
insert image description here
5. In the Unity engine installation directory, there are two folders il2cpp and mono in Editor\Data\PlaybackEngines\AndroidPlayer\Variations, select according to the language compiled in Unity, enter the folder and find the jar package in Release/Classes, Copy it to the libs folder in the Android studio project. Right-click the jar package and add it to dependencies.
insert image description here
6. After Unity2019 (it seems to be), the jar package does not contain the UnityPlayerActivity.java class, so you have to add it manually, return to the previous level in the mono folder, and find the Source folder to get the file. Then copy it to the com folder.
insert image description here
7. Then double-click the MainActivity class to open it, and modify the base class to inherit from UnityPlayerActivity.

package com.wsetjin.monkey;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import com.unity3d.player.UnityPlayerActivity;

public class MainActivity extends UnityPlayerActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);注释掉这一行
    }

    public int CallBackSurprise(){
    
    
        return 666;
    }
    
    public String GetDeviceId(){
    
    
        String deviceId = null;
        String android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
        //deviceId = getDeviceId();
       return android_id;
    }
public String GetUUID()
    {
    
    
        String serial = "";
        try {
    
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
    
                serial = Build.getSerial();
            } else {
    
    
                serial = Build.SERIAL;
            }
            //serial = Build.class.getField("SERIAL").get(null).toString();
            //return serial;
        } catch (Exception exception) {
    
    
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
            //return serial;
        }
        String m_szDevIDShort = "28" +
                Build.BOARD.length()%10+ Build.BRAND.length()%10 +

                Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +

                Build.DISPLAY.length()%10 + Build.HOST.length()%10 +

                Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +

                Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +

                Build.TAGS.length()%10 + Build.TYPE.length()%10 +

                Build.USER.length()%10 ; //13 位
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }
}

8.ok is not far from success, and then modify AndroidMainfest.xml. I forgot to add a line of code in the picture, add it below the end of the intent-filter node.

 <meta-data android:name="unityplayer.UnityActivity" android:value="true"/> 

insert image description here
9. Finally – select the menu bar Build->Make Moudle'your project name', a build folder will be created in the project after the build is completed, select the jar package in intermediates/aar_main_jar, open it with decompression software, and delete the unity3d folder inside , to avoid duplication when unity is released. Then import it into unity. Import AndroidMainfest.xml into unity as well.

10. The Android studio operation is over.


Operation of Unity

  • To create a script, I simply write a test script.
	void Start()
    {
    
    
		AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
		AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
		int value = jo.Call<int>("CallBackSurprise");
	}

Finally publish and you're done

Guess you like

Origin blog.csdn.net/qq_39556084/article/details/126261789