Unity中的回调监听器 - 如何从Android中的UnityPlayerActivity调用脚本文件方法 [英] Callback Listener in Unity - How to call script file method from UnityPlayerActivity in Android

查看:114
本文介绍了Unity中的回调监听器 - 如何从Android中的UnityPlayerActivity调用脚本文件方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android库项目并在Unity项目中导入了库项目。现在,我想在Unity项目中实现一个回调,它将根据android库项目给出的响应执行。我的意思是说,来自UnityPlayerActivity的调用脚本文件方法(Android项目)。

I have an android library project and imported the library project in the Unity project. Now, I want to implement a callback in Unity project, which will execute according to the response given by the android library project. I mean to say, Call Script File method from UnityPlayerActivity (Android Project).

目前我在下面的代码行使用但没有任何反应:

Currently I am using below line of code but nothing happens:

UnityPlayer.UnitySendMessage("Main Camera","showMessage",errorMessage);

主摄像头是我的游戏对象。 showMessage 是脚本文件中的消息名称。
消息是将通过Android活动在Unity中显示的消息。

Main Camera is my Game Object. showMessage is message name in Script File. Message is message which will be displayed in Unity through Android Activity.

请检查我的以下代码Unity脚本文件和Android活动。

Please check my below code Unity Script File and Android Activity.

Unity脚本文件:

Unity Script File:

using UnityEngine;
using System.Collections;

public class scriptfile : MonoBehaviour {

    // Use this for initialization
    void Start () {


        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); 
        jo.Call("shareText","236","236");
    }

    void showMessage(string message){
        print ("hello");
        Debug.Log ("hello");
    } 
}

Android文件UnityPlayerActivity:

Android File UnityPlayerActivity:

/**
 * Created by CH-E01073 on 28-09-2015.
 */
public class MainAct extends UnityPlayerActivity implements RegistrationListener,BOffersListener {
    Context context;
    SharedPreferences prefs ;
    String AppIds="";
    String PublisherIDs="";
     public void shareText(String AppId,String PublisherID) {
       context=MainAct.this;
        prefs = PreferenceManager
               .getDefaultSharedPreferences(context);
       Log.e("AppID", AppId);
       Log.e("PublisherID",PublisherID);

        AppIds=AppId;
        PublisherIDs=PublisherID;

         runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 UnityPlayer.UnitySendMessage("Main Camera","showMessage","Start UI Thread");
                 if (prefs.getString(FreeBConstants.ID, null) == null
                         || prefs.getString(FreeBConstants.ID, null).equals("")
                         || !Build.VERSION.RELEASE.equals(prefs.getString(
                         FreeBConstants.VERSION, null))
                         || !FreeBCommonUtility.getDeviceId(context).equals(
                         (prefs.getString(FreeBConstants.DEVICE_ID, null)))) {
                BSDKLogger.enableLogging(true);
                SDKRegistration.initialize(MainAct.this, getApplicationContext(), AppIds,PublisherIDs);
                 }else{

                Offers Offers = new Offers(MainAct.this);
                 Offers.setOnFreeBOffersListener(MainAct.this);
                 Offers.setTitle(
                         "Pick Any Offer to unlock your premium features",
                         "#FFFFFF", "#FF6D00");
                 }
         }
         });



    }

    @Override
    public void onOffersLoaded(String code,String freeBOffers) {
        CommonUtility.showToast(getApplicationContext(), code);
        UnityPlayer.UnitySendMessage("Main Camera","showMessage",freeBOffers);
    }

    @Override
    public void onShowOffers() {

         UnityPlayer.UnitySendMessage("Main Camera","showMessage","Show Offers");
    }

    @Override
    public void noOfferInstalled(String s, String s2) {
    }

    @Override
    public void onLeaveApplication(String s, String s2) {
    }

    @Override
    public void onDialogDismiss(String s) {
    }

    @Override
    public void onOffersFailed(String code, String errorMessage) {

        FreeBCommonUtility.showToast(getApplicationContext(), errorMessage);
        UnityPlayer.UnitySendMessage("Main Camera","showMessage",errorMessage);
    }

    @Override
    public void onOffersInstallSuccess(String code, String errorMessage) {
         FreeBCommonUtility.showToast(getApplicationContext(), errorMessage);
    }

    @Override
    public void onOffersInstallFailure(String code, String errorMessage) {
         FreeBCommonUtility.showToast(getApplicationContext(), errorMessage);
    }


    @Override
    public void onRegistrationFailed(String code, String errorMessage) {
        FreeBCommonUtility.showToast(getApplicationContext(), errorMessage);
        UnityPlayer.UnitySendMessage("Main Camera","showMessage",errorMessage);
    }

    @Override
    public void onRegistrationSuccess(String code, String errorMessage) {
      // FreeBCommonUtility.showToast(getApplicationContext(), errorMessage);
        Log.e("SUCCESS", errorMessage);
        // TODO Auto-generated method stub
        UnityPlayer.UnitySendMessage("Main Camera","showMessage",errorMessage);

        Offers Offers = new Offers(MainAct.this);
        Offers.setOnFreeBOffersListener(MainAct.this);
       Offers.setTitle(
             "Pick Any Offer to unlock your premium features",
              "#FFFFFF", "#FF6D00");
    }
}

任何人都可以帮我摆脱这个问题吗?

Can anyone help me to get rid of this issue?

推荐答案

另一种选择是使用 AndroidJavaProxy 实现接口回调。您可以在Java代码中简单地使用Unity回调,然后在C#中使用 AndroidJavaProxy 并将其传递给Java方法以便接收消息。

Another option will be to implement an interface callback using AndroidJavaProxy. Instead of using UnitySendMessage, you can simply have an Interface callback in your java code and then implement this interface in C# using AndroidJavaProxy and pass it to the Java method in order to receive messages back.

创建Java界面:

package com.example.android;
public interface PluginCallback {
    public void onSuccess(String videoPath);
    public void onError(String errorMessage);
}

调用传递的侦听器/回调以返回消息

public void myPluginMethod(PluginCallback callback) {
    // Do something
    callback.onSuccess("onSuccess");
    // Do something horrible
    callback.onError("onError");
}

在C#中实现界面

class AndroidPluginCallback : AndroidJavaProxy
    {
        public AndroidPluginCallback() : base("com.example.android.PluginCallback") { }

        public void onSuccess(string videoPath) {
            Debug.Log("ENTER callback onSuccess: " + videoPath);
        }
        public void onError(string errorMessage)
        {
            Debug.Log("ENTER callback onError: " + errorMessage);
        }
    }

将C#接口传递给Java方法

AndroidJavaObject pluginClass = new     AndroidJavObject("com.example.android.MyPlugin");
pluginClass.Call("myPluginMethod", new AndroidPluginCallback());

这篇关于Unity中的回调监听器 - 如何从Android中的UnityPlayerActivity调用脚本文件方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆