iOS 和 Android 的 Unity 插件因 AndroidJavaObject 而导致 iOS 的 AOT 失败

Unity Plugin for both iOS and Android Failing AOT for iOS due to AndroidJavaObject

我正在尝试编写一个适用于 iOS 和 Android 的 Unity 插件。 我的插件有两个入口点,一个 class 用于 iOS,另一个用于 Android.

问题是,当我仅使用 iOS class 并尝试从 Unity 构建 XCode 项目时,出现此错误:

无法加载class UnityEngine.AndroidJavaObject,在UnityEngine中使用

AndroidJavaObject class 仅在 Android class 中使用,unity 脚本永远不会引用它。

问题是我想提供一个插件,它将 "just work" 同时用于 iOS 和 Android。该插件已编译,因此我无法使用任何统一预处理器标志。

我该如何解决这个问题?

如果您需要访问本机 API,我认为除了条件编译之外别无选择。我建议使用接口来封装插件访问,例如:

public interface IMyPlugin
{
    void Do ();
}

public class MyIOSConnector : IMyPlugin
{
    public void Do () {
#if UNITY_IOS
        // iOS implementation
#endif
    }
}
public class MyAndroidConnector : IMyPlugin
{
    public void Do () {
#if UNITY_ANDROID
        // Android implementation
#endif
    }
}
public static class MyPlugin
{
    static IMyPlugin _myPlugin = null;

    public static IMyPlugin Access {
        get { 
            if (_myPlugin == null) {
#if UNITY_EDITOR
                // maybe this needs special handling e.g. a dummy implementation
#endif
                switch (Application.platform) {
                case RuntimePlatform.IPhonePlayer:
                    _myPlugin = new MyIOSConnector ();
                    break;
                case RuntimePlatform.Android:
                    _myPlugin = new MyAndroidConnector ();
                    break;
                default:
                    break;
                }
            }
            return _myPlugin;
        }
    }
}

来自其他 类 你打电话给:

MyPlugin.Access.Do ();