This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
flutter-unity-view-widget/lib/flutter_unity_widget.dart

145 lines
4.0 KiB
Dart
Raw Permalink Normal View History

2019-03-09 10:47:09 -05:00
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2019-03-10 18:53:08 -04:00
typedef void UnityWidgetCreatedCallback(UnityWidgetController controller);
2019-03-09 10:47:09 -05:00
class UnityWidgetController {
final _UnityWidgetState _unityWidgetState;
final MethodChannel channel;
UnityWidgetController._(
this.channel,
this._unityWidgetState,
) {
channel.setMethodCallHandler(_handleMethod);
2019-03-28 14:05:38 -04:00
}
2019-03-26 17:06:17 -04:00
static UnityWidgetController init(int id, _UnityWidgetState unityWidgetState) {
final MethodChannel channel = MethodChannel('unity_view_$id');
return UnityWidgetController._(
channel, unityWidgetState,
);
2019-03-09 10:47:09 -05:00
}
/// Returns true if Unity is ready and false otherwise.
Future<bool> isReady() => channel.invokeMethod('isReady');
2019-03-10 18:53:08 -04:00
/// Returns true once Unity is ready.
Future<bool> createUnity() => channel.invokeMethod('createUnity');
2019-03-10 18:53:08 -04:00
2020-06-09 15:58:18 -04:00
Future<bool> postMessage(String gameObject, String methodName, String message) => channel.invokeMethod('postMessage', <String, dynamic>{
'gameObject': gameObject,
'methodName': methodName,
'message': message,
});
2019-03-10 18:53:08 -04:00
/// Unless an error is thrown, always returns true.
Future<bool> pause() {
2019-03-28 14:05:38 -04:00
print('Pressed paused');
return channel.invokeMethod('pause');
2019-03-10 18:53:08 -04:00
}
/// Unless an error is thrown, always returns true.
Future<bool> resume() => channel.invokeMethod('resume');
2019-03-26 17:06:17 -04:00
/// Called from [UnityWidget.dispose]
2019-03-28 14:05:38 -04:00
Future<void> _dispose() async {
// await channel.invokeMethod('dispose');
2019-03-28 14:05:38 -04:00
}
/// Handles a method call from Android (on behalf of Unity) to Flutter.
2019-03-26 17:06:17 -04:00
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "onUnityMessage":
if (_unityWidgetState.widget != null) {
_unityWidgetState.widget.onUnityMessage(this, call.arguments);
}
2019-03-26 17:06:17 -04:00
break;
default:
throw UnimplementedError("Unimplemented ${call.method} method");
}
}
2019-03-09 10:47:09 -05:00
}
2019-03-26 17:06:17 -04:00
typedef onUnityMessageCallback = void Function(
UnityWidgetController controller, dynamic handler);
2019-03-09 10:47:09 -05:00
class UnityWidget extends StatefulWidget {
2019-03-26 17:06:17 -04:00
final UnityWidgetCreatedCallback onUnityViewCreated;
2019-03-09 10:47:09 -05:00
2019-03-26 17:06:17 -04:00
///Event fires when the [UnityWidget] gets a message from unity.
final onUnityMessageCallback onUnityMessage;
2019-03-09 10:47:09 -05:00
2019-07-31 03:46:56 -04:00
final bool isARScene;
2019-03-26 17:06:17 -04:00
UnityWidget(
2019-07-31 03:46:56 -04:00
{Key key, @required this.onUnityViewCreated, this.onUnityMessage, this.isARScene = false});
2019-03-09 10:47:09 -05:00
@override
_UnityWidgetState createState() => _UnityWidgetState();
}
class _UnityWidgetState extends State<UnityWidget> {
2019-03-28 14:05:38 -04:00
UnityWidgetController _controller;
@override
void initState() {
// widget.controller =
2019-03-28 14:05:38 -04:00
super.initState();
}
@override
2019-03-28 14:05:38 -04:00
void dispose() {
super.dispose();
if (_controller != null) {
_controller._dispose();
_controller = null;
}
}
2019-03-09 10:47:09 -05:00
@override
Widget build(BuildContext context) {
2019-07-31 03:46:56 -04:00
final Map<String, dynamic> creationParams = <String, dynamic>{
'ar': widget.isARScene,
};
2019-03-26 17:06:17 -04:00
if (defaultTargetPlatform == TargetPlatform.android) {
2019-03-09 10:47:09 -05:00
return AndroidView(
2019-03-10 18:53:08 -04:00
viewType: 'unity_view',
2019-03-28 14:05:38 -04:00
onPlatformViewCreated: _onPlatformViewCreated,
2019-03-09 10:47:09 -05:00
creationParamsCodec: const StandardMessageCodec(),
2019-07-31 03:46:56 -04:00
creationParams: creationParams,
2019-03-09 10:47:09 -05:00
);
2019-03-26 17:06:17 -04:00
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
2019-03-09 10:47:09 -05:00
return UiKitView(
2019-03-10 18:53:08 -04:00
viewType: 'unity_view',
2019-03-28 14:05:38 -04:00
onPlatformViewCreated: _onPlatformViewCreated,
2019-03-09 10:47:09 -05:00
creationParamsCodec: const StandardMessageCodec(),
);
}
2019-03-26 17:06:17 -04:00
return new Text(
'$defaultTargetPlatform is not yet supported by this plugin');
2019-03-09 10:47:09 -05:00
}
2019-03-28 14:05:38 -04:00
@override
void didUpdateWidget(UnityWidget oldWidget) {
super.didUpdateWidget(oldWidget);
}
void _onPlatformViewCreated(int id) {
_controller = UnityWidgetController.init(id, this);
print('--------------------------------------------');
print('| Internal setup complete ');
print('--------------------------------------------');
2019-03-28 14:05:38 -04:00
if (widget.onUnityViewCreated != null) {
widget.onUnityViewCreated(_controller);
2019-03-09 10:47:09 -05:00
}
}
}