diff --git a/example/unity/DemoApp/Assets/Editor/XCodePostBuild.cs b/example/unity/DemoApp/Assets/Editor/XCodePostBuild.cs index 42fee74..778fc1b 100644 --- a/example/unity/DemoApp/Assets/Editor/XCodePostBuild.cs +++ b/example/unity/DemoApp/Assets/Editor/XCodePostBuild.cs @@ -155,6 +155,33 @@ public static class XcodePostBuild inScope = false; markerDetected = false; + // Modify inline GetAppController + EditCodeFile(path, line => + { + inScope |= line.Contains("quitHandler)"); + + if (inScope && !markerDetected) + { + if (line.Trim() == "") + { + inScope = false; + markerDetected = true; + + return new string[] + { + "@property (nonatomic, copy) void(^unityMessageHandler)(const char* message);", + }; + } + + return new string[] { line }; + } + + return new string[] { line }; + }); + + inScope = false; + markerDetected = false; + // Add static GetAppController EditCodeFile(path, line => { @@ -171,8 +198,6 @@ public static class XcodePostBuild "", "// Added by " + TouchedMarker, "+ (UnityAppController*)GetAppController;", - "+ (void)addUnityEventListenerInternal:(id)listener;", - "+ (void)removeUnityEventListenerInternal:(id)listener;", "" }; } @@ -206,17 +231,7 @@ public static class XcodePostBuild " return [UnityAppController GetAppController];", "}", "", - "// Added by " + TouchedMarker, - "static inline void addUnityEventListenerInternal(id listener)", - "{", - " [UnityAppController addUnityEventListenerInternal: listener];", - "}", - "", - "// Added by " + TouchedMarker, - "static inline void removeUnityEventListenerInternal(id listener)", - "{", - " [UnityAppController removeUnityEventListenerInternal:listener];", - "}" + }; } @@ -257,25 +272,14 @@ public static class XcodePostBuild "}", "", "// Added by " + TouchedMarker, - "static NSHashTable* mUnityEventListeners = [NSHashTable weakObjectsHashTable];", - "+ (void)addUnityEventListener2:(id)listener", - "{", - " [mUnityEventListeners addObject: listener];", - "}", - "", - "// Added by " + TouchedMarker, - "+(void)removeUnityEventListener2:(id)listener", - "{", - " [mUnityEventListeners removeObject: listener];", - "}", - line, - "// Added by " + TouchedMarker, "extern \"C\" void onUnityMessage(const char* message)", "{", - " for (id listener in mUnityEventListeners) {", - " [listener onMessage:[NSString stringWithUTF8String:message]];", + " if (GetAppController().unityMessageHandler) {", + " GetAppController().unityMessageHandler(message);", " }", "}", + line, + }; } @@ -330,6 +334,33 @@ public static class XcodePostBuild return new string[] { line }; }); + + inScope = false; + markerDetected = false; + + // Modify inline GetAppController + EditCodeFile(path, line => + { + inScope |= line.Contains("@synthesize quitHandler"); + + if (inScope && !markerDetected) + { + if (line.Trim() == "") + { + inScope = false; + markerDetected = true; + + return new string[] + { + "@synthesize unityMessageHandler = _unityMessageHandler;", + }; + } + + return new string[] { line }; + } + + return new string[] { line }; + }); } /// diff --git a/example/unity/DemoApp/obj/Debug/Assembly-CSharp-Editor.csprojAssemblyReference.cache b/example/unity/DemoApp/obj/Debug/Assembly-CSharp-Editor.csprojAssemblyReference.cache index 82bed45..4a34a76 100644 Binary files a/example/unity/DemoApp/obj/Debug/Assembly-CSharp-Editor.csprojAssemblyReference.cache and b/example/unity/DemoApp/obj/Debug/Assembly-CSharp-Editor.csprojAssemblyReference.cache differ diff --git a/example/unity/DemoApp/obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache b/example/unity/DemoApp/obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache index 5c4a556..daa348a 100644 Binary files a/example/unity/DemoApp/obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache and b/example/unity/DemoApp/obj/Debug/Assembly-CSharp.csprojAssemblyReference.cache differ diff --git a/ios/Classes/FlutterUnityWidgetPlugin.h b/ios/Classes/FlutterUnityWidgetPlugin.h index 013da00..a3eff36 100644 --- a/ios/Classes/FlutterUnityWidgetPlugin.h +++ b/ios/Classes/FlutterUnityWidgetPlugin.h @@ -6,11 +6,12 @@ // #import +#import @interface FlutterUnityWidgetPlugin : NSObject @end -@interface FUController : NSObject +@interface FUController : NSObject - (instancetype)initWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId @@ -18,6 +19,7 @@ registrar:(NSObject *)registrar; - (UIView*)view; +- (void)onMessage:(NSString *)message; @end @interface FUViewFactory : NSObject diff --git a/ios/Classes/FlutterUnityWidgetPlugin.m b/ios/Classes/FlutterUnityWidgetPlugin.m index 4febb23..7a71162 100644 --- a/ios/Classes/FlutterUnityWidgetPlugin.m +++ b/ios/Classes/FlutterUnityWidgetPlugin.m @@ -63,11 +63,14 @@ [_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { [weakSelf onMethodCall:call result:result]; }]; - } return self; } +- (void)onMessage:(NSString *)message { + [_channel invokeMethod:@"onUnityMessage" arguments:message]; +} + - (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if ([[call method] isEqualToString:@"postMessage"]) { [self postMessage:call result:result]; @@ -94,6 +97,10 @@ [UnityUtils createPlayer:^{ [_uView setUnityView: (UIView*)[GetAppController() unityView]]; }]; + [GetAppController() setUnityMessageHandler: ^(const char* message) + { + [_channel invokeMethod:@"onUnityMessage" arguments:[NSString stringWithUTF8String:message]]; + }]; } return _uView; } diff --git a/ios/Classes/UnityUtils.h b/ios/Classes/UnityUtils.h index 31fdca5..ea3420c 100644 --- a/ios/Classes/UnityUtils.h +++ b/ios/Classes/UnityUtils.h @@ -23,6 +23,7 @@ extern "C" { } // extern "C" #endif + @protocol UnityEventListener - (void)onMessage:(NSString *)message; @end @@ -31,8 +32,6 @@ extern "C" { + (BOOL)isUnityReady; + (void)createPlayer:(void (^)(void))completed; -+ (void)addUnityEventListener:(id)listener; -+ (void)removeUnityEventListener:(id)listener; @end diff --git a/ios/Classes/UnityUtils.mm b/ios/Classes/UnityUtils.mm index 7d59064..348b636 100644 --- a/ios/Classes/UnityUtils.mm +++ b/ios/Classes/UnityUtils.mm @@ -153,26 +153,9 @@ static BOOL _isUnityReady = NO; UnityAppController *controller = GetAppController(); [controller application:application didFinishLaunchingWithOptions:nil]; [controller applicationDidBecomeActive:application]; - + [UnityUtils listenAppState]; }); } -extern "C" void onUnityMessage(const char* message) -{ - for (id listener in mUnityEventListeners) { - [listener onMessage:[NSString stringWithUTF8String:message]]; - } -} - -+ (void)addUnityEventListener:(id)listener -{ - [mUnityEventListeners addObject:listener]; -} - -+ (void)removeUnityEventListener:(id)listener -{ - [mUnityEventListeners removeObject:listener]; -} - @end