publicclassUnityPlayerActivityextendsActivityimplementsIUnityPlayerLifecycleEvents { protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Override this in your custom UnityPlayerActivity to tweak the command line arguments passed to the Unity Android Player // The command line arguments are passed as a string, separated by spaces // UnityPlayerActivity calls this from 'onCreate' // Supported: -force-gles20, -force-gles30, -force-gles31, -force-gles31aep, -force-gles32, -force-gles, -force-vulkan // See https://docs.unity3d.com/Manual/CommandLineArguments.html // @param cmdLine the current command line arguments, may be null // @return the modified command line string or null protected String updateUnityCommandLineArguments(String cmdLine) { return cmdLine; }
mUnityPlayer = new UnityPlayer(this, this); setContentView(mUnityPlayer); mUnityPlayer.requestFocus(); }
// When Unity player unloaded move task to background @OverridepublicvoidonUnityPlayerUnloaded(){ moveTaskToBack(true); }
// When Unity player quited kill process @OverridepublicvoidonUnityPlayerQuitted(){ Process.killProcess(Process.myPid()); }
@OverrideprotectedvoidonNewIntent(Intent intent) { // To support deep linking, we need to make sure that the client can get access to // the last sent intent. The clients access this through a JNI api that allows them // to get the intent set on launch. To update that after launch we have to manually // replace the intent with the one caught here. setIntent(intent); mUnityPlayer.newIntent(intent); }
// Trim Memory Unity @OverridepublicvoidonTrimMemory(int level) { super.onTrimMemory(level); if (level == TRIM_MEMORY_RUNNING_CRITICAL) { mUnityPlayer.lowMemory(); } }
// This ensures the layout will be correct. @OverridepublicvoidonConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mUnityPlayer.configurationChanged(newConfig); }
// Notify Unity of the focus change. @OverridepublicvoidonWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); mUnityPlayer.windowFocusChanged(hasFocus); }
// For some reason the multiple keyevent type is not supported by the ndk. // Force event injection by overriding dispatchKeyEvent(). @OverridepublicbooleandispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MULTIPLE) return mUnityPlayer.injectEvent(event); returnsuper.dispatchKeyEvent(event); }
// Pass any events not handled by (unfocused) views straight to UnityPlayer @OverridepublicbooleanonKeyUp(int keyCode, KeyEvent event){ return mUnityPlayer.injectEvent(event); } @OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event){ return mUnityPlayer.injectEvent(event); } @OverridepublicbooleanonTouchEvent(MotionEvent event){ return mUnityPlayer.injectEvent(event); } /*API12*/publicbooleanonGenericMotionEvent(MotionEvent event){ return mUnityPlayer.injectEvent(event); } }
@OverrideprotectedvoidonNewIntent(Intent intent) { // To support deep linking, we need to make sure that the client can get access to // the last sent intent. The clients access this through a JNI api that allows them // to get the intent set on launch. To update that after launch we have to manually // replace the intent with the one caught here. setIntent(intent); mUnityPlayer.newIntent(intent); }
// Trim Memory Unity @OverridepublicvoidonTrimMemory(int level) { super.onTrimMemory(level); if (level == TRIM_MEMORY_RUNNING_CRITICAL) { mUnityPlayer.lowMemory(); } }
// This ensures the layout will be correct. @OverridepublicvoidonConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mUnityPlayer.configurationChanged(newConfig); }
// Notify Unity of the focus change. @OverridepublicvoidonWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); mUnityPlayer.windowFocusChanged(hasFocus); }
// For some reason the multiple keyevent type is not supported by the ndk. // Force event injection by overriding dispatchKeyEvent(). @OverridepublicbooleandispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MULTIPLE) return mUnityPlayer.injectEvent(event); returnsuper.dispatchKeyEvent(event); }
// Pass any events not handled by (unfocused) views straight to UnityPlayer @OverridepublicbooleanonKeyUp(int keyCode, KeyEvent event){ return mUnityPlayer.injectEvent(event); } @OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event){ return mUnityPlayer.injectEvent(event); } @OverridepublicbooleanonTouchEvent(MotionEvent event){ return mUnityPlayer.injectEvent(event); } /*API12*/publicbooleanonGenericMotionEvent(MotionEvent event){ return mUnityPlayer.injectEvent(event); }