我在Unity中创建了一个回调,使我能够将结果从Android应用程序发送回Unity应用程序。我更喜欢UnitySendMessage,因为它更“干净”。
我使用了这里给出的确切示例:Callback Listener in Unity - How to call script file method from UnityPlayerActivity in Android
Unity c#code:
public class MoviePlayer : MonoBehaviour {
// Use this for initialization
void Awake () {
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
StartUpCallback startupCallback = new StartUpCallback();
AndroidJavaObject control = new AndroidJavaObject("vrbridge.Control", activity);
//control.Call("startUp");
control.Call("startUp", startupCallback);
}
class StartUpCallback: AndroidJavaProxy
{
public StartUpCallback() : base("vrbridge.StartUpCallback") { }
public void OnSuccess() { }
public void OnFailure() { }
}
}
Android代码:
public class Control implements LibraryManagerInitStartDialogFragment.ConfigurationFragmentListener{
String TAG = "QuickPlayTest";
private Activity _activity;
private StartUpCallback _startUpCallback;
public Control(){
Log.d(TAG, "Constructor.");
}
public Control(Activity activity){
Log.d(TAG, "Constructor. Activity = " + activity);
_activity = activity;
}
public void startUp(StartUpCallback startUpCallback){
Log.d(TAG, "startUp. Callback = " + startUpCallback);
_startUpCallback = startUpCallback;
new LibraryManagerInitStart(_activity, this);
Log.d(TAG, "startUp - end");
}
}
public interface StartUpCallback {
public void onSuccess();
public void onFailure();
}
由于某种原因,startup()中的参数startUpCallback始终为null。
我尝试将其转换为具有相同结果的构造函数。 OTOH,活动的价值还可以。
我错过了什么吗?
答案 0 :(得分:0)
问题可能是您的C#代理没有正确实现Java接口。请注意单词&#34; on / On&#34;中的大小写差异。