|
/** |
|
* A custom Android Activity that renders Unity |
|
* as a FrameLayer. This is needed to exit Unity |
|
* by pressing an area of the screen. |
|
|
|
* Note: This needs and Activity Layout, see activity_start_unity.xml |
|
* |
|
* Location: android/app/src/main/java/<app_dir>/StartUnity.java |
|
*/ |
|
|
|
package <APP_BUNDLE_ID>; |
|
|
|
import android.support.v7.app.AppCompatActivity; |
|
import android.os.Bundle; |
|
import com.facebook.react.bridge.ReactApplicationContext; |
|
import com.facebook.react.bridge.ReactMethod; |
|
import com.unity3d.player.*; |
|
import android.content.Intent; |
|
import android.content.Context; |
|
import <UNITY_PROJ_BUNDLE_ID>.UnityPlayerActivity; |
|
import com.facebook.react.bridge.ReactContextBaseJavaModule; |
|
import android.app.Activity; |
|
import com.facebook.react.bridge.ReactApplicationContext; |
|
|
|
import android.content.res.Configuration; |
|
import android.graphics.PixelFormat; |
|
import android.widget.Button; |
|
import android.widget.FrameLayout; |
|
import android.widget.LinearLayout.LayoutParams; |
|
import com.facebook.react.bridge.ReactContextBaseJavaModule; |
|
import android.os.Debug; |
|
import android.view.KeyEvent; |
|
import android.view.MotionEvent; |
|
import android.view.View; |
|
import android.view.Window; |
|
import android.view.WindowManager; |
|
|
|
public class StartUnity extends AppCompatActivity { |
|
|
|
protected UnityPlayer mUnityPlayer; |
|
|
|
Context mContext; |
|
|
|
//Declare a FrameLayout object |
|
FrameLayout fl_forUnity; |
|
|
|
//Declare the buttons |
|
Button backButton; |
|
|
|
|
|
@Override |
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
// requestWindowFeature(Window.FEATURE_NO_TITLE); |
|
super.onCreate(savedInstanceState); |
|
|
|
// Hold a reference to this activity |
|
final AppCompatActivity activityRef = this; |
|
|
|
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy |
|
|
|
// Create the UnityPlayer |
|
mUnityPlayer = new UnityPlayer(this); |
|
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1); |
|
boolean trueColor8888 = false; |
|
|
|
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true)) |
|
{ |
|
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen); |
|
getWindow ().setFlags ( |
|
WindowManager.LayoutParams.FLAG_FULLSCREEN, |
|
WindowManager.LayoutParams.FLAG_FULLSCREEN |
|
); |
|
} |
|
|
|
//setContentView(mUnityPlayer); |
|
//Set the content to main |
|
setContentView(R.layout.activity_start_unity); |
|
|
|
//Inflate the frame layout from XML |
|
this.fl_forUnity = (FrameLayout)findViewById(R.id.fl_forUnity); |
|
|
|
//Add the mUnityPlayer view to the FrameLayout, and set it to fill all the area available |
|
this.fl_forUnity.addView( |
|
mUnityPlayer.getView(), |
|
FrameLayout.LayoutParams.MATCH_PARENT, |
|
FrameLayout.LayoutParams.MATCH_PARENT |
|
); |
|
|
|
//Initialize the buttons from the XML file |
|
this.backButton = (Button) findViewById(R.id.backButton); |
|
|
|
this.backButton.setOnClickListener(new View.OnClickListener() { |
|
@Override |
|
public void onClick(View v) { |
|
UnityPlayer.UnitySendMessage("Main Camera", "ReceiveRotDir", " "); |
|
activityRef.finish(); |
|
} |
|
}); |
|
|
|
|
|
mUnityPlayer.init(glesMode, trueColor8888); |
|
mUnityPlayer.start(); |
|
mUnityPlayer.requestFocus(); |
|
|
|
} |
|
|
|
@Override protected void onNewIntent(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); |
|
} |
|
|
|
// Quit Unity |
|
@Override protected void onDestroy () |
|
{ |
|
mUnityPlayer.quit(); |
|
super.onDestroy(); |
|
} |
|
|
|
// Pause Unity |
|
@Override protected void onPause() |
|
{ |
|
super.onPause(); |
|
mUnityPlayer.pause(); |
|
} |
|
|
|
// Resume Unity |
|
@Override protected void onResume() |
|
{ |
|
super.onResume(); |
|
mUnityPlayer.resume(); |
|
} |
|
|
|
@Override protected void onStart() |
|
{ |
|
super.onStart(); |
|
mUnityPlayer.start(); |
|
} |
|
|
|
@Override protected void onStop() |
|
{ |
|
super.onStop(); |
|
mUnityPlayer.stop(); |
|
} |
|
|
|
// Low Memory Unity |
|
@Override public void onLowMemory() |
|
{ |
|
super.onLowMemory(); |
|
mUnityPlayer.lowMemory(); |
|
} |
|
|
|
// Trim Memory Unity |
|
@Override public void onTrimMemory(int level) |
|
{ |
|
super.onTrimMemory(level); |
|
if (level == TRIM_MEMORY_RUNNING_CRITICAL) |
|
{ |
|
mUnityPlayer.lowMemory(); |
|
} |
|
} |
|
|
|
// This ensures the layout will be correct. |
|
@Override public void onConfigurationChanged(Configuration newConfig) |
|
{ |
|
super.onConfigurationChanged(newConfig); |
|
mUnityPlayer.configurationChanged(newConfig); |
|
} |
|
|
|
// Notify Unity of the focus change. |
|
@Override public void onWindowFocusChanged(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(). |
|
@Override 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 UnityPlayer |
|
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); } |
|
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); } |
|
@Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); } |
|
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); } |
|
|
|
|
|
// Quit Unity |
|
public void closeActivity() { |
|
mUnityPlayer = new UnityPlayer(this); |
|
this.mUnityPlayer.quit(); |
|
UnityPlayer.currentActivity.finish(); |
|
} |
|
|
|
} |