我有一个Java对象,它获取一个侦听器对象作为参数。该侦听器应实现某个java抽象类。我试图阻止用Java编写此代码,因为我使用jar文件中附带的SDK,并且要从java文件中调用jar文件,因此我需要创建一个包含两者的jar文件(请参见)这个答案解释了我的错误,但没有给出解决方案。Unity3D with multiple jars (android jar + pure java lib)我将尝试更详细:我有Listener类(位于jar文件中):public abstract class AttachCallback { public AttachCallback(); public void onAttached(Sdk sdk);}我的C#代码当前(并且不起作用):public class AttachCallback : AndroidJavaProxy{ public AttachCallback() : base("com.example.AttachCallback") { } public void onAttached(AndroidJavaObject sdk) { Debug.Log("-----Attached------"); }}目前,我得到java.lang.IllegalArgumentException: com.example.AttachCallback is not an interface那么,有没有办法做到这一点?提前致谢,查姆 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 首先:阅读此article。 Android Java代理只能实现接口。因此,您应该在Java中创建接口。就像是:public interface IAttachable{ public void onAttached(Sdk sdk);}在AttachCallback类中,您可以添加以下接口:public abstract class AttachCallback implements IAttachable { public IAttachable unityCallback; public void onAttached(String sdk) { unityCallback.onAttached(sdk); }}然后在您的统一类中,创建JavaProxypublic class AttachCallback : AndroidJavaProxy{ public AttachCallback() : base("com.example.IAttachable") { } public void onAttached(AndroidJavaObject sdk) { Debug.Log("-----Attached------"); }}为了附加您的Unity Proxy,您应该将AttachCallback传递给java,例如:public void AddAttachToJava(){ AttachCallback callback = new AttachCallback(); //Passing to activity, but you can do whatever you want using (AndroidJavaClass javaClass = new AndroidJavaClass("your activity class name")) { using (AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("mContext")) { activity.Call("attachUnityCallback", callback); } }}活动中的java方法应如下所示:public void attachUnityCallback(IAttachable attachable){ // if AttachCallback is created attachCallback.unityCallback = attachable; attachCallback.onAttached(sdk);} (adsbygoogle = window.adsbygoogle || []).push({});
10-07 15:52