Method instance of Unity synchronous and asynchronous call to Android
- 2021-12-13 17:18:48
- OfStack
2.2 Example 2
3 How to call an asynchronous method 3.1 Android End
3.1. 1 Adding dependencies
3.1. 2 Code Logic 3.2 Unity End
Summarize
1 How does Unity use the Android plug-in
Very simple, package the module of android into aar or jar, and put it into Assets/Plugin/Android!
2 How does the synchronization method call
2.1 Example 1
Android terminal
Suppose Android has one method
public static String GetVideoParams() {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(LOCAL_VIDEO_PATH);
String width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); // Width
String height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); // Gao
Log.w(TAG, "GetVideoParams, width " + width + " height " + height);
return width +"_" + height;
}
Unity terminal
Then the C # script for Unity can be written as follows:
AndroidJavaObject androidVideoPlugin = new AndroidJavaObject("com.iqiyi.cutgreenvideosdk.VideoPlugin");
String width_height = androidVideoPlugin.CallStatic<String>("GetVideoParams");
Where com. iqiyi. cutgreenvideosdk. VideoPlugin is the package name and class name of the Android method.
CallStatic
<
String
>
That is, the static method of a class is called and the return value is String. If the return value is Int or bool, it can be modified accordingly, such as CallStatic
<
bool
>
.
2.2 Example 2
Android terminal
Android has 1 method
public void start(Context context, int externalTextureId) {
....
}
This is no longer a static method, but you need to pass an context object.
Unity terminal
AndroidJavaObject androidVideoPlugin = new AndroidJavaObject("com.iqiyi.cutgreenvideosdk.VideoPlugin");
//get activity
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
androidVideoPlugin.Call("start", activity, 1);
Call is followed by the method name on the android side, along with the parameters.
3 How to call an asynchronous method
If you are asynchronous, you can do this:
Unity initiates the call, Android processes the call, and notifies Unity through a message.
Specifically. First of all, Java has a way to send messages to C #:
UnityPlayer.UnitySendMessage
How this class is called:
UnityPlayer. UnitySendMessage ("GameObjectName", "MethodName", "parameter to send");
The first parameter is the object name of Unity. Note that it is not the script name! ! !
The second parameter is the method name of a script mounted on the Unity object
The third parameter is the parameter of the method.
3.1 Android End
3.1. 1 Adding dependencies
First of all, we should rely on UnityPlayer. Where is it?
In your Unity installation directory, Editor\ Data\ PlaybackEngines\ AndroidPlayer\ Variations\ mono\ Release\ Classes
There is an classes. jar, take it out and put it in the Android project, under the libs directory of specific modules.
Then android gradle adds dependencies:
dependencies {
compileOnly files('libs/classes.jar')
}
Of course, if you are happy, you can also change the name of jar.
Note that compileOnly is required instead of implementation, otherwise, after packaging aar to Unity, the libs directory will also be packaged to aar, and finally Unity will fail to compile apk because there are duplicate classes. jar.
3.1. 2 Code logic
private static String mGameObject = "";
private static String mCallbackFunction = "";
public static void checkPermission(Context context, String gameObject, String callbackFunction) {
Log.i(TAG, "checkPermission gameObject " + gameObject + " callbackFunction " + callbackFunction);
mGameObject = gameObject;
mCallbackFunction = callbackFunction;
//TODO Do actual processing, such as requesting permissions
}
// After dealing with things, tell them by message Unity Results
private static void notifyResult(boolean permissionGranted) {
try {
// Parameter 1 , Script mounted gameObject Name, parameter 2 , Script method, parameter 3 , return value
Log.w(TAG, "notifyResult, permissionGranted " + permissionGranted + " mGameObject " + mGameObject + " mCallbackFunction " + mCallbackFunction);
UnityPlayer.UnitySendMessage(mGameObject, mCallbackFunction, permissionGranted ? "1" : "0");
getInstance().mContext = null;
} catch (Exception e) {
e.printStackTrace();
}
}
The above checkPermission is called by Unity. notifyResult is to process things and tell Unity the result through a message.
3.2 Unity end
The first is the method called:
private void checkPermission()
{
Debug.Log(TAG + "checkPermission");
AndroidJavaObject nativeObject = new AndroidJavaObject("com.iqiyi.cutgreenvideosdk.PermissionHelper");
nativeObject.CallStatic("checkPermission", activity, "AndroidVideoScreen", "onPermissionResult");//AndroiwdVideoScreen Objects mounted for scripts, onPermissionResult The name of the function that listens for callbacks for the script
}
Here, the name of the object on which the script is mounted, AndroidVideoScreen, is passed to Android. And the name of the callback function onPermissionResult for the script processing, also tell Android.
Next, we implement onPermissionResult, waiting to receive the callback:
public void onPermissionResult(String resultCode)
{
//TODO
}