Unity calls Android method

Source: Internet
Author: User

Steps

No more nonsense, just go ahead.
1. Create a project and make it look like this interface

2. Write code in unity, like this, and remember to bind the interface to events.

Using Unityengine;Using Unityengine.ui;PublicClassTestScripts:Monobehaviour {Public Inputfield m_inputfiled;Public Text M_label;PublicvoidOnClickButton1 () {Androidjavaclass JC =New Androidjavaclass ("Com.unity3d.player.UnityPlayer"); Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("CurrentActivity"); Jo. Call ("U3d_showtoast1"); }PublicvoidOnClickButton2 (new androidjavaclass (" Com.unity3d.player.UnityPlayer "); Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ( "currentactivity"); Jo. Call ( "U3d_showtoast2", M_inputfiled.text);} public void onclickbutton3 (new androidjavaclass ( "Com.unity3d.player.UnityPlayer"); Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ( "currentactivity"); m_label.text = Jo. Call<string> ( "U3d_getvalue");}}     

3. Export Android Project

4. Import the project into eclipse

5. Find the corresponding activity

6. Add the appropriate code to the activity

7. Package Run

Click the first button

After entering text in a text box, click the second button

Click the third button and the label word changes to Android return text

Unity calls Android method

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.