Unity and Android Interaction

Source: Internet
Author: User

Recently in contact with unity, for unity and Android interaction, online find a lot of, mostly is a way, the following say I use after the experience.

First of all:

Unity calls Android,

Unity Code:

New Androidjavaclass ("com.unity3d.player.UnityPlayer");         = JC. Getstatic<androidjavaobject> ("currentactivity");        Jo. Call ("login","");

Android Code:

public void Login (String str) {//write your own action}

The above is the online method.

When I was actually testing myself, I found that login () on the Android side is not necessarily written under the Unitypalyer class under the Com.unity3d.palyer package.

You just have to write login () in the Unityplayeractivity.java of your own definition package. Of course, the class must inherit the activity.

You can put the mouse on the Unity code call to see the method can fill in the parameters, you will find the method can be filled with parameters can be params object[]. That is, you can pass multiple parameters to the Android in the form of an array.

Next is Android calling Unity.

Cases:

Unityplayer.unitysendmessage ("btntest""showlog" "  a#b#c");

To illustrate, Unitysendmessage's first argument is the name of the Unity control, the second parameter is the method name, and the third parameter is the parameter to pass. and can only pass one parameter (feel good pit). But it's okay. You can pass the arguments you want to pass into a string passing through, and unity will do the dividing string on the line. For example, the above is to connect the a,b,c with #. Unity's side with the # as a delimiter to split is OK.

So the problem comes, the script hangs in the Unity control name is not good to find out what to do. In fact, there is a way to do it easily, that is, in the start () method of the script specifies that the name of the control passed by your method is OK.

If the control I'm looking for in the method above is btntest, then:

void Start () {    this"btntest";}

The above is widely reproduced on the Internet method.

Next, I'll introduce the following in the project I found a great God written by Unity to call the Android method (of course, definitely different from the above).

 Public voidCallFunction (stringFunctionName,params Object[] args) {        if(Androidclass = =NULL) {Androidclass=NewAndroidjavaclass ("COM.**.**.SDK. DEMOSDK");    } androidclass.callstatic (functionname, args); }     Public voidinitsdk () { This. CallFunction ("INITSDK","DEMONSDK","Callbackfunc", "A", "B", "C"); }

Explain: The parameter com.**.**.sdk in new Androidjavaclass. DEMOSDK,COM.**.**.SDK is the package name, DEMOSDK is the class name. The method name is INITSDK.

Unity and Android Interaction

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.