java Unity 中的回调侦听器 - 如何从 Android 中的 UnityPlayerActivity 调用脚本文件方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32944478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Callback Listener in Unity - How to call script file method from UnityPlayerActivity in Android
提问by user1986760
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).
我有一个android库项目并在Unity项目中导入了库项目。现在,我想在Unity项目中实现一个回调,它会根据android库项目给出的响应来执行。我的意思是说,从 UnityPlayerActivity(Android 项目)调用脚本文件方法。
Currently I am using below line of code but nothing happens:
目前我正在使用下面的代码行,但没有任何反应:
UnityPlayer.UnitySendMessage("Main Camera","showMessage",errorMessage);
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.
Main Camera
是我的游戏对象。showMessage
是脚本文件中的消息名称。
Message
是将通过 Android Activity 在 Unity 中显示的消息。
Please check my below code Unity Script File and Android Activity.
请检查我下面的代码 Unity 脚本文件和 Android 活动。
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 File UnityPlayerActivity:
Android 文件 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?
谁能帮我摆脱这个问题?
回答by velval
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 AndroidJavaProxyand pass it to the Java method in order to receive messages back.
另一种选择是使用AndroidJavaProxy实现接口回调。除了使用 UnitySendMessage,您可以简单地在 Java 代码中使用接口回调,然后使用AndroidJavaProxy在 C# 中实现此接口并将其传递给 Java 方法以接收消息。
Create your Java interface:
创建您的 Java 接口:
package com.example.android;
public interface PluginCallback {
public void onSuccess(String videoPath);
public void onError(String errorMessage);
}
Call the passed listener/callback to return messages
调用传入的监听器/回调函数返回消息
public void myPluginMethod(PluginCallback callback) {
// Do something
callback.onSuccess("onSuccess");
// Do something horrible
callback.onError("onError");
}
Implement the interface in C#
用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);
}
}
Pass the C# interface to the Java method
将 C# 接口传递给 Java 方法
AndroidJavaObject pluginClass = new AndroidJavaObject("com.example.android.MyPlugin");
pluginClass.Call("myPluginMethod", new AndroidPluginCallback());
回答by peterept
I believe you are only allowed to call UnitySendMessage()from the main thread - at least in one scenario above you are calling it from the Android UI worker thread.
我相信您只能从主线程调用UnitySendMessage()- 至少在上述一种情况下,您是从 Android UI 工作线程调用它的。
As a quick sanity test, try calling it before you right at the top of your shareText()function.
作为一个快速的健全性测试,尝试在你的shareText()函数的顶部调用它。