Skip to content

fix: ignore Unity keyboard initialization error#224

Merged
Depal1 merged 1 commit into
PlayCover:masterfrom
viatearz:fix/unity-keyboard-error
Jun 6, 2026
Merged

fix: ignore Unity keyboard initialization error#224
Depal1 merged 1 commit into
PlayCover:masterfrom
viatearz:fix/unity-keyboard-error

Conversation

@viatearz

Copy link
Copy Markdown
Contributor

Summary

Some Unity games crash on startup with the following assertion failure:

[KeyboardDelegate Initialize] called after creating keyboard

This PR adds an option to suppress the assertion and allow affected games to continue running.

Related Code

@implementation KeyboardDelegate
+ (void)Initialize
{
    NSAssert(_keyboard == nil, @"[KeyboardDelegate Initialize] called after creating keyboard");
    if (!_keyboard)
        _keyboard = [[KeyboardDelegate alloc] init];
}

+ (KeyboardDelegate*)Instance
{
    if (!_keyboard)
        _keyboard = [[KeyboardDelegate alloc] init];
    return _keyboard;
}
@end
@implementation UnityView
- (NSArray*)keyCommands
{
    if ([[KeyboardDelegate Instance] status] == Visible)
        return nil;
	// ...
}
@end
@implementation _UIMainMenuSystem
- (void)_setupMainSystemObservations {
	if ([[NSProcessInfo processInfo] isiOSAppOnMac]) {
	    [[NSNotificationCenter defaultCenter] addObserverForName:@"_UIWindowDidBecomeApplicationKeyNotification"
	                                                      object:nil
	                                                       queue:nil
	                                                  usingBlock:^(NSNotification *notification) {
	        // Collect key commands ...
	    }];
	}	
}
@end

Explanation

By default, Unity calls initUnityWithApplication: during application:didFinishLaunchingWithOptions:.

application:didFinishLaunchingWithOptions: 
└─ [UnityAppController initUnityWithApplication:]  
   ├─ [UnityAppController createUnityView]
   ├─ [UIWindow makeKeyAndVisible]
   └─ [KeyboardDelegate Initialize] 

However, some games defer initUnityWithApplication: until applicationDidBecomeActive:.
In this case, [KeyboardDelegate Instance] is called before [KeyboardDelegate Initialize], which triggers the assertion failure.

applicationDidBecomeActive: 
└─ [UnityAppController initUnityWithApplication:]
   ├─ [UnityAppController createUnityView]
   ├─ [UIWindow makeKeyAndVisible]
   │  └─ Post _UIWindowDidBecomeApplicationKeyNotification
   │     └─ _UIMainMenuSystem
   │        └─ [UnityView keyCommands]
   │           └─ [KeyboardDelegate Instance]
   └─ [KeyboardDelegate Initialize]  ← Assertion failure 

Apparently it is an issue with the game itself, and there is nothing PlayCover can do.

Fortunately, this assertion failure does not affect gameplay, so the simplest solution is to ignore the assertion.

Testing

For a quick test, check this game (less than 100MB).

@Depal1 Depal1 merged commit d688f69 into PlayCover:master Jun 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants