【问题标题】:Can't access R.class from Unity Android project无法从 Unity Android 项目访问 R.class
【发布时间】:2014-01-18 02:58:55
【问题描述】:

我有一个 Android 库,我现在正在为其构建一个 Unity3D 插件。 Android 库对 R.Styleable 类执行反射,以检索“declare-styleable”资源上的数据,从而实现类似于 this 的功能。

第一步如下:

Field[] fields = Class.forName( context.getPackageName() + ".R$styleable" ).getFields();

当库在 Eclipse 中作为本机 Android 项目的一部分运行时,上述行执行没有问题。但是,当通过 Unity Android 应用程序运行时,它会引发 NoClassDefFoundError。 (如果您将上面的“.R&$styleable”简单地替换为“.R”,也会发生同样的情况)。

我读过很多关于 Unity 不生成 R.class 文件的说法。这是真的?我知道至少在 Unity 的“StagingArea/gen”文件夹中生成了一个 R.java 文件。如果是这样,除了通过 Context.getResources() 之外,没有其他方法可以动态访问 Unity Android 项目中的资源吗?

【问题讨论】:

    标签: android unity3d


    【解决方案1】:

    你可以试试下面的代码。

    final R.drawable drawableResources = new R.drawable();
            final Class<R.drawable> c = R.drawable.class;
            final Field[] fields = c.getDeclaredFields();
            int resourceId;
            for (Field field : fields) {
                try {
                    resourceId = field.getInt(drawableResources);
                    //TODO do something
                } catch (Exception e) {
                    continue;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多