Unity3d Call Android Method (non-plug-in mode)

Source: Internet
Author: User

For Unity3d Engineering and Android engineering conversion and merging, please refer to my other blog, if you are not familiar with the Unity3d project to add to the Android project, please read the following blog first:

Android interacts with Unity3d, fragment displays Unity3d view.

Below, let's talk about the terminal of this blog, Unity3d call Android method:

1. First, on the network you can see a lot of Android project packaging, and then plug-in way, put into the Unity3d project to call the way, this way, I do not comment, but very troublesome, so I also did not try, we are interested in the network to search for a look.

2. If the Unity3d project is exported as Android project and then re-developed the process, you need to Unity3d call the method of Android, this time, then use the method in 1, it is obviously inappropriate.


So what are we going to do about it? Divided into the following parts:

1. Add a method to the activity of initializing Unityplayer as follows:

/** * Test Unity calls Android method */public void TestMethod (String param) {System.out.println ("parameter:" +param);}

For the provided method, the UI operation is not possible because Unity3d for Android's UI thread, which is just a child thread, and you can use handler to send a message if you want to do a UI operation.


2. In Unity Engineering, use C # files to write the code that Unity calls:

public static void Activateshareimage (string content)    {        if (Application.platform = = Runtimeplatform.iphoneplayer)        {            _pressbutton3 (content);        }        else if (Application.platform = = runtimeplatform.android)        {            uisystem.console ("Test method!");            Androidjavaclass JC = new Androidjavaclass ("Com.unity3d.player.UnityPlayer");            Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity");            Jo. Call ("TestMethod", "Unity");        }        else        {                 }    }
3. If you already have an Android project, then export the newly modified Unity3d project to Android project and replace the assets folder in the newly exported project with the old project, The purpose of this is to change the unity changes, if you change the other things, but also to replace the same, but if you have other resources under the Assets folder, please keep.

If you do not have an Android project, then export the Unity3d project to Android for use with Project projects.


4. For androidjavaclass this class, and the method it provides, you can refer to the following website:

Androidjavaclass Android Java class


This makes it possible to complete the call without using a plugin at all. About Android sends a message to Unity3d, and will post a blog narrative later.

Unity3d Call Android Method (non-plug-in mode)

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.