For an app which has to show both Unity3d and regular Android UI, I wrote a guide for my future use. AFAIR it's based on Unity3d version 4.3. I hope it's detailed enough :)
Based on links: http://forum.unity3d.com/threads/98315-Using-Unity-Android-In-a-Sub-View/page4?p=1477830&viewfull=1#post1477830
Pre-setup:
In order to start integrating Unity3d and Android you need the classes.jar file found in '...Unity\Editor\Data\PlaybackEngines\androidplayer\bin'. You can place it in your workspace directory.
Setup:
- Start with an Android project which will contain all the Android bits.
- Prepare the Unity3d Android project. Import the Unity project into your workspace like a regular Android project.
- RMB on the Unity project, click “Properties”, then go to “Android” and tick the “Is Library?” check box.
- RMB on the Android project, click “Properties”, then go to “Android” and click “Add” to add a library - the Unity project - to the Android one.
- Still in the “Properties” dialog, go to “Java Build Path” and select the “Libraries” tab. There, click “Add external JAR” and navigate to the classes.jar file.
- Ensure both projects target the same SDK.
- Move the “assets” folder from the Unity project into the Android project. This step has to be repeated each time your Unity project is rebuilt.
Coding:
This guide assumes your goal is to have an Android activity which will display both a Unity model and regular Android UI.
- Add the following line to the base of the project’s manifest file: ‘’.
- Add the following line in the manifest to the activity which is supposed to contain the Unity model: ‘’. This enables touch events to be passed to both Unity models and regular Android UI.
- Make the activity extend UnityPlayerActivity rather than Activity.
- Don’t add the ‘setContentView’ straight in onCreate() - this leads to unwanted behaviour - in my case the Unity model was very small and couldn’t be interacted with; instead, in onResume() find the parent view of the UnityPlayer object and in it inflate the layout and add all the other UI elements. Note that it’s possible that finding of said view might need to be done in a separate thread.
Troubleshooting:
- NoClassDefFoundException (or similar) on classes which extend UnityPlayerActivity: go to Properties of your Android project, then Java Build Path -> Order and Export and tick classes.jar (in my case it was at the top of the list if that makes any difference). Press OK, clean the project, try to run again and PRAY…
- multiple dex files define Lcom/unity3d/player/nativeloader - make sure the “libs” dir in the Unity project does NOT contain the “unity-classes.jar” file.
- (Not sure but worth a try) problems in the src folder - remove the contents of the src folder
Here's some sample code to illustrate the Activity
which holds the Unity3d object:
private Button buttonChangeColour;
private UnityPlayer unityPlayer = null;
@Override
public void onResume() {
super.onResume();
if (unityPlayer == null) {
View rootView = findViewById(android.R.id.content);
unityPlayer = findUnityPlayerView(rootView);
if (unityPlayer != null) {
ViewGroup unityPlayerParentView = (ViewGroup)(unityPlayer.getParent());
View mainHomeView = getLayoutInflater().inflate(R.layout.activity_home, null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
unityPlayerParentView.addView(mainHomeView, layoutParams);
buttonChangeColour = (Button) findViewById(R.id.buttonChangeColour);
buttonChangeColour.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// [...]
}
});
}
}
}
private UnityPlayer findUnityPlayerView(View view) {
if (view instanceof UnityPlayer) {
return (UnityPlayer) view;
}
if (view instanceof ViewGroup) {
ViewGroup childrenViews = (ViewGroup) view;
for (int i = 0; i < childrenViews.getChildCount(); i++) {
UnityPlayer foundView = findUnityPlayerView(childrenViews.getChildAt(i));
if (foundView != null) {
return foundView;
}
}
}
return null;
}