2019-08-01 04:43:34 -04:00
|
|
|
//
|
|
|
|
// FlutterUnityWidgetPlugin.m
|
|
|
|
// FlutterUnityWidgetPlugin
|
|
|
|
//
|
|
|
|
// Created by Kris Pypen on 8/1/19.
|
|
|
|
//
|
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
#import "FlutterUnityWidgetPlugin.h"
|
2019-08-01 04:43:34 -04:00
|
|
|
#import "UnityUtils.h"
|
|
|
|
#import "FlutterUnityView.h"
|
|
|
|
|
|
|
|
#include <UnityFramework/UnityFramework.h>
|
2019-03-09 10:47:09 -05:00
|
|
|
|
2019-08-01 04:43:34 -04:00
|
|
|
@implementation FlutterUnityWidgetPlugin
|
2019-03-26 17:06:17 -04:00
|
|
|
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
2019-08-01 04:43:34 -04:00
|
|
|
FUViewFactory* fuviewFactory = [[FUViewFactory alloc] initWithRegistrar:registrar];
|
|
|
|
[registrar registerViewFactory:fuviewFactory withId:@"unity_view"];
|
2019-03-26 17:06:17 -04:00
|
|
|
}
|
2019-08-01 04:43:34 -04:00
|
|
|
@end
|
2019-03-26 17:06:17 -04:00
|
|
|
|
2019-08-01 04:43:34 -04:00
|
|
|
@implementation FUViewFactory {
|
|
|
|
NSObject<FlutterPluginRegistrar>* _registrar;
|
|
|
|
}
|
|
|
|
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_registrar = registrar;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
- (NSObject<FlutterMessageCodec>*)createArgsCodec {
|
|
|
|
return [FlutterStandardMessageCodec sharedInstance];
|
|
|
|
}
|
2019-03-26 17:06:17 -04:00
|
|
|
|
2019-08-01 04:43:34 -04:00
|
|
|
- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
|
|
|
|
viewIdentifier:(int64_t)viewId
|
|
|
|
arguments:(id _Nullable)args {
|
|
|
|
FUController* controller = [[FUController alloc] initWithFrame:frame
|
|
|
|
viewIdentifier:viewId
|
|
|
|
arguments:args
|
2019-08-01 10:36:58 -04:00
|
|
|
registrar:_registrar];
|
2019-08-01 04:43:34 -04:00
|
|
|
return controller;
|
|
|
|
}
|
2019-03-26 17:06:17 -04:00
|
|
|
|
2019-08-01 04:43:34 -04:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FUController {
|
|
|
|
FlutterUnityView* _uView;
|
|
|
|
int64_t _viewId;
|
|
|
|
FlutterMethodChannel* _channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
viewIdentifier:(int64_t)viewId
|
2019-08-01 10:36:58 -04:00
|
|
|
arguments:(id _Nullable)args
|
|
|
|
registrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
2019-08-01 04:43:34 -04:00
|
|
|
if ([super init]) {
|
|
|
|
_viewId = viewId;
|
|
|
|
|
|
|
|
NSString* channelName = [NSString stringWithFormat:@"unity_view_%lld", viewId];
|
2019-08-01 10:36:58 -04:00
|
|
|
_channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:registrar.messenger];
|
|
|
|
__weak __typeof__(self) weakSelf = self;
|
|
|
|
[_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
|
|
|
|
[weakSelf onMethodCall:call result:result];
|
|
|
|
}];
|
2019-08-01 04:43:34 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-08-01 10:36:58 -04:00
|
|
|
- (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
|
|
|
|
if ([[call method] isEqualToString:@"postMessage"]) {
|
|
|
|
[self postMessage:call result:result];
|
|
|
|
} else {
|
|
|
|
result(FlutterMethodNotImplemented);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)postMessage:(FlutterMethodCall*)call result:(FlutterResult)result {
|
|
|
|
NSString* object = [call arguments][@"gameObject"];
|
|
|
|
NSString* method = [call arguments][@"methodName"];
|
|
|
|
NSString* message = [call arguments][@"message"];
|
|
|
|
|
|
|
|
UnityPostMessage(object, method, message);
|
|
|
|
|
|
|
|
result(nil);
|
|
|
|
}
|
|
|
|
|
2019-08-01 04:43:34 -04:00
|
|
|
- (UIView*)view {
|
|
|
|
_uView = [[FlutterUnityView alloc] init];
|
|
|
|
if ([UnityUtils isUnityReady]) {
|
|
|
|
[_uView setUnityView: (UIView*)[GetAppController() unityView]];
|
|
|
|
} else {
|
|
|
|
[UnityUtils createPlayer:^{
|
|
|
|
[_uView setUnityView: (UIView*)[GetAppController() unityView]];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
return _uView;
|
2019-03-09 10:47:09 -05:00
|
|
|
}
|
2019-08-01 04:43:34 -04:00
|
|
|
|
2019-03-09 10:47:09 -05:00
|
|
|
@end
|
2019-08-01 04:43:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|