Skip to content

Commit a1d1572

Browse files
authored
Merge pull request juicycleff#64 from krispypen/master
Fix + Cleanup onUnityMessage on iOS
2 parents b2a8760 + 7229556 commit a1d1572

File tree

6 files changed

+63
-52
lines changed

6 files changed

+63
-52
lines changed

example/unity/DemoApp/Assets/Editor/XCodePostBuild.cs

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,33 @@ private static void EditUnityAppControllerH(string path)
155155
inScope = false;
156156
markerDetected = false;
157157

158+
// Modify inline GetAppController
159+
EditCodeFile(path, line =>
160+
{
161+
inScope |= line.Contains("quitHandler)");
162+
163+
if (inScope && !markerDetected)
164+
{
165+
if (line.Trim() == "")
166+
{
167+
inScope = false;
168+
markerDetected = true;
169+
170+
return new string[]
171+
{
172+
"@property (nonatomic, copy) void(^unityMessageHandler)(const char* message);",
173+
};
174+
}
175+
176+
return new string[] { line };
177+
}
178+
179+
return new string[] { line };
180+
});
181+
182+
inScope = false;
183+
markerDetected = false;
184+
158185
// Add static GetAppController
159186
EditCodeFile(path, line =>
160187
{
@@ -171,8 +198,6 @@ private static void EditUnityAppControllerH(string path)
171198
"",
172199
"// Added by " + TouchedMarker,
173200
"+ (UnityAppController*)GetAppController;",
174-
"+ (void)addUnityEventListenerInternal:(id<UnityEventListener>)listener;",
175-
"+ (void)removeUnityEventListenerInternal:(id<UnityEventListener>)listener;",
176201
""
177202
};
178203
}
@@ -206,17 +231,7 @@ private static void EditUnityAppControllerH(string path)
206231
" return [UnityAppController GetAppController];",
207232
"}",
208233
"",
209-
"// Added by " + TouchedMarker,
210-
"static inline void addUnityEventListenerInternal(id<UnityEventListener> listener)",
211-
"{",
212-
" [UnityAppController addUnityEventListenerInternal: listener];",
213-
"}",
214-
"",
215-
"// Added by " + TouchedMarker,
216-
"static inline void removeUnityEventListenerInternal(id<UnityEventListener> listener)",
217-
"{",
218-
" [UnityAppController removeUnityEventListenerInternal:listener];",
219-
"}"
234+
220235
};
221236
}
222237

@@ -257,25 +272,14 @@ private static void EditUnityAppControllerMM(string path)
257272
"}",
258273
"",
259274
"// Added by " + TouchedMarker,
260-
"static NSHashTable* mUnityEventListeners = [NSHashTable weakObjectsHashTable];",
261-
"+ (void)addUnityEventListener2:(id<UnityEventListener>)listener",
262-
"{",
263-
" [mUnityEventListeners addObject: listener];",
264-
"}",
265-
"",
266-
"// Added by " + TouchedMarker,
267-
"+(void)removeUnityEventListener2:(id<UnityEventListener>)listener",
268-
"{",
269-
" [mUnityEventListeners removeObject: listener];",
270-
"}",
271-
line,
272-
"// Added by " + TouchedMarker,
273275
"extern \"C\" void onUnityMessage(const char* message)",
274276
"{",
275-
" for (id<UnityEventListener> listener in mUnityEventListeners) {",
276-
" [listener onMessage:[NSString stringWithUTF8String:message]];",
277+
" if (GetAppController().unityMessageHandler) {",
278+
" GetAppController().unityMessageHandler(message);",
277279
" }",
278280
"}",
281+
line,
282+
279283
};
280284
}
281285

@@ -330,6 +334,33 @@ private static void EditUnityAppControllerMM(string path)
330334

331335
return new string[] { line };
332336
});
337+
338+
inScope = false;
339+
markerDetected = false;
340+
341+
// Modify inline GetAppController
342+
EditCodeFile(path, line =>
343+
{
344+
inScope |= line.Contains("@synthesize quitHandler");
345+
346+
if (inScope && !markerDetected)
347+
{
348+
if (line.Trim() == "")
349+
{
350+
inScope = false;
351+
markerDetected = true;
352+
353+
return new string[]
354+
{
355+
"@synthesize unityMessageHandler = _unityMessageHandler;",
356+
};
357+
}
358+
359+
return new string[] { line };
360+
}
361+
362+
return new string[] { line };
363+
});
333364
}
334365

335366
/// <summary>
Binary file not shown.

ios/Classes/FlutterUnityWidgetPlugin.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ - (instancetype)initWithFrame:(CGRect)frame
6363
[_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
6464
[weakSelf onMethodCall:call result:result];
6565
}];
66-
6766
}
6867
return self;
6968
}
@@ -94,6 +93,10 @@ - (UIView*)view {
9493
[UnityUtils createPlayer:^{
9594
[_uView setUnityView: (UIView*)[GetAppController() unityView]];
9695
}];
96+
[GetAppController() setUnityMessageHandler: ^(const char* message)
97+
{
98+
[_channel invokeMethod:@"onUnityMessage" arguments:[NSString stringWithUTF8String:message]];
99+
}];
97100
}
98101
return _uView;
99102
}

ios/Classes/UnityUtils.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@ extern "C" {
2323
} // extern "C"
2424
#endif
2525

26-
@protocol UnityEventListener <NSObject>
27-
- (void)onMessage:(NSString *)message;
28-
@end
29-
3026
@interface UnityUtils : NSObject
3127

3228
+ (BOOL)isUnityReady;
3329
+ (void)createPlayer:(void (^)(void))completed;
34-
+ (void)addUnityEventListener:(id<UnityEventListener>)listener;
35-
+ (void)removeUnityEventListener:(id<UnityEventListener>)listener;
3630

3731
@end
3832

ios/Classes/UnityUtils.mm

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,4 @@ + (void)createPlayer:(void (^)(void))completed
158158
});
159159
}
160160

161-
extern "C" void onUnityMessage(const char* message)
162-
{
163-
for (id<UnityEventListener> listener in mUnityEventListeners) {
164-
[listener onMessage:[NSString stringWithUTF8String:message]];
165-
}
166-
}
167-
168-
+ (void)addUnityEventListener:(id<UnityEventListener>)listener
169-
{
170-
[mUnityEventListeners addObject:listener];
171-
}
172-
173-
+ (void)removeUnityEventListener:(id<UnityEventListener>)listener
174-
{
175-
[mUnityEventListeners removeObject:listener];
176-
}
177-
178161
@end

0 commit comments

Comments
 (0)