Package Com.t.t;\n\rimport Com.unity3d.player.*;\n\rimport Android.app.nativeactivity;\n\rimport Android.content.res.configuration;import Android.graphics.pixelformat;\n\rimport Android.os.Bundle;import Android.view.keyevent;import Android.view.motionevent;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//Setup activity [email protected] protected void onCreate (Bundle savedinstancestate) {R Equestwindowfeature (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); Setcontentview (Munityplayer); Munityplayer.requestfocus ();} Quit [email protected] protected void OnDestroy () {munityplayer.quit (); Super.ondestroy ();} Pause [email protected] protected void OnPause () {super.onpause (); Munityplayer.pause ();} Resume [email protected] protected void Onresume () {super.onresume (); Munityplayer.resume ();} This ensures the layout would be is [email protected] public void onconfigurationchanged (Configuration newconfig) {sup Er.onconfigurationchanged (Newconfig); munityplayer.configurationchanged (newconfig);} Notify Unity of the focus [email protected] public void onwindowfocuschanged (Boolean hasfocus) { Super.onwindowfocuschanged (Hasfocus); munityplayer.windowfocuschanged (Hasfocus);} For some reason the multiple keyevent type are not supported by the NDK.//force event injection by overriding Dispatchk Eyevent () [email protected] public boolean dispatchkeyevent (KeyEvent event) {if (event.getaction () = = Keyevent.acTion_multiple) return munityplayer.injectevent (event); return Super.dispatchkeyevent (event);} Pass any events not handled by (unfocused) views straight to [email protected] public boolean onKeyUp (int keycode, KeyEvent event) {return munityplayer.injectevent (event); \n\r} @Override public boolean onKeyDown (int keycode, Keyeve NT Event) {return munityplayer.injectevent (event); \n\r} @Override public boolean ontouchevent (Motionevent event) {return munityplayer.injectevent (event);} /*api12*/public boolean ongenericmotionevent (Motionevent event) {return munityplayer.injectevent (event);}}
Import Unity project in Android, interface Click event loses focus problem