With the eclipse guide removed from the docs I have updated this to go from start to end in doing this.
First thing is to get your unity project set up and built.
As a basic example just create a spinning cube project then build for android ( obviously ).
The next step is to move some files around.
Go into your project directory and you will see a Temp folder in there you will see a Staging Area folder. You need to copy that folder out with the Temp folder. It doesn’t matter where to but as long as it not in the Temp folder. The reason for this is when you use the unity project in eclipse if you use the staging area in temp folder directly when unity rebuilds it changes the contents and this leads to problems with eclipse.
Now you need to open up eclipse.
Once open create a new android project and select create from existing source and point to the staging area folder that you created. This will give you the unity application in eclipse. I called this UnityLib for ease of reference in the future.
Next thing to do is to make another android project in eclipse.
The two important parts here are to make sure you have the correct API levels and the package name matches that of the package name you gave for the project in unity. I called this Unity Java for ease of reference.
Now we need to change a bunch of settings and move some file around.
-
Right Click UnityLib and select properties. Then under android check IsLibrary, Apply, OK
-
Right Click UnityJava and select properties. Then under Java build Path there are a few things that need to be done
-
First click external jar then navigate to your unity install directory then select Data\PlaybackEngines\androidplayer\bin\classes.jar
-
Then under android you need to add the library UnityLib.
-
now move the folder and their contents under the asset folde rin UnityLIb to the asset folder in UnityJava
-
next move the AndroidManifest from the UnityLib to UnityJava
Now you need to make some small code changes
- in the main source file in UnityJava you need to extend from UnityPlayerActivity and not Activity.
- if you press ctrl+shift+O after doing that it will automatically resolve the dependencies
- next you need to remove the line of code setContentView(R.layout.main);
And that should be it if you now run the UnityJava applicaiton for android it will build and run and you will see your unity content launched through an eclipse project.
Now assuming you have managed that without running into any errors its time to do some work in eclipse.
Repairing the damage we have already caused
- The class needs to go back to extending a normal activity and not the unity one.
Creating the Frame Layout We Require
Under the Java part of the project( as I referred to it in the docs ) expand the res folder to res->layout then double click main.xml. You should then see the main.xml open up. Notice there are now two tabs at the bottom one for the graphical layout and the other for the actual xml.
I prefer to do it by manually editing some of the xml to start with. I suspect that by default that a LinearLayout and a Textview will have been created for you in the xml. Delete it all apart from the <?xml version="1.0" encoding="utf-8"?> line. This gives us a blank slate to work on in the much friendlier Graphical Layout tab.
In the Graphical Layout drag a FrameLayout onto the screen area. Then drag another view onto the screen area. This second view can then be resized and will represent the area of the screen where you want the unity player to render.
That’s it for setting up the view.
Gratuitous ScreenShot
The code
A large portion of the code that is being used in here is the same as the code that is being used in the UnityPlayerActivity.java file that you would find in
C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\src\com\unity3d\player\
You will need a UnityPlayer variable to start off with.
The Unity Player then needs to be setup
// Create the UnityPlayer
m_UnityPlayer = new UnityPlayer(this);
int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
m_UnityPlayer.init(glesMode, trueColor8888);
Then the unity player needs to be allocated to a view to get the player to run.
// Add the Unity view
FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.addView(m_UnityPlayer.getView(), 0, lp);
The important part of this is the line of code:
FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
This will determine which framelayout we add the view to render unity in.
The “R.id.framelayout2” is and autogenerated file that you can find in the project under the gen folder.
Here is the complete source file:
package com.unity3d.viewexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import com.unity3d.player.UnityPlayer;
public class JavaCubeViewActivity extends Activity {
private UnityPlayer m_UnityPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the UnityPlayer
m_UnityPlayer = new UnityPlayer(this);
int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
m_UnityPlayer.init(glesMode, trueColor8888);
setContentView(R.layout.main);
// Add the Unity view
FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout2);
LayoutParams lp = new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layout.addView(m_UnityPlayer.getView(), 0, lp);
}
}
Did you just say pics or it didn’t happen.