Unity 中的回调监听器 - 如何从 UnityPlayerActivity 在 Android 中调用脚本文件方法
2022-09-01 15:33:25
我有一个Android库项目,并在Unity项目中导入了库项目。现在,我想在Unity项目中实现一个回调,它将根据Android库项目给出的响应执行。我的意思是说,从UnityPlayerActivity(Android Project)调用Script File方法。
目前我正在使用下面的代码行,但没有任何反应:
UnityPlayer.UnitySendMessage("Main Camera","showMessage",errorMessage);
Main Camera
是我的游戏对象。 是脚本文件中的消息名称。 是将通过 Android Activity 在 Unity 中显示的消息。showMessage
Message
请检查我下面的代码Unity脚本文件和Android活动。
统一脚本文件:
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:
/**
* 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");
}
}
任何人都可以帮我摆脱这个问题吗?