Unity calls Android's activity

Source: Internet
Author: User

Before http://shuxiayeshou.blog.51cto.com/4452347/1932216

Wrote about the problems and solutions of unity and Android


To summarize, unity does a simple scenario where Androidstudio builds a simple project where mainactivity is to inherit from

Unityplayeractivity, so the Unity program can invoke the Android-mainactivity method.


So if there's another newactivity on Android, we're going to open it up through unity.


The following examples are also from the Internet to find, but the operation of the problem, I have been modified before it can be used.

First of all, this activity should be added to the androidmenifest.xml.


<?XML version="1.0"encoding="Utf-8"?>
<manifestxmlns:Android="Http://schemas.android.com/apk/res/android"
package="Com.reach.test">

<application
Android: allowbackup="true"
Android: icon="@mipmap/ic_launcher"
Android: label="@string/app_name"
Android: roundicon="@mipmap/ic_launcher_round"
Android: supportsrtl="true"
Android: Theme="@style/apptheme"
>
<activityAndroid: Name=". Mainactivity "
Android: configchanges="fontscale|keyboard|keyboardhidden|locale|mnc|mcc|navigation|orientation|screenlayout|screensize| Smallestscreensize|uimode|touchscreen ">
<intent-filter>
<actionAndroid: Name="Android.intent.action.MAIN"/>

<categoryAndroid: Name="Android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-dataAndroid: Name="Unityplayer. Unityactivity "Android: value="true"/>
</activity>
<activity android:theme= "@android: Style/theme.notitlebar.fullscreen"
Android:name= ". OpenActivity1 ">
</activity>
</application>

</manifest>


As in the yellow Word section

Since there is this OpenActivity1, it is bound to have a Java class,openactivity1, inherit the activity

Under the same path as the mainactivity

And in the Layout folder also has the corresponding XML Office file, I named Activity_new.xml


The code for OPENACTIVITY1 is:

Com.reach.testandroid.app.Activityandroid.content.Intentandroid.os.Bundleandroid.view.Viewandroid.view.View.OnClickListen Erandroid.widget.Buttonandroid.widget.TextViewOpenActivity1 activity{(Bundle savedinstancestate) {TextView Text= (TextView). Findviewbyid (r.id.) Text.settext (. Getintent (). Getstringextra ()) Button close= (button). Findviewbyid (r.id.) Close.setonclicklistener (Onclicklistener () {(View v) {OpenActivity1. Finish ()})}}


Which textview what are added on the components, first without tube, the simplest is the yellow word that two lines can be


And the Mainactivity code we use to interact with unity is as follows

Com.reach.testandroid.app.activityandroid.os.bundlecom.unity3d.player.unityplayeractivityandroid.content.contextandroid.c Ontent. Intentmainactivity unityplayeractivity {Activity =context = (Bundle savedinstancestate) {. OnCreate (Savedinstanc eState) ==startactivity1 ()} (String name) {Intent intent=intent (OpenActivity1.) Intent.putextra (name). StartActivity (Intent)}}


Where the StartActivity1 method is used to open OpenActivity1, first the app runs because the last line in the OnCreate is called, so first you see OpenActivity1 (don't tangle with this)

In summary, this method is open OpenActivity1 and will be called in unity


Then look back at unity.

We're going to build a script called Test.cs, just hang on to some gameobject on the scene.

The code is as follows:

Using unityengine;using system.collections;public class test : monobehaviour  {// Use this for initializationvoid Start  ()  {}// update is  called once per framevoid Update  ()  {         if  (Input.getkeydown (keycode.escape)  | |  input.getkeydown (keycode.home)  | |  input.getkeydown (Keycode.menu))         {             application.quit ();         }}    private void ongui ()     {         if (Guilayout.button ("open activity",  guilayout.height))          {             androidjavaclass jc&nBsp;= new androidjavaclass ("Com.unity3d.player.UnityPlayer");       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ANDROIDJAVAOBJECT&NBSP;JO&NBSP;=&NBSP;JC. Getstatic<androidjavaobject> ("currentactivity");             jo. Call ("StartActivity1",  "the way to bring up Android in Unity");        }     }}

Main View Ongui method

There is a button in it that shows the open Activity

The following three lines of code specify the actions the button clicks to perform

The first two lines are basically fixed usage

The third line is to call the StartActivity1 method in Mainactivity in the Android project, which is to open the OpenActivity1

One thing to be aware of here is the second line.

Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity");

JC is used here. Getstatic method, is no problem

In the original case, JC was used. The Get method (which is the same as the parameters below), actually clicking the button in the unity scene is unresponsive.


Also, in the original case, OpenActivity1 opened the layout of mainactivity, which is

Setcontentview (R.layout.activity_main);

To do this, run the app and Flash back.

So I added the activity_new and let OpenActivity1 correspond to this layout

So I changed it.

Setcontentview (r.layout.activity_new);

It's OK.



This article is from the "under the Tree Beast" blog, please make sure to keep this source http://shuxiayeshou.blog.51cto.com/4452347/1932442

Unity calls Android's activity

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.