Unity's SDK access (Unity and Android communication)

Source: Internet
Author: User

New Android project in 1.eclipse, add Mainactivity.java code:

 Packagecom.example.test;ImportCom.unity3d.player.UnityPlayer;Importcom.unity3d.player.UnityPlayerActivity;ImportAndroid.os.Bundle; Public classMainactivityextendsunityplayeractivity {protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); }      Public voidSend () {Unityplayer.unitysendmessage ("AAA", "Androidcallback", "Okokoko"); }}
mainactivity

Export the jar package from eclipse

2.Unity, New project,

Write the EclipseCall.cs code as follows:

using unityengine;using system.collections;using unityengine.ui;using System; Public classeclipsecall:monobehaviour{PrivateAndroidjavaclass JC; PrivateAndroidjavaobject Jo; Privatetext text; PrivateButton btn; //single-case mode    Private StaticEclipsecall _instance;  Public StaticEclipsecall Instance {get {if(_instance = =NULL) {_instance=NewEclipsecall (); }            return_instance; }    }    voidStart () {text= Gameobject.find ("AAA"). Getcomponent<text>(); BTN= Gameobject.find ("button"). Getcomponent<button>();  This. Name = "AAA"; _instance= This; JC=NewAndroidjavaclass ("Com.unity3d.player.UnityPlayer"); Jo= JC. Getstatic<androidjavaobject> ("CurrentActivity"); }     Public voidclickbtn () {Jo. Call ("Send"); }     Public voidandroidcallback (String str) {Text.text=str; }}
Eclipsecall

Drag the exported jar package from eclipse into the Libs file

Packaged:

Run, click the UI after:

Unity's SDK access (Unity and Android communication)

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.