rework onunitymessage for ios
This commit is contained in:
parent
db637ce73d
commit
ea71d3b2be
|
@ -155,6 +155,33 @@ public static class XcodePostBuild
|
||||||
inScope = false;
|
inScope = false;
|
||||||
markerDetected = 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
|
// Add static GetAppController
|
||||||
EditCodeFile(path, line =>
|
EditCodeFile(path, line =>
|
||||||
{
|
{
|
||||||
|
@ -171,8 +198,6 @@ public static class XcodePostBuild
|
||||||
"",
|
"",
|
||||||
"// Added by " + TouchedMarker,
|
"// Added by " + TouchedMarker,
|
||||||
"+ (UnityAppController*)GetAppController;",
|
"+ (UnityAppController*)GetAppController;",
|
||||||
"+ (void)addUnityEventListenerInternal:(id<UnityEventListener>)listener;",
|
|
||||||
"+ (void)removeUnityEventListenerInternal:(id<UnityEventListener>)listener;",
|
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -206,17 +231,7 @@ public static class XcodePostBuild
|
||||||
" return [UnityAppController GetAppController];",
|
" return [UnityAppController GetAppController];",
|
||||||
"}",
|
"}",
|
||||||
"",
|
"",
|
||||||
"// Added by " + TouchedMarker,
|
|
||||||
"static inline void addUnityEventListenerInternal(id<UnityEventListener> listener)",
|
|
||||||
"{",
|
|
||||||
" [UnityAppController addUnityEventListenerInternal: listener];",
|
|
||||||
"}",
|
|
||||||
"",
|
|
||||||
"// Added by " + TouchedMarker,
|
|
||||||
"static inline void removeUnityEventListenerInternal(id<UnityEventListener> listener)",
|
|
||||||
"{",
|
|
||||||
" [UnityAppController removeUnityEventListenerInternal:listener];",
|
|
||||||
"}"
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,25 +272,14 @@ public static class XcodePostBuild
|
||||||
"}",
|
"}",
|
||||||
"",
|
"",
|
||||||
"// Added by " + TouchedMarker,
|
"// Added by " + TouchedMarker,
|
||||||
"static NSHashTable* mUnityEventListeners = [NSHashTable weakObjectsHashTable];",
|
|
||||||
"+ (void)addUnityEventListener2:(id<UnityEventListener>)listener",
|
|
||||||
"{",
|
|
||||||
" [mUnityEventListeners addObject: listener];",
|
|
||||||
"}",
|
|
||||||
"",
|
|
||||||
"// Added by " + TouchedMarker,
|
|
||||||
"+(void)removeUnityEventListener2:(id<UnityEventListener>)listener",
|
|
||||||
"{",
|
|
||||||
" [mUnityEventListeners removeObject: listener];",
|
|
||||||
"}",
|
|
||||||
line,
|
|
||||||
"// Added by " + TouchedMarker,
|
|
||||||
"extern \"C\" void onUnityMessage(const char* message)",
|
"extern \"C\" void onUnityMessage(const char* message)",
|
||||||
"{",
|
"{",
|
||||||
" for (id<UnityEventListener> listener in mUnityEventListeners) {",
|
" if (GetAppController().unityMessageHandler) {",
|
||||||
" [listener onMessage:[NSString stringWithUTF8String:message]];",
|
" GetAppController().unityMessageHandler(message);",
|
||||||
" }",
|
" }",
|
||||||
"}",
|
"}",
|
||||||
|
line,
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,6 +334,33 @@ public static class XcodePostBuild
|
||||||
|
|
||||||
return new string[] { line };
|
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 };
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -6,11 +6,12 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#import <Flutter/Flutter.h>
|
#import <Flutter/Flutter.h>
|
||||||
|
#import <UnityUtils.h>
|
||||||
|
|
||||||
@interface FlutterUnityWidgetPlugin : NSObject<FlutterPlugin>
|
@interface FlutterUnityWidgetPlugin : NSObject<FlutterPlugin>
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface FUController : NSObject <FlutterPlatformView>
|
@interface FUController : NSObject <FlutterPlatformView, UnityEventListener>
|
||||||
|
|
||||||
- (instancetype)initWithFrame:(CGRect)frame
|
- (instancetype)initWithFrame:(CGRect)frame
|
||||||
viewIdentifier:(int64_t)viewId
|
viewIdentifier:(int64_t)viewId
|
||||||
|
@ -18,6 +19,7 @@
|
||||||
registrar:(NSObject<FlutterPluginRegistrar> *)registrar;
|
registrar:(NSObject<FlutterPluginRegistrar> *)registrar;
|
||||||
|
|
||||||
- (UIView*)view;
|
- (UIView*)view;
|
||||||
|
- (void)onMessage:(NSString *)message;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface FUViewFactory : NSObject <FlutterPlatformViewFactory>
|
@interface FUViewFactory : NSObject <FlutterPlatformViewFactory>
|
||||||
|
|
|
@ -63,11 +63,14 @@
|
||||||
[_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
|
[_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
|
||||||
[weakSelf onMethodCall:call result:result];
|
[weakSelf onMethodCall:call result:result];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)onMessage:(NSString *)message {
|
||||||
|
[_channel invokeMethod:@"onUnityMessage" arguments:message];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
|
- (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
|
||||||
if ([[call method] isEqualToString:@"postMessage"]) {
|
if ([[call method] isEqualToString:@"postMessage"]) {
|
||||||
[self postMessage:call result:result];
|
[self postMessage:call result:result];
|
||||||
|
@ -94,6 +97,10 @@
|
||||||
[UnityUtils createPlayer:^{
|
[UnityUtils createPlayer:^{
|
||||||
[_uView setUnityView: (UIView*)[GetAppController() unityView]];
|
[_uView setUnityView: (UIView*)[GetAppController() unityView]];
|
||||||
}];
|
}];
|
||||||
|
[GetAppController() setUnityMessageHandler: ^(const char* message)
|
||||||
|
{
|
||||||
|
[_channel invokeMethod:@"onUnityMessage" arguments:[NSString stringWithUTF8String:message]];
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
return _uView;
|
return _uView;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ extern "C" {
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@protocol UnityEventListener <NSObject>
|
@protocol UnityEventListener <NSObject>
|
||||||
- (void)onMessage:(NSString *)message;
|
- (void)onMessage:(NSString *)message;
|
||||||
@end
|
@end
|
||||||
|
@ -31,8 +32,6 @@ extern "C" {
|
||||||
|
|
||||||
+ (BOOL)isUnityReady;
|
+ (BOOL)isUnityReady;
|
||||||
+ (void)createPlayer:(void (^)(void))completed;
|
+ (void)createPlayer:(void (^)(void))completed;
|
||||||
+ (void)addUnityEventListener:(id<UnityEventListener>)listener;
|
|
||||||
+ (void)removeUnityEventListener:(id<UnityEventListener>)listener;
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
|
@ -153,26 +153,9 @@ static BOOL _isUnityReady = NO;
|
||||||
UnityAppController *controller = GetAppController();
|
UnityAppController *controller = GetAppController();
|
||||||
[controller application:application didFinishLaunchingWithOptions:nil];
|
[controller application:application didFinishLaunchingWithOptions:nil];
|
||||||
[controller applicationDidBecomeActive:application];
|
[controller applicationDidBecomeActive:application];
|
||||||
|
|
||||||
[UnityUtils listenAppState];
|
[UnityUtils listenAppState];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void onUnityMessage(const char* message)
|
|
||||||
{
|
|
||||||
for (id<UnityEventListener> listener in mUnityEventListeners) {
|
|
||||||
[listener onMessage:[NSString stringWithUTF8String:message]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
+ (void)addUnityEventListener:(id<UnityEventListener>)listener
|
|
||||||
{
|
|
||||||
[mUnityEventListeners addObject:listener];
|
|
||||||
}
|
|
||||||
|
|
||||||
+ (void)removeUnityEventListener:(id<UnityEventListener>)listener
|
|
||||||
{
|
|
||||||
[mUnityEventListeners removeObject:listener];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Reference in New Issue