Unity calls Android features

Source: Internet
Author: User

Unity needs to call Android's features mainly in two ways, one is to use Android plug-ins, and the other is exported to Googleandroid engineering, for processing. Use a third-party plug-in or Access Channel SDK.

1: Use Android plugin for processing.
The specific process, the search engine has a lot of results, no longer repeat.
The main point to note: When exporting the jar, use the export option in eclipse directly, select All in Src and R.java in Gen. In addition Unity-classes.jar does not need to be included again.

When you need to use multiple plugins at the same time:
Only one mainactivity can be set in Androidmanifest, then the activity of the other plugin may be started in the following way.

Untiy C #

Androidjavaclass JC = new Androidjavaclass ("Com.unity3d.player.UnityPlayer");
Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity");

Androidjavaclass jc2 = new Androidjavaclass ("Com.foo.bar.Start");
Jc2. Callstatic ("Startintent", Jo);


Android Java
Class Start
{
public static void Startintent (Activity main)
{
Intent Intent = new Intent (main,anothermain.class)
Main. Startactivtiy (Intent);
}
}

The main activity of another plugin
Class Anothermain extends Activity
{
//...

}

2: Export to Googleandroid Project
Projectsettings is set to export Android project, after export, open with eclipse.
After exporting src there are 3 files.
Unityplayeracitivity,unityplayerproxyactivity,unityplayernativeactivtiy
The first two classes are no longer used, directly inherit from Unityplayernativeactivity, and start the unityplayernativeactivity directly in the unityplayerproxyactivity.
is to let users use utniyplayernativeactivity directly.

My understanding is that the user's class inherits from one of these to be androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity"); Obtained.
Official documentation is required to inherit from Unityplayeractivity.
But Unityplayeractvtiy actually has 2 copies, one in Unity-classes.jar and the other in SRC. Inherit any one copy can be used.
The difference is that if you need to pick up an SDK that contains tools such as a suspended window, the inheritance from the jar package cannot be modified and inherits from SRC
Unityplayernativeactivtiy extends Nativeactivity
Switch
Unityplayernativeactivtiy extends Activity to allow the hover window to get a click event.

In addition modify Androidmanifest mainactivity (Android.intent.action.MAIN) for the activity that you need. The default is untiyplayernativtiactivtiy when exporting.

Problems found:
When running with Eclipse or packaging the installer apk, in the Unity C # code
The #if Untiy_editor was established. Other words. You need to write the relevant code for Android execution directly instead of the macro definition.
I just used #if unity_editor.
#else
#endif jumped the pit. This is pending update.

In fact, the difference between the two ways is not very large, may write plug-in the former convenient, the latter is convenient to connect the SDK. Unity calls Android if the static method can be directly new class and then callstatic, otherwise it can only inherit from Untiyplayeractivity to do.
Android calls Untiy using Unityplayer.unitysendmessage ("Gameobjectname", "MethodName", "args");
The three parameters are the object name, method name, and parameter that the script is located in. Unity processing.
public void MethodName (String message)
{
To-do
}

Unity calls Android features

Related Article

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.