三个版本的配置方案,前两个版本纠结在“create project from exiting source”,几经曲折找到了比较好的解决方法。第三版为亲测实践版本。
------------------------ 建议三篇文章都读一下----------------------
【一.官方版】
Integrating Unity with Eclipse
Please not that every care has been taken to make this guide as friendly as possible to new users. However it will be a lot simpler to follow if you are at least familiar with using eclipse, the java programming language and have some knowledge of the android sdk.
To be able to use unity android with eclipse you will first need to have Eclipse configured for use with Android. There is great guide on how to do this in the android developer documentation.
Unity as a Library
Once you have eclipse installed and up to date you have to follow a few steps to get your unity project set up as a library in eclipse.
- Create a new empty unity project and build it for android. It does not matter at this point if the unity project contains any code or other assets. This part of the process is in no way future proof and will be subject to change with future release of unity android.
- Navigate to your new project folder and then into the Temp folder. In there is a folder called staging area.
- Copy the staging area folder and place it somewhere in your file system that you can use as a holding area. This holding area should not be in your eclipse workspace as it will lead to errors.
- Open Eclipse and create a new Android Project using File --> New --> Project.
- Select Android Project then click next.
- This will be the library part of your project so name the project accordingly to avoid any confusion.
- You will also want to select the option to create project from an existing source. This source being the files you copied to the folder you are using as a holding area. If you select the Staging Area folder then click OK you will see that eclipse has filled in the relevant fields for you.
- click finish on the project creation wizard.
- Finally right click the root folder in the Package explorer tab in Eclipse and select the Properties option then the android tab. Check the "Is Library" check box, click Apply and then OK.
This completes the steps that you need to carry out to have your unity project addressed as a library in eclipse.
Java Project
The next step is to create the eclipse project that will form the executable part of the project.
- Create another new android project.
- Name the project and select the Build Target and Min SDK Version to be the same as the Library project that you created.
- Make sure that the package name is the same as the bundle identifier in your unity project otherwise you can get problems with conflicting files in the generated java files in your project.
- Now the library you created needs to be added to the project, you do this by right clicking the JavaProject and selecting properties.
- You then need to Select Android and click add. A window should then popup select the library project that was created and click ok. Once you are back in the main window click apply.
- Now select Java Build Path from the list on the left and click add external jar. A window will now popup you need to navigate to your unity install directory and in Unity\Editor\Data\PlaybackEngines\androidplayer\bin there will be a file called classes.jar. Double click that and then click on ok.
This sets up a basic Java Project that is now linked to your unity project and the relevant libraries that we will later need.
Moving the Projects Assets
This is the important part to get your unity content into the Java Project so that it will display when you later build and run your Java Project. You now need to move all the files in the assets folder in the library project into the new project. This is because Android does not allow assets to be used in libraries. The best way to do this is from within the Eclipse IDE. If you expand the assets files within both parts of the package explorer and simply drag the bin and libs folders from the library part of your project to the other. This last step needs to repeated everytime your unity project is rebuilt or maybe you could write a script to handle moving the files everytime you build the project automatically. This could also be achieved using symlinks or junctions. At this point when you run the from the eclipse ide you will be asked which type of project that you want to run. Select android and click ok. After eclipse builds the project it will deploy it to your connected android device and you should see a hello world style application running.
Getting Your Unity Content Running
To get your unity project you need to make a few changes to the java source code. In your eclipse project there will be one source file (.java) open this file. The contents of this file will resemble something like this.
package com.Unity3D.EclipseIntegration;
import android.app.Activity;
import android.os.Bundle;
public class EclipseIntegration extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
We need to change this. The first thing is to change the base class of the object. It should now instead extend UnityPlayerActivity. You will notice that when you do this that it is red underlined proclaiming errors. This is simply missing import directives in the file. The eclipse shortcut to auto resolve this is ctrl+shift+o. The fiel should now have the import statement.
import com.unity3d.player.UnityPlayerActivity;
If you run in this state you will see that you now have a full screen hello world application. This is not what you want we need one more change to get your unity content running.
In the onCreate function there is one line that is overriding the unity view in the android application. If you delete the line.
setContentView(R.layout.main);
The complete file should now look like this.
package com.Unity3D.EclipseIntegration;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerActivity;
public class EclipseIntegration extends UnityPlayerActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
If you run it now you will see your unity content running on your android device.
【二.网络版】
这个过程非常复杂。步骤一定要谨记
1,建立一个unity3d工程,然后自己丢点模型进去吧。然后设置导出时候的包名字(重要,后面解析),build一下。就会发现工程目录下面的temp文件夹里面多了一个文件夹,如果不build是没有的,如果你关闭了unity的话这个文件夹也会消失,这个现象费了我一天的时间,看官网英文教程漏一个单词都不行啊。
2,拷贝temp文件夹里面的StagingArea出来,然后以这个文件夹为source建立一个eclipse工程,就是建立eclipse选create project from exiting source啦。
3 右键这个以stagingarea文件夹为基础的工程,选择Properties -》android-》Is Library 设置为资料
4,新建一个新的eclipse工程(包名称与unity3d设置的导出工程文件名要一致,否则直接报错),导入上一个工程作为library,就是同样在已经建立好的工程上右键,在同样的地方点选add,就可以看见啦,然后导入。
5 导入jar包,右键工程属性,java build path-》添加jar到library,Unity安装目录\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar
6; 复制stagingArea工程里面的assets所有文件到新建立的文件中,(必须)我们就完成了unity和eclipse的合体了。
7.我们来直接一activity的样子运行一下Unity做好的工程吧
package com.Unity3D.EclipseIntegration;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerActivity;
public class EclipseIntegration extends UnityPlayerActivity
{
@Overridepublic
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);需要注息这一句,否则会被遮挡。因为继承了unityplayerActivity,所以会直接显示unity的工程啦
}
}
【三.实践版】可以直接跳过此版本 直接看【四.傻瓜版】傻瓜版
基本步骤同上。简单说下流程+注意事项:
1.创建Unity工程,填充游戏内容,导出Unity工程为Eclipse工程时选择android平台,勾选 ,设置包名称,注意:两个地方需要使用到包名称,设置为相同,此处为第一处。不久会得到了一个Eclipse游戏工程的模板,目录结构和普通Eclipse工程基本相同。
2.将Unity工程Assets目录下Temp目录下的StagingArea文件夹拷贝到一个非工程目录的任意位置【建议非中文目录】,下一步要用到。
3.【假设你的android开发环境ADT等配置已經正確搭建】启动Eclipse,然后File——>Import——>Android下的Existing Android Code Into Workspace——>Next——>Browse——>选中刚才的StagingArea文件夹——>Open——>Next——>.......默认生成一个名称为UnityPlayerProxyActivity的工程。
4.选中该Eclipse工程,右键选择Build Path——>Configuer Build Path...——>选中左侧的Android选项——>勾选Is Library选项——>Apply———>OK——>此时bin目录生成unityplayerproxyactivity.jar,这个工程将会被另一个Eclipse工程做为类库引用。我称它为”类工程“。
5.新建一个新的Eclipse工程:File——>New——>Android Application Project——>按需求填写,注意包名与导出Unity包名相同,此处为第二处——>Next——>按需求勾选——>Next——>默认——>Next——>不够选Create Activity【本人习惯-_-!!!!】——>Finish.我称它为”游戏工程“
6.选中"游戏工程",右键——>Build Path——>Configuer Build Path...——>选中左侧的Android选项——>不勾选Is Library选项——>Add——>选中弹窗中的"类工程"——>Apply———>OK.
7.删除”类工程“的Asset文件夹下的子文件夹,将模板【步骤1生成的】中的相应文件替换掉”游戏工程“中的对应文件包括:除了res文件夾,全部替換掉【具体按需求】。
8.选中"游戏工程",右键——>Build Path——>Configuer Build Path...——>选中左侧的Java Build Path选项——>选择Library选项卡——>Add JARs——>classes.jar【已经从模板拷贝到了”游戏工程“】——>Add JARs——>找到”游戏工程“下Android Dependencies下的unityplayerproxyactivity.jar【或者”游戏工程“Plugins/Android/bin/unityplayerproxyactivity.jar】——>选择Order and Export选项卡——>勾选刚才的这两个jar包——>Apply———>OK.
9.收工。
注意事项: 1.相同包名
2.jar包的引用及可导出
3.JNI的使用【请参考其他文章】
关于第三方SDK引用,JNI的交互使用【充当Unity调用Java的媒介】,了解Android的同学,得意的笑吧。
【四.傻瓜版】傻瓜版
1.新建Unity3d工程,然后是各种游戏逻辑【可为空场景】
2.导出Eclipse工程。配置时注意Other Settings下的Bundle Identifier和Minimum API Level。设置完成后,点击Export
3.此时将Unity3d工程下的Temp/StagingArea文件夹 拷贝至非Eclipse 工作空间目录下【该文家夹在关闭Unity3d软件后消失】
4.将StagingArea文件夹Import进Eclipse成为一个新工程。将该工程设为类库,删除assets和libs文件夹。
5.新建Eclipse Android空工程。参数设置参见步骤2.
6.将步骤2导出的工程内容全部拷贝至步骤5新建的工程内进行文件覆盖.
7.将classes.jar和unityplayerproxyactivity.jar两个包 进行引用并设置为可导出。
【相关链接】
http://blog.csdn.net/qyxls/article/details/7376891
http://blog.csdn.net/zlfxy/article/details/8755839
http://blog.csdn.net/zlfxy/article/details/8755813
http://blog.csdn.net/menuconfig/article/details/9273615 [3]
http://blog.tianya.cn/blogger/post_read.asp?BlogID=3923664&PostID=38751394
http://game.ceeger.com/Manual/Plugins.html
http://docs.unity3d.com/Documentation/Manual/Plugins.html
http://www.rbcafe.com/Softwares/Unity/Documentation/Manual/Android-Integrating%20Unity%20With%20Eclipse.html