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.
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.
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);
}
}
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>
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();
}
...
}
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:
- 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
UPDATE
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
).
Here's what I read here:
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.
Adding to that, if I have multiple libraries the priority matches the dependency order (the order they appear in the Gradle dependencies block).
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 ?