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.
- Create a new project(name it whatever you want),add your package name (mine is” com.nexxmobile.openunityscene”).
- Mark it as a library and add your java classes(MainActivity,GameActivity) under that package name.
- Add “unityclasses.jar” as an external jar.(It is found in the installation folder (usually (on Windows) or (on Mac)) in a sub-folder called .)
Next add a layout for our MainActivity. I named it “main_activity.xml”.
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.
Now the project starts with MainActivity and when you press the button it will open Unity scene!
If you like this article please support my work on Patreon, thank you!
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?
– 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.
I didn’t understand about the extended UnityPlayerActivity .