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

Source: Internet
Author: User

For the conversion and merger of Unity3dproject and Androidproject, please refer to one of my other blogs. Suppose you are unfamiliar with the process of unity3dproject adding to Androidproject. Also, please read the following blog post first:

Android interacts with Unity3d, fragment displays Unity3d view.

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

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

2. Let's say that Unity3d project is exported as Android project and then re-developed in the process. Need to Unity3d call Android method, this time, then use the method in 1. is obviously inappropriate.


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

1. Add a method to the activity that initializes unityplayer, such as the following:

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

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


2. In the Unityproject. Using C # files to write, unity calls the code:

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, assuming you already have an Android project, export the newly modified Unity3d project to Android project, and replace the assets directory in the newly exported project with the old project, The purpose of this is to replace unity's changes, assuming that other things have been modified, and that this is the case. But suppose there are other resources in your assets directory. Please keep.

Assume. 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. As well as the methods it provides, you can refer to the following sites:

Androidjavaclass Android Java class


In this way, the call will be completed. There is no need to use plugins 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.