@@ -50,6 +50,7 @@ final class FlutterUnityViewController
50
50
private final Context context ;
51
51
private UnityView unityView ;
52
52
private MethodChannel channel ;
53
+ private MethodChannel .Result unityReadyResult ;
53
54
private int channelId ;
54
55
private ThreadUtils mThreadUtils ;
55
56
private final AtomicInteger activityState ;
@@ -101,31 +102,27 @@ void init() {
101
102
switch (activityState .get ()) {
102
103
case STOPPED :
103
104
if (unityView != null ) {
104
- // this.createPlayer(true);
105
105
unityView .onStop ();
106
106
}
107
107
break ;
108
108
case PAUSED :
109
109
if (unityView != null ) {
110
- // this.createPlayer(true);
111
110
unityView .onPause ();
112
111
}
113
112
break ;
114
113
case RESUMED :
115
114
if (unityView != null ) {
116
- // this.createPlayer(true);
117
115
unityView .onResume ();
118
116
}
119
117
break ;
120
118
case STARTED :
121
119
if (unityView != null ) {
122
- // this.createPlayer(true);
123
120
unityView .onStart ();
124
121
}
125
122
break ;
126
123
case CREATED :
127
124
if (unityView == null ) {
128
- // this.createPlayer(true);
125
+ // TODO: handle created lifecycle
129
126
}
130
127
break ;
131
128
case DESTROYED :
@@ -146,27 +143,28 @@ void init() {
146
143
@ Override
147
144
public void onMethodCall (MethodCall methodCall , final MethodChannel .Result result ) {
148
145
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 );
157
155
158
156
break ;
159
- case "isReady" :
157
+ case "unity# isReady" :
160
158
result .success (unityView .isUnityReady ());
161
159
break ;
162
- case "isLoaded" :
160
+ case "unity# isLoaded" :
163
161
result .success (unityView .isUnityLoaded ());
164
- case "isPaused" :
162
+ case "unity# isPaused" :
165
163
result .success (unityView .isUnityPaused ());
166
- case "isInBackground " :
164
+ case "unity#inBackground " :
167
165
result .success (unityView .isUnityInBackground ());
168
166
break ;
169
- case "postMessage" :
167
+ case "unity# postMessage" :
170
168
String gameObject , methodName , message ;
171
169
gameObject = methodCall .argument ("gameObject" );
172
170
methodName = methodCall .argument ("methodName" );
@@ -175,35 +173,35 @@ public void onReady() {
175
173
UnityUtils .postMessage (gameObject , methodName , message );
176
174
result .success (true );
177
175
break ;
178
- case "pause " :
176
+ case "unity#pausePlayer " :
179
177
UnityUtils .pause ();
180
178
result .success (true );
181
179
break ;
182
- case "openNative " :
180
+ case "unity#openInNativeProcess " :
183
181
openNativeUnity ();
184
182
result .success (true );
185
183
break ;
186
- case "resume " :
184
+ case "unity#resumePlayer " :
187
185
UnityUtils .resume ();
188
186
result .success (true );
189
187
break ;
190
- case "unload " :
188
+ case "unity#unloadPlayer " :
191
189
if (unityView != null && unityView .getUnityPlayer () != null ) {
192
190
unityView .unload ();
193
191
}
194
192
UnityUtils .unload ();
195
193
result .success (true );
196
194
break ;
197
- case "dispose" :
195
+ case "unity# dispose" :
198
196
// TODO: Handle disposing player resource efficiently
199
197
// UnityUtils.unload();
200
198
result .success (true );
201
199
break ;
202
- case "silentQuitPlayer" :
200
+ case "unity# silentQuitPlayer" :
203
201
UnityUtils .quitPlayer ();
204
202
result .success (true );
205
203
break ;
206
- case "quitPlayer" :
204
+ case "unity# quitPlayer" :
207
205
if (UnityUtils .getPlayer () != null )
208
206
UnityUtils .getPlayer ().destroy ();
209
207
result .success (true );
@@ -214,19 +212,21 @@ public void onReady() {
214
212
215
213
}
216
214
217
-
218
- private void createPlayer (boolean reInitialize ) {
215
+ private void createPlayer (final UnityView view , boolean reInitialize ) {
219
216
UnityUtils .createPlayer (this .activity , mThreadUtils ,this , reInitialize , new OnCreateUnityViewCallback () {
220
217
@ Override
221
218
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
+ }
223
224
}
224
225
});
225
226
}
226
227
227
228
private void openNativeUnity () {
228
- // isUnityLoaded = true;
229
- Intent intent = new Intent (activity , ExtendedUnityActivity .class );
229
+ Intent intent = new Intent (activity , OverrideUnityActivity .class );
230
230
intent .setFlags (Intent .FLAG_ACTIVITY_REORDER_TO_FRONT );
231
231
intent .putExtra ("ar" , options .isArEnable ());
232
232
intent .putExtra ("fullscreen" , options .isFullscreenEnabled ());
@@ -284,20 +284,8 @@ private UnityView getUnityView() {
284
284
285
285
if (UnityUtils .getPlayer () != null && UnityUtils .isUnityLoaded ()) {
286
286
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
- });
294
287
} 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 );
301
289
}
302
290
return view ;
303
291
}
@@ -306,7 +294,7 @@ public void onReady() {
306
294
public void onMessage (final String message ) {
307
295
activity .runOnUiThread (new Runnable () {
308
296
public void run () {
309
- getChannel ().invokeMethod ("onUnityMessage" , message );
297
+ getChannel ().invokeMethod ("events# onUnityMessage" , message );
310
298
}
311
299
});
312
300
}
@@ -320,12 +308,11 @@ public void run() {
320
308
payload .put ("buildIndex" , buildIndex );
321
309
payload .put ("isLoaded" , isLoaded );
322
310
payload .put ("isValid" , isValid );
323
- getChannel ().invokeMethod ("onUnitySceneLoaded" , payload );
311
+ getChannel ().invokeMethod ("events# onUnitySceneLoaded" , payload );
324
312
}
325
313
});
326
314
}
327
315
328
-
329
316
private MethodChannel getChannel () {
330
317
return channel ;
331
318
}
@@ -336,7 +323,7 @@ private MethodChannel getChannel() {
336
323
public void onUnityPlayerUnloaded () {
337
324
activity .runOnUiThread (new Runnable () {
338
325
public void run () {
339
- getChannel ().invokeMethod ("onUnityUnloaded" , true );
326
+ getChannel ().invokeMethod ("events# onUnityUnloaded" , true );
340
327
}
341
328
});
342
329
}
@@ -403,8 +390,9 @@ public void onCreate(@NonNull LifecycleOwner owner) {
403
390
if (disposed ) {
404
391
return ;
405
392
}
393
+
406
394
if (unityView != null ) {
407
- // this.createPlayer(true);
395
+ // TODO: handle onCreate
408
396
}
409
397
}
410
398
0 commit comments