将两个Unity项目导入Android Studio无法正常工作 [英] Importing two Unity projects into Android Studio doesn't work as expected

查看:349
本文介绍了将两个Unity项目导入Android Studio无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在训练使使用Unity添加一些功能(AR,VR等)的应用程序.目前,我一直在使用Android Studio和Android进行开发,一旦完成,我将在iOS上进行培训.

I am currently training to make applications which use Unity to add some features (AR, VR, etc...). For now I've been working on Android, with Android Studio, and once I'll be done with I'll train on iOS.

我的目标很简单:我的MainActivity显示两个按钮,每个按钮调用一个单独的Unity项目(从Unity导出为Google Android项目)以启动其场景.

My aim is simple: my MainActivity displays two buttons, each one calls a separate Unity project (exported from Unity as Google Android project) to launch its scene.

为此,我将这两个Unity项目Scene1Scene2作为库导入,并通过开始它们的活动来调用它们(请参见下面的代码).

To do so, I imported those two Unity projects Scene1 and Scene2 as libraries and I call them by starting their activities (see the code below).

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void goToUnity1(View v){
        Intent intent = new Intent(this, com.example.unityscene1.UnityPlayerActivity.class);
        startActivity(intent);
    }

    public void goToUnity2(View v){
            Intent intent = new Intent(this, com.example.unityscene2.UnityPlayerActivity.class);         
            startActivity(intent);

     }
}

及其AndroidManifest.xml:

And its AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multipleunity">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.example.unityscene1.UnityPlayerActivity"></activity>
    <activity android:name="com.example.unityscene2.UnityPlayerActivity"></activity>


</application>

Scene1Scene2中的UnityPlayerActivity文件是由Unity生成的,因此它们是相似的(这是它们的onCreate方法):

The UnityPlayerActivity files in Scene1 and Scene2 are generated by Unity so they are similar (here are their onCreate method):

public class UnityPlayerActivity extends Activity {
    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code


    // Setup activity layout
    @Override protected void onCreate (Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

        mUnityPlayer = new UnityPlayer(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

    ...
}

代码编译没有问题,应用程序启动并通过其两个按钮显示MainActivity.当我单击第一个按钮时,它将按预期启动第一个Unity场景.但是,当我单击第二个按钮时,它还会启动第一个Unity场景,而不是第二个.

The code compiles without any problem, the application launches and displays the MainActivity with its two buttons. When I click on the first button, it launches the first Unity scene as I expected. However, when I click on the second button it also launches the first Unity scene instead of the second one.

所以我试图理解为什么会发生这种情况,这是到目前为止我能说的:

So I tried to understand why this happens and here is what I can tell so far:

  • 我没有错误地将同一个Unity项目放入两次
  • 在将Scene2转换为库之前,它可以很好地运行
  • 单击第二个按钮时会调用活动com.example.unityscene2.UnityPlayerActivity
  • I didn't put twice the same Unity project by mistake
  • Before transforming Scene2 into library it works well on its own
  • The activity com.example.unityscene2.UnityPlayerActivity is called when I click on the second button

更新

花了几个小时后,我确定问题出在我在Google Android项目中导出时Unity始终提供的资源名称(unity-classes.jar).

After spending hours on this I'm sure the problem comes from the name of the ressources Unity delivers when I exported in Google Android project which are always the same (unity-classes.jar).

这是我在此处阅读的内容:

Android开发工具将库项目的资源与应用程序项目的资源合并.如果多次定义了资源的ID,则工具会从应用程序或优先级最高的库中选择资源,然后丢弃其他资源.

The Android development tools merges the resources of a library project with the resources of the application project. In the case that a resource’s ID is defined several times, the tools select the resource from the application, or the library with highest priority, and discard the other resource.

此外,如果我有多个库,则优先级与依赖关系顺序(它们在Gradle依赖关系块中出现的顺序)相匹配.

Adding to that, if I have multiple libraries the priority matches the dependency order (the order they appear in the Gradle dependencies block).

因此,我试图颠倒依赖关系的顺序,将第二个Unity项目放在第一个Unity项目之前,并且可以预期这两个按钮都会启动第二个Unity场景.

So I tried to reverse the order of the dependencies, I put the second Unity project before the first one and as that was expected both buttons launch the second Scene of Unity.

现在我知道,是否有办法避免此名称冲突?

Now I know that, is there a way to avoid this name conflict ?

推荐答案

我遇到的一个可能的解决方案是在单个apk内动态加载多个较小的unity apk.我进行了一些研究,发现可以从另一个启动一个apk,而无需实际安装apk.这可以通过各种方法来完成,并且有许多可用于执行此操作到.他们很少.

One possible solution which i came across is dynamically loading multiple smaller unity apks inside a single apk . I have done some research and found out that it is possible to launch one apk from other without even actually installing the apk. This can be done by various methods and there are many library available for doing this to. Few of them are.

DexClassLoader 类加载器,它从包含classes.dex条目的.jar和.apk文件加载类.这可用于执行未作为应用程序一部分安装的代码.

DexClassLoader class loader that loads classes from .jar and .apk files containing a classes.dex entry. This can be used to execute code not installed as part of an application.

Grab-n-Run 将安全的代码从APK容器或JAR库动态地加载到Android应用程序中,这些容器可以转换为Dalvik虚拟机(DVM)和Android运行时(ART)可执行的文件.

Grab-n-Run Securely load code dynamically into your Android application from APK containers or JAR libraries translated to be executable by both the Dalvik Virtual Machine (DVM) and the Android Runtime (ART).

这篇关于将两个Unity项目导入Android Studio无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆