【问题标题】:Unity Plugin for both iOS and Android Failing AOT for iOS due to AndroidJavaObject适用于 iOS 和 Android 的 Unity 插件由于 AndroidJavaObject 导致 iOS 的 AOT 失败
【发布时间】:2015-03-16 15:40:08
【问题描述】:

我正在尝试编写一个可在 iOS 和 Android 上运行的 Unity 插件。 我的插件有两个入口点,一个用于 iOS,另一个用于 Android。

问题是当我只使用 iOS 类并尝试从 Unity 构建 XCode 项目时,我收到此错误:

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

AndroidJavaObject 类仅用于统一脚本从不引用的 Android 类。

问题是我想提供一个适用于 iOS 和 Android 的插件。该插件已编译,因此我不能使用任何统一预处理器标志。

我该如何解决这个问题?

【问题讨论】:

    标签: android ios unity3d


    【解决方案1】:

    如果您需要访问本机 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 ();
    

    【讨论】:

    • 我最终使用了这个解决方案。在我的情况下,我将大部分插件代码编译到 DLL 中,并且我只留下了需要 #ifdef 作为 .cs 文件的部分。
    • 附带说明,如果您将 Unity 3D 放置在 Assets/Plugins/Android 下,我希望 Unity 3D 甚至不会在 iOS 构建中引用 Android 插件。这至少是文档所建议的:docs.unity3d.com/Manual/SpecialFolders.htmlwiki.unity3d.com/index.php/… 等。但它显然不能以这种方式工作,并且 iOS 引用了 Android 插件程序集。另请参阅此可能的相关帖子:forum.unity3d.com/threads/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2015-09-04
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多