Java programmer illustrated in Unity calls Android interface

Source: Internet
Author: User

http://bbs.csdn.net/topics/391876421

A recent project to provide a colleague with an interface that allows him to invoke the Android function in unity to enable QQ login and obtain user information. According to some books and some post-paste things to get a few days, sometimes logic although all walk through, but sometimes there will be some problems, now will appear some of the problems to do a summary.
1. Development tools Unity3d Eclipse, although Android is currently being developed for Android, it is recommended that you use eclipse
2, back to the beginning, we want to define the interface of Android, before we create the interface, we first create the project. (This is for students who have not used eclipse)

3, the configuration of the smallest SDK and the largest SDK and compiled SDK (why to configure this, a simple understanding is that with the upgrading of the Android operating system, the release of APIs to developers with the upgrade, a new feature of the high-version SDK (interface) may not be used on the lower version of the operating system, For a fault-tolerant hint).
To ensure that the package is the only best for your company's domain name reverse + project name, such as Google is google.com.test



4, all the way to completion, in the following interface, you can tick make is libray (if your interface is very complex, you can temporarily uncheck, use Eclipse debugging finished, packaging when modified to select, will not be Baidu)

5, before defining the interface, the first simple talk about activity, in the creation of the project, the system has provided an activity, the default class called Mainactivity, And when Androidmanifest.xml is already configured as the primary activity, the program runs, first creating an instance of the activity and invoking its life cycle method.
Unlike normal Java classes, activity cannot be instantiated with new and can only be created by the system. If you go to new he is an ordinary Java class, if the project has more than one activity, other activities to instantiate, must directly or indirectly rely on mainactivity, he is like a portal.
In a unity script, you can only get a reference to mainactivity. (If you can't read it above you can look down 6)
6, since the start of the program, first create an instance of Mainactivity, and callback OnCreate () and other methods. The main interface of our unity is rendered by scene, so delete mainactivity in Setcontentview () and change the inheritance relationship.
6.1: Join a JAR package provided by unity (the essence of the jar package is a class library), the location of the jar package under the installation directory editor\data\playbackengines\androidplayer\development\ Bin (depending on your actual location), Classes.jar.

6.2: Use a non-packaged form to add a jar package. (Do not copy the Classes.jar directly under the Libs)
Select Project/preferences/java BuildPath. /library/add External Jars

6.3: Delete Unused code, change the inheritance relationship. (Be sure to remove Setcontentview, because his interface is provided by unity Scene)

7. Add method (Let it be called by Unity), here we define a method of output toast, very simple, unity pass a String type parameter name, you can output toast Hello:name

8, add a method, can let him carry on the activity of jump. Jump before we talk about activity creation.
8.1: Let's create a class first.

8.2: Increase the inheritance relationship, let him inherit the activity class and rewrite its OnCreate () method.

8.3: Defined activity class is just plain Java class, we want to configure in Androidmanifest.xml, let it become an activity
Note that it is configured under the application node

8.4 The activity is configured, and our Android program consists of 2 activity, the first activity interface is provided by the unity scene.
The second activity is the default interface, and below we modify the Secondactivity interface
8.5: Set interface call Setconetentview ();

8.6: Modify this interface, Android interface is actually XML file, through the tree structure to manage the view hierarchy
You can drag and drop components in view mode, but they are not very useful.

8.7 Here we secondactivity the definition, configuration, layout has been set. Next we define a method in mainactivity that is used to invoke in unity to jump
9.MainActivity define a method, jump to secondactivity, very simple, just a jump


10, to this Android interface definition is complete, before packaging, there are still some things to do, these things if not done, packaged things may not be called
10.0: Modify project Make is Libray
10.1: Cancel Bulild Autocomplie (automatic compilation) under the Project menu
10.2:clean a little bit about the project
10.3:build Project


11. To pack our Android project, it is reasonable to say that the package requires only a class, but our project is automatically generated in addition to our own class.
The Gen directory below, there are other resources, in order not to miss, simply packaged. Res Androidmanifest.xml is also packaged, and these are just rubbish.




12. The interface to this Android is already defined. Next talk about how to call in unity


1. Create Unity Apps

2. Define the Plugins\android folder below assets

3. Under Android, define bin to add 11-step jar package to Bin, and copy the Project Res,assets,libs, Androismanifest.xml to the Android directory, these are all Android resources.

4. After the copy is complete
The final directory structure


5, slightly modify the Androidmanifest.xml
5.1 Open the installation path under Editor\data\playbackengines\androidplayer\androidmanifest.xml,

Copy the red part into the androidmanifest.xml of our program, after copy is complete


Write code in 6.Unity to call functions in mainactivity


7. Compile into apk
Note Buildsetting set up a bit


8. OK, let's run the compiled program.

Java programmer illustrated in Unity calls Android interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.