Home > other >  Java programmer illustrated in detail the Unity in the call of the Android interface
Java programmer illustrated in detail the Unity in the call of the Android interface

Time:09-23

Recent work on a project, provides the interface for colleagues, can make the Android function called him in a Unity to implement the QQ login and user information, according to some books and some post bar for a few days, although all go through the sometimes logic, but sometimes will still be some problem, now will make a summary, some problems of
1, development tools Unity3d eclipse, while android's current development has been widely used android studio but here suggest that you use the eclipse
2, get to the point, we must first define the android interface, before creating the interface, we first to create the project, (here for not using eclipse's classmate)

3, configure the smallest SDK and the largest SDK and compile the SDK (why do you want to configure this, simple to understand, is that with the continuous upgrade of the android operating system, issued to the developer API with the upgrade, also some new features high version of the SDK (interface) may be on the low version of the operating system with no, for a tip fault tolerance),
In order to ensure that only the best package is your company's domain name reverse + project name, such as Google is google.com.test



4, to run all the way to the finish, in the following interface, can check the Make is Libray (if your interface is very complex, can temporarily not check, after using the Eclipse debug, modify for the selected package, can not baidu)

5, before defining interfaces, talk easy first Activity, at the time of creating the project, system has provided an Activity, the default class named MainActivity, and in the androidManifest. XML has been configured to give priority to the Activity, a running program, will create the first instance of the Activity, and invoke its lifecycle methods,
Activity and ordinary Java classes is different, he can't use new to instantiate, only by the system to create, if you go to new he is an ordinary Java classes , if more than one Activity in the project, other Activity to be instantiated, must rely on MainActivity directly or indirectly, he is just like an entrance,
In Unity script, also can obtain MainActivity reference. (if you don't understand can look down 6)
6, due to program a start, first create MainActivity instance, and callback onCreate () method, such as the Unity of our main interface is to rely on scene to render, so delete MainActivity the setContentView (); Inheritance and change,
6.1: join the Unity offers a jar package (jar package is the essence of a class library), the location of the jar package under the installation directory Editor \ Data \ PlaybackEngines \ androidplayer \ development \ bin (according to their actual location to find), classes, jars,

6.2: the use of packaging forms to join the jar package, (do not use classes. The jar directly copied to the libs to below)
Select the project/preferences/Java BuildPath../Library/Add External Jars

6.3: delete unused code, change the inheritance relationship. (be sure to delete the setContentView, because he's interface is Unity Scene)

7, increase method (let it be Unity calls), here we define an output toast a method, is very simple, Unity passed a String type parameter name, you can output toast Hello: name

8, add a method, can let him on the Activity of the jump, jump before we talk about the Activity of creating,
8.1: we create a class first,

8.2: to increase its inheritance, let he inherited Activity class and rewrite the onCreate () method,

8.3: defined Activity class is just plain Java classes, we will in the AndroidManifest. XML configuration, let it become an Activity
Note is underneath the Application node configuration

8.4 to this Activity is configured, our Android program will consist of two activities, the first Activity of the interface by Unity Scene will provide
The second Activity is the default interface, let's modify SecondActivity interface
8.5: Settings interface call setConetentView ();

8.6: a change on the interface, the android interface is an XML file, by the View hierarchy tree structure management
You can drag and drop components in view mode, but not very good with,

8.7 here we SecondActivity definition, configuration and the layout has been set, we define a method in the MainActivity, used to call in Unity, to jump
9. MainActivity defines a method, jump to SecondActivity, very simple, just a skip


10, and finished the android interface definition, before packaging, also need to do some things, these things if you don't do, pack things may not be able to be called
10.0: Make modification project is Libray
10.1: under the Project menu to cancel Bulild Autocomplie (automatic compilation)
10.2: the clean project
10.3: to Build the Project the Project


11. Our Android program packaging, by definition, something packaging may need only a class, but our project in addition to their own class and automatically generated
Gen things under the directory, and other resources, in order not to miss, simply all packaging, res AndroidManifest. XML is packaged, these just rubbish,




12. To the Android interface has been defined, took about how to call in Unity


The application of 1, create Unity

2, under the Assets to define the Plugins \ Android folder

3, under the Android defines bin will have a jar package step 11, to join the bin, and the project of the res, assets, libs, AndroisManifest. XML is copied to the Android directory, these are the resources of the Android,

4, after the copy
The directory structure of a final


5, to modify a little bit about the AndroidManifest. XML
5.1 open the installation directory path Editor \ Data \ PlaybackEngines \ androidplayer \ AndroidManifest XML,

The red parts are copied to our program's AndroidManifest. XML, copy after the completion of the


6. The Unity of writing code calls the function in the MainActivity


7, compiled into apk
Note buildsetting review


8, well, we run the compiled programnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related