Unity Android Tutorial- Opening unity scene from an activity

In this tutorial we are going to open our unity scene from a native activity. To accomplish that we just need to do the exactly same steps to make an android plugin by overriding unityplayer activity, and with a slightly change we are going to change our launching activity with a native one.

Here are the steps;

  • First creating a library project that we will override UnityPlayerActivity.
  • Then we will add two activities. MainActivity(native launcher activity) and GameActivity(UnityPlayerActivity)
  • Then we will add a layout to MainActivity(right now with a simple button to open Unity scene).
  • Adding android manifest to Unity project and export as Google Android Project.
  • Importing exported project into Eclipse and testing.

 

  • Creating android library project

    Like in my previous tutorials we are going to make a library project on Eclipse.

  1. Create a new project(name it whatever you want),add your package name (mine is” com.nexxmobile.openunityscene”).
  2. Mark it as a library and add your java classes(MainActivity,GameActivity) under that package name.
  3. Add “unityclasses.jar” as an external jar.(It is found in the installation folder (usually C:\Program Files\Unity\Editor\Data (on Windows) or/Applications/Unity (on Mac)) in a sub-folder called PlaybackEngines/AndroidPlayer/bin.)

1

2

Next add a layout for our MainActivity. I named it “main_activity.xml”.

3

Here is the code for mainactivity and gameactivity:

public class MainActivity extends Activity{
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_activity);

      Button unityButton = (Button) findViewById(R.id.button1);
      unityButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        startActivity(new Intent(MainActivity.this,GameActivity.class));
        }
      });
    }
}
public class GameActivity extends UnityPlayerActivity {
    @Override
    protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
      super.onCreate(arg0);
      Log.d("Unity", "Unity started");
    }
}

That’s all for our java side.

  • Unity Part

For unity part, we just need to add our manifest into /Plugins/Android folder and export our project as Google Android Project. Here is our androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.nexxmobile.openunityscene"
 android:installLocation="preferExternal"
 android:theme="@android:style/Theme.NoTitleBar"
 android:versionCode="1"
 android:versionName="1.0" >

 <supports-screens
 android:anyDensity="true"
 android:largeScreens="true"
 android:normalScreens="true"
 android:smallScreens="true"
 android:xlargeScreens="true" />

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 <application
   android:icon="@drawable/app_icon"
   android:label="@string/app_name" >
   <activity
     android:name="com.nexxmobile.openunityscene.MainActivity">
     <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
   </activity>
   <activity
     android:name="com.nexxmobile.openunityscene.GameActivity"
     android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
     android:label="@string/app_name"
     android:launchMode="singleTask" >
   </activity>
 </application>

 <uses-sdk
 android:minSdkVersion="9"
 android:targetSdkVersion="21" />

 <uses-feature android:glEsVersion="0x00020000" />
</manifest>

As you can see from the manifest our launcher activity is MainActivity not our GameActivity!

  • Final Eclipse Part

After exporting as Google Android Project, go to Eclipse and import it as an Android Project. Right click on the properties and add our library project to libraries section. Now you are ready to build and run your project.

5

 

Now the project starts with MainActivity and when you press the button it will open Unity scene!

6

Advertisements

2 Comments Add yours

  1. Cristian says:

    Hello,

    At first, thank you for the article.
    But I’ve a question, I followed the steps but it doesn’t works fine.
    When I make click on the button, the app is closed and it goes at background.

    Looking the log, I see these lines:
    – E/dalvikvm: Could not find class ‘android.os.PersistableBundle’, referenced from method com.mamasu.openunityscene.GameActivity.access$super
    – E/Unity: Unable to find main
    – E/Unity: Unable to locate player settings. bin/Data/settings.xml

    Have you idea of what could be happening?
    Did I forget anything?

  2. snowrain says:

    – Adding android manifest to Unity project and export as Google Android Project.

    When I export as Google Android Project, the AndroidManifest.xml file created by Unity does not have MainActivity included in it.

    Please help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s