Skip to content

Commit 6fe1e83

Browse files
committed
Add support for onDettached onAttach UnityWidget
1 parent b233e52 commit 6fe1e83

File tree

9 files changed

+102
-1
lines changed

9 files changed

+102
-1
lines changed

android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/FlutterUnityWidgetController.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,20 @@ class FlutterUnityWidgetController(
245245
// add unity to view
246246
UnityPlayerUtils.addUnityViewToGroup(view)
247247
UnityPlayerUtils.focus()
248+
248249
attached = true
250+
251+
if (UnityPlayerUtils.controllers.size >= 2) {
252+
for (controller in UnityPlayerUtils.controllers.subList(0, UnityPlayerUtils.controllers.size - 1)) {
253+
if (controller.attached) {
254+
controller.detach()
255+
}
256+
}
257+
}
258+
259+
Handler(Looper.getMainLooper()).post {
260+
methodChannel.invokeMethod("events#onViewAttached", null)
261+
}
249262
}
250263

251264
// DO NOT CHANGE THIS FUNCTION
@@ -289,5 +302,12 @@ class FlutterUnityWidgetController(
289302
Choreographer.getInstance()
290303
.postFrameCallback { f.run() }
291304
}
305+
306+
private fun detach() {
307+
Handler(Looper.getMainLooper()).post {
308+
methodChannel.invokeMethod("events#onViewDetached", null)
309+
}
310+
attached = false
311+
}
292312
//#endregion
293313
}

lib/src/facade_widget.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class UnityWidget extends StatefulWidget {
1616
this.unloadOnDispose = false,
1717
this.printSetupLog = true,
1818
this.onUnityUnloaded,
19+
this.onUnityAttached,
20+
this.onUnityDetached,
1921
this.gestureRecognizers,
2022
this.placeholder,
2123
this.useAndroidViewSurface = false,
@@ -38,6 +40,12 @@ class UnityWidget extends StatefulWidget {
3840
///Event fires when the [UnityWidget] unity player gets unloaded.
3941
final UnityUnloadCallback? onUnityUnloaded;
4042

43+
///Event fires when Unity player is attached to the widget
44+
final UnityAttachedCallback? onUnityAttached;
45+
46+
///Event fires when Unity player is attached to the widget
47+
final UnityDetachedCallback? onUnityDetached;
48+
4149
final Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers;
4250

4351
/// Set to true to force unity to fullscreen

lib/src/helpers/events.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ class UnityCreatedEvent extends UnityEvent<void> {
3434
class UnityMessageEvent extends UnityEvent<dynamic> {
3535
UnityMessageEvent(int unityId, dynamic value) : super(unityId, value);
3636
}
37+
38+
class UnityAttachedEvent extends UnityEvent<void> {
39+
UnityAttachedEvent(int unityId, void value) : super(unityId, value);
40+
}
41+
42+
class UnityDetachedEvent extends UnityEvent<void> {
43+
UnityDetachedEvent(int unityId, void value) : super(unityId, value);
44+
}

lib/src/helpers/misc.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ typedef void UnityMessageCallback(dynamic handler);
2525
typedef void UnitySceneChangeCallback(SceneLoaded? message);
2626

2727
typedef void UnityUnloadCallback();
28+
29+
typedef void UnityAttachedCallback();
30+
31+
typedef void UnityDetachedCallback();

lib/src/helpers/types.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ enum UnityEventTypes {
3636
OnUnityPlayerQuited,
3737
OnUnitySceneLoaded,
3838
OnUnityMessage,
39+
OnViewAttached,
40+
OnViewDetached,
3941
}
4042

4143
class EventDataPayload {

lib/src/io/device_method.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class MethodChannelUnityWidget extends UnityWidgetPlatform {
8484
_unityStreamController
8585
.add(UnitySceneLoadedEvent(0, SceneLoaded.fromMap(event.data)));
8686
break;
87+
case UnityEventTypes.OnViewAttached:
88+
_unityStreamController.add(UnityAttachedEvent(0, event.data));
89+
break;
90+
case UnityEventTypes.OnViewDetached:
91+
_unityStreamController.add(UnityDetachedEvent(0, event.data));
92+
break;
8793
case UnityEventTypes.OnUnityPlayerReInitialize:
8894
case UnityEventTypes.OnViewReattached:
8995
case UnityEventTypes.OnUnityPlayerCreated:
@@ -144,6 +150,16 @@ class MethodChannelUnityWidget extends UnityWidgetPlatform {
144150
return _events(unityId).whereType<UnitySceneLoadedEvent>();
145151
}
146152

153+
@override
154+
Stream<UnityAttachedEvent> onUnityAttached({required int unityId}) {
155+
return _events(unityId).whereType<UnityAttachedEvent>();
156+
}
157+
158+
@override
159+
Stream<UnityDetachedEvent> onUnityDetached({required int unityId}) {
160+
return _events(unityId).whereType<UnityDetachedEvent>();
161+
}
162+
147163
@override
148164
Widget buildViewWithTextDirection(
149165
int creationId,

lib/src/io/mobile_unity_widget_controller.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class MobileUnityWidgetController extends UnityWidgetController {
1414
/// used for cancel the subscription
1515
StreamSubscription? _onUnityMessageSub,
1616
_onUnitySceneLoadedSub,
17-
_onUnityUnloadedSub;
17+
_onUnityUnloadedSub,
18+
_onUnityAttachedSub,
19+
_onUnityDetachedSub;
1820

1921
MobileUnityWidgetController._(
2022
this.unityWidgetState, {
@@ -34,6 +36,8 @@ class MobileUnityWidgetController extends UnityWidgetController {
3436
unityWidgetState,
3537
unityId: id,
3638
);
39+
await UnityWidgetPlatform.instance.init(id);
40+
return controller;
3741
}
3842

3943
void _connectStreams(int unityId) {
@@ -63,6 +67,22 @@ class MobileUnityWidgetController extends UnityWidgetController {
6367
unityWidgetState.widget.onUnityUnloaded!();
6468
});
6569
}
70+
71+
if (_unityWidgetState.widget.onUnityAttached != null) {
72+
_onUnityAttachedSub = UnityWidgetPlatform.instance
73+
.onUnityAttached(unityId: unityId)
74+
.listen((_) {
75+
_unityWidgetState.widget.onUnityAttached!();
76+
});
77+
}
78+
79+
if (_unityWidgetState.widget.onUnityDetached != null) {
80+
_onUnityDetachedSub = UnityWidgetPlatform.instance
81+
.onUnityDetached(unityId: unityId)
82+
.listen((_) {
83+
_unityWidgetState.widget.onUnityDetached!();
84+
});
85+
}
6686
}
6787

6888
/// Checks to see if unity player is ready to be used
@@ -200,10 +220,14 @@ class MobileUnityWidgetController extends UnityWidgetController {
200220
_onUnityMessageSub?.cancel();
201221
_onUnitySceneLoadedSub?.cancel();
202222
_onUnityUnloadedSub?.cancel();
223+
_onUnityAttachedSub?.cancel();
224+
_onUnityDetachedSub?.cancel();
203225

204226
_onUnityMessageSub = null;
205227
_onUnitySceneLoadedSub = null;
206228
_onUnityUnloadedSub = null;
229+
_onUnityAttachedSub = null;
230+
_onUnityDetachedSub = null;
207231
}
208232

209233
void dispose() {

lib/src/io/unity_widget.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class UnityWidget extends StatefulWidget {
6161
this.unloadOnDispose = false,
6262
this.printSetupLog = true,
6363
this.onUnityUnloaded,
64+
this.onUnityAttached,
65+
this.onUnityDetached,
6466
this.gestureRecognizers,
6567
this.placeholder,
6668
this.useAndroidViewSurface = false,
@@ -87,6 +89,12 @@ class UnityWidget extends StatefulWidget {
8789
///Event fires when the [UnityWidget] unity player gets unloaded.
8890
final UnityUnloadCallback? onUnityUnloaded;
8991

92+
///Event fires when Unity player is attached to the widget
93+
final UnityAttachedCallback? onUnityAttached;
94+
95+
///Event fires when Unity player is detached to the widget
96+
final UnityDetachedCallback? onUnityDetached;
97+
9098
final Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers;
9199

92100
/// Set to true to force unity to fullscreen
@@ -195,6 +203,9 @@ class _UnityWidgetState extends State<UnityWidget> {
195203
widget.onUnityCreated!(controller);
196204
}
197205

206+
// if (widget.onUnityAttached != null)
207+
// widget.onUnityAttached!();
208+
198209
if (widget.printSetupLog) {
199210
log('*********************************************');
200211
log('** flutter unity controller setup complete **');

lib/src/io/unity_widget_platform.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ abstract class UnityWidgetPlatform extends PlatformInterface {
107107
throw UnimplementedError('onUnitySceneLoaded() has not been implemented.');
108108
}
109109

110+
Stream<UnityAttachedEvent> onUnityAttached({required int unityId}) {
111+
throw UnimplementedError('onUnityAttached() has not been implemented.');
112+
}
113+
114+
Stream<UnityDetachedEvent> onUnityDetached({required int unityId}) {
115+
throw UnimplementedError('onUnityDetached() has not been implemented.');
116+
}
117+
110118
/// Dispose of whatever resources the `unityId` is holding on to.
111119
void dispose({required int unityId}) {
112120
throw UnimplementedError('dispose() has not been implemented.');

0 commit comments

Comments
 (0)