Two ways Unity calls Android

Source: Internet
Author: User

Two methods are summarized, for reference.

Method One

(To write a method under the Unityplayeractivity class, otherwise it cannot be called):

1 //Get class2 3Androidjavaclass JC =NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer");4 5  6 7 //Get Java Objects8 9Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity");Ten  One   A  - //Execution Method -  theJo. Call ("Method", ARG); -  -  

Method Two:

1     //JNI Create a new thread2 3 Androidjni.attachcurrentthread ();4 5  6 7         //Get class8 9IntPtr CLS = Androidjni.findclass ("Com/unity3d/player/unityplayer");Ten  OneINTPTR FID = Androidjni.getstaticfieldid (CLS,"currentactivity","landroid/app/activity;"); A  -IntPtr obj =Androidjni.getstaticobjectfield (CLS, FID); -  the   -  -         //Get class -  +IntPtr Cls_ourappactitvityclass = Androidjni.findclass ("Com/bodhi/talkingbodhi"); -  +   A  at         //How to obtain -  -IntPtr Enablemethod = Androidjni.getmethodid (Cls_ourappactitvityclass,"enableads","V"); -  -   -  in         //If the method exists, execute the -  to         if(Androidjni.isinstanceof (obj, cls_ourappactitvityclass)! =false) { +  -jvalue[] MyArray =Newjvalue[1]; the  * Androidjni.callvoidmethod (obj, Enablemethod, myArray); $ Panax Notoginseng}

Two ways Unity calls Android

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.