Unity interacts with Android

Source: Internet
Author: User

Most of the internet is putting Android projects into unity to pack into. apk. But I feel so bad, because I use the idea of iOS, unity and iOS interaction is using unity to export Xcode project for two times development, in fact, unity can also export eclipse for two development, I use the version is unity4.3, I remember before I used 4.0 to export the Eclipse project will generate three. Java scripts, now only generate one, unityplayernativeactivity, but this class to inherit two layers is unityplayeractivity, all the same, It can only be said that 4.3 simplifies the interaction of unity and Android,

I did a test completely stress-free interaction.

Unity Test Code,

Using unityengine;using System.collections;public class Test:monobehaviour {bool Issend = false;//Use this for Initiali Zationvoid Start () {}//Update is called once per framevoid update () {}void Ongui () {if (GUI. The button (new Rect (0,0,200,200), "one")) {using (Androidjavaclass JC = new Androidjavaclass (" Com.unity3d.player.UnityPlayer ")) {using (Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("CurrentActivity")) {Androidjavaclass CLS = new Androidjavaclass ("   Com.dilitechcompany.demotest.UnityPlayerNativeActivity "); Cls.   Callstatic ("_hideview", "one"); Jo. Call ("_hideview", "both");}}} if (GUI. The button (new Rect (0,200,200,200), "both")) {using (Androidjavaclass JC = new Androidjavaclass (" Com.unity3d.player.UnityPlayer ")) {using (Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("CurrentActivity")) {Androidjavaclass CLS = new Androidjavaclass ("   Com.dilitechcompany.demotest.UnityPlayerNativeActivity "); Cls.   Callstatic ("_display", "one"); Jo. Call ("_display", "both");}}} if (issend) {GUi. button (new Rect (200,0,200,200), "testbtn");}} void Androidsendmessage (string name) {issend =!issend;}}

Note: Unity provides us with the ability to call Android-specific classes, Androidjavaclass, Androidjavaobject, this does not understand the document, the online interpretation of a lot, but com. Unity3d . player . Unityplayer This I explain, this writing is a fixed wording, Android can generally understand, is to follow the package path to find this unityplayer, CurrentActivity This ambiguity is not the current activity but the main activity, this does not know can go to Androidmanifest.xml to see the main activity,

This is a fixed notation, the parameters are written dead

Alternatively, you can use androidjavaclass cls_compassactivity = new Androidjavaclass (" Com.dilitechcompany.demotest.UnityPlayerNativeActivity ");

Is your export of Android project package name + main activity, in fact, the same can be called success,

The code of this interaction can only be written in the main activity.

Package Com.dilitechcompany.demotest;import Com.unity3d.player.unityplayer;import android.app.NativeActivity; Import Android.content.res.configuration;import Android.graphics.pixelformat;import Android.os.Bundle;import Android.util.log;import Android.view.keyevent;import Android.view.view;import Android.view.Window;import Android.view.windowmanager;public class Unityplayernativeactivity extends nativeactivity{protected UnityPlayer munityplayer;//don ' t change the name of this variable;  Referenced from native code//unityplayer.init () should being called before attaching the view to a layout-it would load the Native code.//Unityplayer.quit () should be the last thing called-it would unload the native code.protected void oncreat E (Bundle savedinstancestate) {requestwindowfeature (window.feature_no_title); super.oncreate (savedInstanceState); GetWindow (). Takesurface (null); SetTheme (Android. R.style.theme_notitlebar_fullscreen); GetWindow (). SetFormat (pixelformat.rgb_565); munityplayer = new UnityPlayER (this), if (Munityplayer.getsettings (). Getboolean ("Hide_status_bar", True)) GetWindow (). SetFlags (  WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); int Glesmode = Munityplayer.getsettings (). GetInt ("Gles_mode", 1); Boolean trueColor8888 = False;munityplayer.init (Glesmode, trueColor8888); View Playerview = Munityplayer.getview (); Setcontentview (Playerview);p layerview.requestfocus ();//View rootView= Munityplayer.getview ();////toast.maketext (This, "Class:" +rootview.getclass (). GetName (), Toast.length_long). Show ( );//}public void _hideview (String name) {unityplayer.unitysendmessage ("Main Camera", "Androidsendmessage", "" "); LOG.V ("Unity3d", "Hide View"); public void _display (String name) {unityplayer.unitysendmessage ("Main Camera", "Androidsendmessage", "" "); LOG.V ("Unity3d", "Display");} protected void OnDestroy () {munityplayer.quit (); Super.ondestroy ();} OnPause ()/onresume () must is sent to Unityplayer to enable pause and resource RecReation on resume.protected void OnPause () {super.onpause (); Munityplayer.pause ();} protected void Onresume () {super.onresume (); Munityplayer.resume ();} public void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); Munityplayer.configurationchanged (newconfig);} public void Onwindowfocuschanged (Boolean hasfocus) {super.onwindowfocuschanged (hasfocus); Munityplayer.windowfocuschanged (Hasfocus);} public boolean dispatchkeyevent (KeyEvent event) {if (event.getaction () = = Keyevent.action_multiple) return Munityplayer.onkeymultiple (Event.getkeycode (), Event.getrepeatcount (), event), Return Super.dispatchkeyevent (event );}}

Finally Java code attached, this is also a 3dview view, easy to operate, two times the use of development, you can look at the log information, you can sung unity to see the results of interactive verification.

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.