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




 

 
 
 

 
   
     
      
      
     
   
   
   
 

 

 

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

If you like this article please support my work on Patreon, thank you!

Advertisement

3 thoughts on “Unity Android Tutorial- Opening unity scene from an activity

Add yours

  1. 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. – 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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Powered by WordPress.com.

Up ↑

%d bloggers like this: