Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ example/unity/DemoApp/Temp/
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
!/unitypackages/flutter_tools/test/data/dart_dependencies_test/**/.packages

example/unity/DemoApp/test
example/ios/UnityLibrary
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.1.0-null-safe

* Fixed bitcode enabled issue on iOS. [369](https://github.com/juicycleff/flutter-unity-view-widget/issues/369)

## 4.1.0

* Fixed bitcode enabled issue on iOS. [369](https://github.com/juicycleff/flutter-unity-view-widget/issues/369)
Expand Down
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
analyzer:
enable-experiment:
- non-nullable
40 changes: 19 additions & 21 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ class UnityWidgetController {
final int unityId;

/// used for cancel the subscription
StreamSubscription _onUnityMessageSub,
StreamSubscription? _onUnityMessageSub,
_onUnitySceneLoadedSub,
_onUnityUnloadedSub;

UnityWidgetController._(this._unityWidgetState, {@required this.unityId})
: assert(_unityViewFlutterPlatform != null) {
UnityWidgetController._(this._unityWidgetState, {required this.unityId}) {
_connectStreams(unityId);
}

Expand All @@ -26,7 +25,6 @@ class UnityWidgetController {
/// in [UnityWidget.onUnityCreated] callback.
static Future<UnityWidgetController> init(
int id, _UnityWidgetState unityWidgetState) async {
assert(id != null);
await _unityViewFlutterPlatform.init(id);
return UnityWidgetController._(
unityWidgetState,
Expand All @@ -35,7 +33,7 @@ class UnityWidgetController {
}

@visibleForTesting
MethodChannel get channel {
MethodChannel? get channel {
if (_unityViewFlutterPlatform is MethodChannelUnityViewFlutter) {
return (_unityViewFlutterPlatform as MethodChannelUnityViewFlutter)
.channel(unityId);
Expand All @@ -48,26 +46,26 @@ class UnityWidgetController {
_onUnityMessageSub = _unityViewFlutterPlatform
.onUnityMessage(unityId: unityId)
.listen((UnityMessageEvent e) =>
_unityWidgetState.widget.onUnityMessage(e.value));
_unityWidgetState.widget.onUnityMessage!(e.value));
}

if (_unityWidgetState.widget.onUnitySceneLoaded != null) {
_onUnitySceneLoadedSub = _unityViewFlutterPlatform
.onUnitySceneLoaded(unityId: unityId)
.listen((UnitySceneLoadedEvent e) =>
_unityWidgetState.widget.onUnitySceneLoaded(e.value));
_unityWidgetState.widget.onUnitySceneLoaded!(e.value));
}

if (_unityWidgetState.widget.onUnityUnloaded != null) {
_onUnityUnloadedSub = _unityViewFlutterPlatform
.onUnityUnloaded(unityId: unityId)
.listen((_) => _unityWidgetState.widget.onUnityUnloaded());
.listen((_) => _unityWidgetState.widget.onUnityUnloaded!());
}
}

/// Checks to see if unity player is ready to be used
/// Returns `true` if unity player is ready.
Future<bool> isReady() {
Future<bool?>? isReady() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.isReady(unityId: unityId);
}
Expand All @@ -76,7 +74,7 @@ class UnityWidgetController {

/// Get the current pause state of the unity player
/// Returns `true` if unity player is paused.
Future<bool> isPaused() {
Future<bool?>? isPaused() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.isPaused(unityId: unityId);
}
Expand All @@ -85,7 +83,7 @@ class UnityWidgetController {

/// Get the current load state of the unity player
/// Returns `true` if unity player is loaded.
Future<bool> isLoaded() {
Future<bool?>? isLoaded() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.isLoaded(unityId: unityId);
}
Expand All @@ -94,7 +92,7 @@ class UnityWidgetController {

/// Helper method to know if Unity has been put in background mode (WIP) unstable
/// Returns `true` if unity player is in background.
Future<bool> inBackground() {
Future<bool?>? inBackground() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.inBackground(unityId: unityId);
}
Expand All @@ -104,7 +102,7 @@ class UnityWidgetController {
/// Creates a unity player if it's not already created. Please only call this if unity is not ready,
/// or is in unloaded state. Use [isLoaded] to check.
/// Returns `true` if unity player was created succesfully.
Future<bool> create() {
Future<bool?>? create() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.createUnityPlayer(unityId: unityId);
}
Expand All @@ -118,7 +116,7 @@ class UnityWidgetController {
/// ```dart
/// postMessage("GameManager", "openScene", "ThirdScene")
/// ```
Future<void> postMessage(String gameObject, methodName, message) {
Future<void>? postMessage(String gameObject, methodName, message) {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.postMessage(
unityId: unityId,
Expand All @@ -137,7 +135,7 @@ class UnityWidgetController {
/// ```dart
/// postJsonMessage("GameManager", "openScene", {"buildIndex": 3, "name": "ThirdScene"})
/// ```
Future<void> postJsonMessage(
Future<void>? postJsonMessage(
String gameObject, String methodName, Map<String, dynamic> message) {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.postJsonMessage(
Expand All @@ -151,15 +149,15 @@ class UnityWidgetController {
}

/// Pause the unity in-game player with this method
Future<void> pause() {
Future<void>? pause() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.pausePlayer(unityId: unityId);
}
return null;
}

/// Resume the unity in-game player with this method idf it is in a paused state
Future<void> resume() {
Future<void>? resume() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.resumePlayer(unityId: unityId);
}
Expand All @@ -168,7 +166,7 @@ class UnityWidgetController {

/// Sometimes you want to open unity in it's own process and openInNativeProcess does just that.
/// It works for Android and iOS is WIP
Future<void> openInNativeProcess() {
Future<void>? openInNativeProcess() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.openInNativeProcess(unityId: unityId);
}
Expand All @@ -177,15 +175,15 @@ class UnityWidgetController {

/// Unloads unity player from th current process (Works on Android only for now)
/// iOS is WIP. For more information please read [Unity Docs](https://docs.unity3d.com/2020.2/Documentation/Manual/UnityasaLibrary.html)
Future<void> unload() {
Future<void>? unload() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.unloadPlayer(unityId: unityId);
}
return null;
}

/// Quits unity player. Note that this kills the current flutter process, thus quiting the app
Future<void> quit() {
Future<void>? quit() {
if (!_unityWidgetState.widget.enablePlaceholder) {
return _unityViewFlutterPlatform.quitPlayer(unityId: unityId);
}
Expand All @@ -211,6 +209,6 @@ class UnityWidgetController {

typedef void UnityMessageCallback(dynamic handler);

typedef void UnitySceneChangeCallback(SceneLoaded message);
typedef void UnitySceneChangeCallback(SceneLoaded? message);

typedef void UnityUnloadCallback();
82 changes: 41 additions & 41 deletions lib/src/device_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ part of flutter_unity_widget;

class MethodChannelUnityViewFlutter extends UnityViewFlutterPlatform {
// Every method call passes the int unityId
final Map<int, MethodChannel> _channels = {};
final Map<int, MethodChannel?> _channels = {};

/// Accesses the MethodChannel associated to the passed unityId.
MethodChannel channel(int unityId) {
MethodChannel? channel(int unityId) {
return _channels[unityId];
}

Expand All @@ -14,19 +14,19 @@ class MethodChannelUnityViewFlutter extends UnityViewFlutterPlatform {
/// This method is called when the plugin is first initialized.
@override
Future<void> init(int unityId) {
MethodChannel channel;
MethodChannel? channel;
if (!_channels.containsKey(unityId)) {
channel = MethodChannel('plugin.xraph.com/unity_view_$unityId');
channel.setMethodCallHandler(
(MethodCall call) => _handleMethodCall(call, unityId));
_channels[unityId] = channel;
}
return channel.invokeMethod<void>('unity#waitForUnity');
return channel!.invokeMethod<void>('unity#waitForUnity');
}

/// Dispose of the native resources.
@override
Future<void> dispose({int unityId}) async {
Future<void> dispose({int? unityId}) async {
// TODO [Rex] will fix this
// await channel(unityId).invokeMethod('unity#dispose');
}
Expand Down Expand Up @@ -64,54 +64,54 @@ class MethodChannelUnityViewFlutter extends UnityViewFlutterPlatform {
}

@override
Future<bool> isPaused({@required int unityId}) async {
return await channel(unityId).invokeMethod('unity#isPaused');
Future<bool?> isPaused({required int unityId}) async {
return await channel(unityId)!.invokeMethod('unity#isPaused');
}

@override
Future<bool> isReady({@required int unityId}) async {
return await channel(unityId).invokeMethod('unity#isReady');
Future<bool?> isReady({required int unityId}) async {
return await channel(unityId)!.invokeMethod('unity#isReady');
}

@override
Future<bool> isLoaded({@required int unityId}) async {
return await channel(unityId).invokeMethod('unity#isLoaded');
Future<bool?> isLoaded({required int unityId}) async {
return await channel(unityId)!.invokeMethod('unity#isLoaded');
}

@override
Future<bool> inBackground({@required int unityId}) async {
return await channel(unityId).invokeMethod('unity#inBackground');
Future<bool?> inBackground({required int unityId}) async {
return await channel(unityId)!.invokeMethod('unity#inBackground');
}

@override
Future<bool> createUnityPlayer({@required int unityId}) async {
return await channel(unityId).invokeMethod('unity#createPlayer');
Future<bool?> createUnityPlayer({required int unityId}) async {
return await channel(unityId)!.invokeMethod('unity#createPlayer');
}

@override
Stream<UnityMessageEvent> onUnityMessage({@required int unityId}) {
Stream<UnityMessageEvent> onUnityMessage({required int unityId}) {
return _events(unityId).whereType<UnityMessageEvent>();
}

@override
Stream<UnityLoadedEvent> onUnityUnloaded({@required int unityId}) {
Stream<UnityLoadedEvent> onUnityUnloaded({required int unityId}) {
return _events(unityId).whereType<UnityLoadedEvent>();
}

@override
Stream<UnityCreatedEvent> onUnityCreated({@required int unityId}) {
Stream<UnityCreatedEvent> onUnityCreated({required int unityId}) {
return _events(unityId).whereType<UnityCreatedEvent>();
}

@override
Stream<UnitySceneLoadedEvent> onUnitySceneLoaded({@required int unityId}) {
Stream<UnitySceneLoadedEvent> onUnitySceneLoaded({required int unityId}) {
return _events(unityId).whereType<UnitySceneLoadedEvent>();
}

@override
Widget buildView(
Map<String, dynamic> creationParams,
Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers,
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers,
PlatformViewCreatedCallback onPlatformViewCreated,
bool useAndroidView) {
final String _viewType = 'plugin.xraph.com/unity_view';
Expand All @@ -133,7 +133,7 @@ class MethodChannelUnityViewFlutter extends UnityViewFlutterPlatform {
surfaceFactory:
(BuildContext context, PlatformViewController controller) {
return AndroidViewSurface(
controller: controller,
controller: controller as AndroidViewController,
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
Expand Down Expand Up @@ -166,11 +166,11 @@ class MethodChannelUnityViewFlutter extends UnityViewFlutterPlatform {

@override
Future<void> postMessage(
{@required int unityId,
@required String gameObject,
@required String methodName,
@required String message}) async {
await channel(unityId).invokeMethod('unity#postMessage', <String, dynamic>{
{required int unityId,
required String gameObject,
required String methodName,
required String message}) async {
await channel(unityId)!.invokeMethod('unity#postMessage', <String, dynamic>{
'gameObject': gameObject,
'methodName': methodName,
'message': message,
Expand All @@ -179,39 +179,39 @@ class MethodChannelUnityViewFlutter extends UnityViewFlutterPlatform {

@override
Future<void> postJsonMessage(
{@required int unityId,
@required String gameObject,
@required String methodName,
@required Map message}) async {
await channel(unityId).invokeMethod('unity#postMessage', <String, dynamic>{
{required int unityId,
required String gameObject,
required String methodName,
required Map message}) async {
await channel(unityId)!.invokeMethod('unity#postMessage', <String, dynamic>{
'gameObject': gameObject,
'methodName': methodName,
'message': json.encode(message),
});
}

@override
Future<void> pausePlayer({@required int unityId}) async {
await channel(unityId).invokeMethod('unity#pausePlayer');
Future<void> pausePlayer({required int unityId}) async {
await channel(unityId)!.invokeMethod('unity#pausePlayer');
}

@override
Future<void> resumePlayer({@required int unityId}) async {
await channel(unityId).invokeMethod('unity#resumePlayer');
Future<void> resumePlayer({required int unityId}) async {
await channel(unityId)!.invokeMethod('unity#resumePlayer');
}

@override
Future<void> openInNativeProcess({@required int unityId}) async {
await channel(unityId).invokeMethod('unity#openInNativeProcess');
Future<void> openInNativeProcess({required int unityId}) async {
await channel(unityId)!.invokeMethod('unity#openInNativeProcess');
}

@override
Future<void> unloadPlayer({@required int unityId}) async {
await channel(unityId).invokeMethod('unity#unloadPlayer');
Future<void> unloadPlayer({required int unityId}) async {
await channel(unityId)!.invokeMethod('unity#unloadPlayer');
}

@override
Future<void> quitPlayer({@required int unityId}) async {
await channel(unityId).invokeMethod('unity#quitPlayer');
Future<void> quitPlayer({required int unityId}) async {
await channel(unityId)!.invokeMethod('unity#quitPlayer');
}
}
4 changes: 2 additions & 2 deletions lib/src/helpers/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class UnityEvent<T> {
UnityEvent(this.unityId, this.value);
}

class UnitySceneLoadedEvent extends UnityEvent<SceneLoaded> {
UnitySceneLoadedEvent(int unityId, SceneLoaded value) : super(unityId, value);
class UnitySceneLoadedEvent extends UnityEvent<SceneLoaded?> {
UnitySceneLoadedEvent(int unityId, SceneLoaded? value) : super(unityId, value);
}

class UnityLoadedEvent extends UnityEvent<void> {
Expand Down
Loading