Skip to content

Commit d471410

Browse files
authored
Merge pull request #244 from juicycleff/next
Next
2 parents 05f9036 + bb32b27 commit d471410

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1472
-498
lines changed

android/.idea/.name

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ rootProject.allprojects {
2121
// BUILD_ADD_UNITY_LIBS
2222

2323
// FOR DEV ONLY
24-
flatDir {
24+
/*flatDir {
2525
dirs "libs"
26-
}
26+
}*/
2727
}
2828
}
2929

android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip

android/libs/unity-classes.jar

-243 Bytes
Binary file not shown.

android/src/main/java/com/xraph/plugins/flutterunitywidget/FlutterUnityViewController.java

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ final class FlutterUnityViewController
5050
private final Context context;
5151
private UnityView unityView;
5252
private MethodChannel channel;
53+
private MethodChannel.Result unityReadyResult;
5354
private int channelId;
5455
private ThreadUtils mThreadUtils;
5556
private final AtomicInteger activityState;
@@ -101,31 +102,27 @@ void init() {
101102
switch (activityState.get()) {
102103
case STOPPED:
103104
if (unityView != null) {
104-
// this.createPlayer(true);
105105
unityView.onStop();
106106
}
107107
break;
108108
case PAUSED:
109109
if (unityView != null) {
110-
// this.createPlayer(true);
111110
unityView.onPause();
112111
}
113112
break;
114113
case RESUMED:
115114
if (unityView != null) {
116-
// this.createPlayer(true);
117115
unityView.onResume();
118116
}
119117
break;
120118
case STARTED:
121119
if (unityView != null) {
122-
// this.createPlayer(true);
123120
unityView.onStart();
124121
}
125122
break;
126123
case CREATED:
127124
if (unityView == null) {
128-
// this.createPlayer(true);
125+
// TODO: handle created lifecycle
129126
}
130127
break;
131128
case DESTROYED:
@@ -146,27 +143,28 @@ void init() {
146143
@Override
147144
public void onMethodCall(MethodCall methodCall, final MethodChannel.Result result) {
148145
switch (methodCall.method) {
149-
case "createUnity":
150-
UnityUtils.createPlayer(this.activity, mThreadUtils,this, true, new OnCreateUnityViewCallback() {
151-
@Override
152-
public void onReady() {
153-
unityView.setUnityPlayer(UnityUtils.getPlayer());
154-
result.success(true);
155-
}
156-
});
146+
case "unity#waitForUnity":
147+
if (unityView != null) {
148+
result.success(null);
149+
return;
150+
}
151+
unityReadyResult = result;
152+
break;
153+
case "unity#createUnityPlayer":
154+
this.createPlayer(unityView, true);
157155

158156
break;
159-
case "isReady":
157+
case "unity#isReady":
160158
result.success(unityView.isUnityReady());
161159
break;
162-
case "isLoaded":
160+
case "unity#isLoaded":
163161
result.success(unityView.isUnityLoaded());
164-
case "isPaused":
162+
case "unity#isPaused":
165163
result.success(unityView.isUnityPaused());
166-
case "isInBackground":
164+
case "unity#inBackground":
167165
result.success(unityView.isUnityInBackground());
168166
break;
169-
case "postMessage":
167+
case "unity#postMessage":
170168
String gameObject, methodName, message;
171169
gameObject = methodCall.argument("gameObject");
172170
methodName = methodCall.argument("methodName");
@@ -175,35 +173,35 @@ public void onReady() {
175173
UnityUtils.postMessage(gameObject, methodName, message);
176174
result.success(true);
177175
break;
178-
case "pause":
176+
case "unity#pausePlayer":
179177
UnityUtils.pause();
180178
result.success(true);
181179
break;
182-
case "openNative":
180+
case "unity#openInNativeProcess":
183181
openNativeUnity();
184182
result.success(true);
185183
break;
186-
case "resume":
184+
case "unity#resumePlayer":
187185
UnityUtils.resume();
188186
result.success(true);
189187
break;
190-
case "unload":
188+
case "unity#unloadPlayer":
191189
if (unityView != null && unityView.getUnityPlayer() != null) {
192190
unityView.unload();
193191
}
194192
UnityUtils.unload();
195193
result.success(true);
196194
break;
197-
case "dispose":
195+
case "unity#dispose":
198196
// TODO: Handle disposing player resource efficiently
199197
// UnityUtils.unload();
200198
result.success(true);
201199
break;
202-
case "silentQuitPlayer":
200+
case "unity#silentQuitPlayer":
203201
UnityUtils.quitPlayer();
204202
result.success(true);
205203
break;
206-
case "quitPlayer":
204+
case "unity#quitPlayer":
207205
if (UnityUtils.getPlayer() != null)
208206
UnityUtils.getPlayer().destroy();
209207
result.success(true);
@@ -214,19 +212,21 @@ public void onReady() {
214212

215213
}
216214

217-
218-
private void createPlayer(boolean reInitialize) {
215+
private void createPlayer(final UnityView view, boolean reInitialize) {
219216
UnityUtils.createPlayer(this.activity, mThreadUtils,this, reInitialize, new OnCreateUnityViewCallback() {
220217
@Override
221218
public void onReady() {
222-
unityView.setUnityPlayer(UnityUtils.getPlayer());
219+
view.setUnityPlayer(UnityUtils.getPlayer());
220+
if (unityReadyResult != null) {
221+
unityReadyResult.success(null);
222+
unityReadyResult = null;
223+
}
223224
}
224225
});
225226
}
226227

227228
private void openNativeUnity() {
228-
// isUnityLoaded = true;
229-
Intent intent = new Intent(activity, ExtendedUnityActivity.class);
229+
Intent intent = new Intent(activity, OverrideUnityActivity.class);
230230
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
231231
intent.putExtra("ar", options.isArEnable());
232232
intent.putExtra("fullscreen", options.isFullscreenEnabled());
@@ -284,20 +284,8 @@ private UnityView getUnityView() {
284284

285285
if (UnityUtils.getPlayer() != null && UnityUtils.isUnityLoaded()) {
286286
view.setUnityPlayer(UnityUtils.getPlayer());
287-
} else if (UnityUtils.getPlayer() != null) {
288-
UnityUtils.createPlayer(this.activity, mThreadUtils,this, false, new OnCreateUnityViewCallback() {
289-
@Override
290-
public void onReady() {
291-
view.setUnityPlayer(UnityUtils.getPlayer());
292-
}
293-
});
294287
} else {
295-
UnityUtils.createPlayer(this.activity, mThreadUtils, this, false, new OnCreateUnityViewCallback() {
296-
@Override
297-
public void onReady() {
298-
view.setUnityPlayer(UnityUtils.getPlayer());
299-
}
300-
});
288+
this.createPlayer(view, false);
301289
}
302290
return view;
303291
}
@@ -306,7 +294,7 @@ public void onReady() {
306294
public void onMessage(final String message) {
307295
activity.runOnUiThread(new Runnable() {
308296
public void run() {
309-
getChannel().invokeMethod("onUnityMessage", message);
297+
getChannel().invokeMethod("events#onUnityMessage", message);
310298
}
311299
});
312300
}
@@ -320,12 +308,11 @@ public void run() {
320308
payload.put("buildIndex", buildIndex);
321309
payload.put("isLoaded", isLoaded);
322310
payload.put("isValid", isValid);
323-
getChannel().invokeMethod("onUnitySceneLoaded", payload);
311+
getChannel().invokeMethod("events#onUnitySceneLoaded", payload);
324312
}
325313
});
326314
}
327315

328-
329316
private MethodChannel getChannel() {
330317
return channel;
331318
}
@@ -336,7 +323,7 @@ private MethodChannel getChannel() {
336323
public void onUnityPlayerUnloaded() {
337324
activity.runOnUiThread(new Runnable() {
338325
public void run() {
339-
getChannel().invokeMethod("onUnityUnloaded", true);
326+
getChannel().invokeMethod("events#onUnityUnloaded", true);
340327
}
341328
});
342329
}
@@ -403,8 +390,9 @@ public void onCreate(@NonNull LifecycleOwner owner) {
403390
if (disposed) {
404391
return;
405392
}
393+
406394
if (unityView != null) {
407-
// this.createPlayer(true);
395+
// TODO: handle onCreate
408396
}
409397
}
410398

android/src/main/java/com/xraph/plugins/flutterunitywidget/ExtendedUnityActivity.java renamed to android/src/main/java/com/xraph/plugins/flutterunitywidget/OverrideUnityActivity.java

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package com.xraph.plugins.flutterunitywidget;
22

33
import android.content.Intent;
4-
import android.graphics.Color;
5-
import android.graphics.Point;
64
import android.os.Build;
75
import android.os.Bundle;
86
import android.util.Log;
9-
import android.view.Display;
10-
import android.view.View;
117
import android.view.WindowManager;
12-
import android.widget.Button;
138

149
import com.unity3d.player.UnityPlayerActivity;
1510

1611
import java.util.Objects;
1712

18-
public class ExtendedUnityActivity extends UnityPlayerActivity {
19-
public static ExtendedUnityActivity instance = null;
13+
public class OverrideUnityActivity extends UnityPlayerActivity {
14+
public static OverrideUnityActivity instance = null;
2015
static final String LOG_TAG = "ExtendedUnityActivity";
2116
Class mMainActivityClass;
2217

@@ -25,51 +20,20 @@ protected void onCreate(Bundle savedInstanceState) {
2520
super.onCreate(savedInstanceState);
2621
instance = this;
2722
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
28-
addControlsToUnityFrame();
2923
Intent intent = getIntent();
3024
handleIntent(intent);
3125
}
3226

33-
public void addControlsToUnityFrame() {
34-
Display display = getWindowManager().getDefaultDisplay();
35-
Point point = new Point();
36-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
37-
display.getSize(point);
38-
}
39-
int size = (point.x / 2);
40-
41-
Button backButton = new Button(this);
42-
backButton.setBackgroundColor(Color.BLUE);
43-
backButton.setText("GO BACK");
44-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
45-
backButton.setX(10f);
46-
backButton.setY(25f);
47-
}
48-
backButton.setOnClickListener(new View.OnClickListener() {
49-
public void onClick(View v) {
50-
showMainActivity();
51-
}
52-
});
53-
54-
55-
Button unloadButton = new Button(this);
56-
unloadButton.setBackgroundColor(Color.BLUE);
57-
unloadButton.setText("UNLOAD");
58-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
59-
backButton.setX(25f);
60-
backButton.setY(300f);
61-
}
62-
unloadButton.setOnClickListener(new View.OnClickListener() {
63-
public void onClick(View v) {
64-
mUnityPlayer.unload();
65-
}
66-
});
27+
protected void unloadPlayer() {
28+
mUnityPlayer.unload();
29+
showMainActivity();
30+
}
6731

68-
mUnityPlayer.addView(backButton, size, size / 4);
69-
mUnityPlayer.addView(unloadButton, size, size / 4);
32+
protected void quitPlayer() {
33+
mUnityPlayer.quit();
7034
}
7135

72-
private void showMainActivity() {
36+
protected void showMainActivity() {
7337
Intent intent = new Intent(this, mMainActivityClass);
7438
intent.putExtra("showMain",true);
7539
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
@@ -123,9 +87,8 @@ private void handleIntent(Intent intent) {
12387

12488
@Override
12589
public void onBackPressed() {
126-
// moveTaskToBack(true);
12790
Log.i(LOG_TAG, "onBackPressed called");
128-
// this.mUnityPlayer.quit();
91+
this.showMainActivity();
12992
super.onBackPressed();
13093
}
13194

0 commit comments

Comments
 (0)