歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Unity3D與Eclipse集成開發Android應用

Unity3D與Eclipse集成開發Android應用

日期:2017/3/1 10:52:18   编辑:Linux編程

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.

  • 創建一個空的unity3d工程,然後使用Android平台進行編譯。
  • 切換到新工程的目錄中,找到Temp目錄,這個目錄下有:staging area.
  • 拷貝目錄staging area到其他某個目錄下,注意此目錄不要放置於eclipse工作空間中。
  • 打開eclipse並創建一個android的項目。
  • 在創建項目的引導中,選擇Android Project並點擊Next按鈕
  • 這一步是工程的library信息, 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.

Copyright © Linux教程網 All Rights Reserved