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 {
|
|
|
|
static MethodChannel _channel =
|
2019-03-10 18:53:08 -04:00
|
|
|
const MethodChannel('unity_view');
|
2019-03-09 10:47:09 -05:00
|
|
|
|
|
|
|
UnityWidgetController();
|
|
|
|
|
|
|
|
init(int id) {
|
2019-03-10 18:53:08 -04:00
|
|
|
_channel = new MethodChannel('unity_view');
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:53:08 -04:00
|
|
|
Future<bool> isReady() async {
|
|
|
|
final bool isReady = await _channel.invokeMethod('isReady');
|
|
|
|
return isReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> createUnity() async {
|
|
|
|
final bool isReady = await _channel.invokeMethod('createUnity');
|
|
|
|
return isReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
postMessage(String gameObject, methodName, message){
|
|
|
|
_channel.invokeMethod('postMessage', [gameObject, methodName, message]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pause() async{
|
|
|
|
await _channel.invokeMethod('pause');
|
|
|
|
}
|
|
|
|
|
|
|
|
resume() async{
|
|
|
|
await _channel.invokeMethod('resume');
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnityWidget extends StatefulWidget {
|
|
|
|
|
2019-03-10 18:53:08 -04:00
|
|
|
UnityWidgetCreatedCallback onUnityViewCreated;
|
2019-03-09 10:47:09 -05:00
|
|
|
|
|
|
|
UnityWidget({
|
|
|
|
Key key,
|
|
|
|
@required this.onUnityViewCreated,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_UnityWidgetState createState() => _UnityWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UnityWidgetState extends State<UnityWidget> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if(defaultTargetPlatform == TargetPlatform.android) {
|
|
|
|
return AndroidView(
|
2019-03-10 18:53:08 -04:00
|
|
|
viewType: 'unity_view',
|
2019-03-09 10:47:09 -05:00
|
|
|
onPlatformViewCreated: onPlatformViewCreated,
|
|
|
|
creationParamsCodec: const StandardMessageCodec(),
|
|
|
|
|
|
|
|
);
|
|
|
|
} else if(defaultTargetPlatform == TargetPlatform.iOS) {
|
|
|
|
return UiKitView(
|
2019-03-10 18:53:08 -04:00
|
|
|
viewType: 'unity_view',
|
2019-03-09 10:47:09 -05:00
|
|
|
onPlatformViewCreated: onPlatformViewCreated,
|
|
|
|
creationParamsCodec: const StandardMessageCodec(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Text('$defaultTargetPlatform is not yet supported by this plugin');
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> onPlatformViewCreated(id) async {
|
|
|
|
if (widget.onUnityViewCreated == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-10 18:53:08 -04:00
|
|
|
widget.onUnityViewCreated(new UnityWidgetController().init(id));
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|