Monday, October 17, 2016

unity calls method in android plugin

for non-static method in android plugin return void

Calls a Java method on an object (non-static).
jo.Call("setToDefaults");

for non-static method in android plugin return int
        AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some string");

        int hash = jo.Call<int>("hashCode");

for static method in android plugin

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");


AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
jc.CallStatic("UnitySendMessage", "Main Camera", "JavaMessage", "whoowhoo");



AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
pluginClass.CallStatic("initialize", context);
    static public void initialize (Context unityContext){
        try {
            MyApp.initialize(unityContext);
        } catch (NullPointerException e){
            Log.d("UnityPlugin", "Null PTR Exception");
            e.printStackTrace();
        }
    }
 void OnDestroy() {
        int ret;
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
        AndroidJavaClass plugIn = new AndroidJavaClass("com.ebmajor.plugin.MyReceiver");
        ret = plugIn.CallStatic<int>("sendExitEvent", context, 10);
        Debug.LogError("android lib: sendExitEvent returned: " + ret);
    }

public static int sendExitEvent(Context context, int val) {
    Intent intent = new Intent();
    intent.setAction("com.ebmajor.ACTION");
    context.sendBroadcast(intent);
    return 1;
}
=========================================================================================

==========================================================================================
android plug-in under Unity
==========================================================================================

package com.le.androidlib;
public class MyActivity extends UnityPlayerActivity {
    static public String getArgs() {
        return uri_arg;
    }
}

==========================================================================================
Unity.cs
==========================================================================================

void Awake()
{
        AndroidJavaObject jo = new AndroidJavaObject("com.le.androidlib.MyActivity");
        string arg = jo.CallStatic<string>("getArgs"); //string return
        Debug.Log("MediaPlayer: arg: " + arg);
        jo.CallStatic<int>("getArgsInt"); //int return
        jo.CallStatic("flushPendingCommands"); //void return
}
==========================================================================================

No comments:

Post a Comment