Skip to content

Commit

Permalink
Merge pull request #97 from liamhowell13/androidFix
Browse files Browse the repository at this point in the history
Fix hard crash on Android
  • Loading branch information
azesmway authored Feb 17, 2024
2 parents e37683b + a919eab commit bdbd6a5
Showing 1 changed file with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,51 @@ public static void createPlayer(final Activity activity, final UnityPlayerCallba
callback.onReady();
return;
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.getWindow().setFormat(PixelFormat.RGBA_8888);
int flag = activity.getWindow().getAttributes().flags;
boolean fullScreen = false;
if ((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
fullScreen = true;
}

unityPlayer = new UnityPlayer(activity, new IUnityPlayerLifecycleEvents() {
@Override
public void onUnityPlayerUnloaded() {
callback.onUnload();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.getWindow().setFormat(PixelFormat.RGBA_8888);
int flag = activity.getWindow().getAttributes().flags;
boolean fullScreen = false;
if ((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
fullScreen = true;
}

@Override
public void onUnityPlayerQuitted() {
callback.onQuit();
unityPlayer = new UnityPlayer(activity, new IUnityPlayerLifecycleEvents() {
@Override
public void onUnityPlayerUnloaded() {
callback.onUnload();
}

@Override
public void onUnityPlayerQuitted() {
callback.onQuit();
}
});

try {
// wait a moment. fix unity cannot start when startup.
Thread.sleep(1000);
} catch (Exception e) {
}
});

try {
// wait a moment. fix unity cannot start when startup.
Thread.sleep(1000);
} catch (Exception e) {
}

// start unity
addUnityViewToBackground();
unityPlayer.windowFocusChanged(true);
unityPlayer.requestFocus();
unityPlayer.resume();
// start unity
addUnityViewToBackground();
unityPlayer.windowFocusChanged(true);
unityPlayer.requestFocus();
unityPlayer.resume();

// restore window layout
if (!fullScreen) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// restore window layout
if (!fullScreen) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
_isUnityReady = true;
callback.onReady();
}
_isUnityReady = true;
callback.onReady();
}
});
});
}
}

public static void pause() {
Expand Down

0 comments on commit bdbd6a5

Please sign in to comment.