@@ -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
0 commit comments