@@ -18,7 +18,9 @@ class MobileUnityWidgetController extends UnityWidgetController {
1818 /// used for cancel the subscription
1919 StreamSubscription ? _onUnityMessageSub,
2020 _onUnitySceneLoadedSub,
21- _onUnityUnloadedSub;
21+ _onUnityUnloadedSub,
22+ _onUnityAttachedSub,
23+ _onUnityDetachedSub;
2224
2325 MobileUnityWidgetController ._(this ._unityWidgetState,
2426 {required this .unityId}) {
@@ -30,11 +32,12 @@ class MobileUnityWidgetController extends UnityWidgetController {
3032 /// in [UnityWidget.onUnityCreated] callback.
3133 static Future <MobileUnityWidgetController > init (
3234 int id, MobileUnityWidgetState unityWidgetState) async {
33- await UnityWidgetPlatform .instance.init (id);
34- return MobileUnityWidgetController ._(
35+ final controller = MobileUnityWidgetController ._(
3536 unityWidgetState,
3637 unityId: id,
3738 );
39+ await UnityWidgetPlatform .instance.init (id);
40+ return controller;
3841 }
3942
4043 @visibleForTesting
@@ -66,6 +69,18 @@ class MobileUnityWidgetController extends UnityWidgetController {
6669 .onUnityUnloaded (unityId: unityId)
6770 .listen ((_) => _unityWidgetState.widget.onUnityUnloaded !());
6871 }
72+
73+ if (_unityWidgetState.widget.onUnityAttached != null ) {
74+ _onUnityAttachedSub = UnityWidgetPlatform .instance.onUnityAttached (unityId: unityId).listen ((_) {
75+ _unityWidgetState.widget.onUnityAttached !();
76+ });
77+ }
78+
79+ if (_unityWidgetState.widget.onUnityDetached != null ) {
80+ _onUnityDetachedSub = UnityWidgetPlatform .instance.onUnityDetached (unityId: unityId).listen ((_) {
81+ _unityWidgetState.widget.onUnityDetached !();
82+ });
83+ }
6984 }
7085
7186 /// Checks to see if unity player is ready to be used
@@ -200,10 +215,14 @@ class MobileUnityWidgetController extends UnityWidgetController {
200215 _onUnityMessageSub? .cancel ();
201216 _onUnitySceneLoadedSub? .cancel ();
202217 _onUnityUnloadedSub? .cancel ();
218+ _onUnityAttachedSub? .cancel ();
219+ _onUnityDetachedSub? .cancel ();
203220
204221 _onUnityMessageSub = null ;
205222 _onUnitySceneLoadedSub = null ;
206223 _onUnityUnloadedSub = null ;
224+ _onUnityAttachedSub = null ;
225+ _onUnityDetachedSub = null ;
207226 }
208227
209228 void dispose () {
0 commit comments