Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ISSUE-024

Unity KeyboardDelegate重复初始化

常见表现

启动闪退。

通过终端运行游戏,日志显示“[KeyboardDelegate Initialize] called after creating keyboard”。

相关源码

Unity引擎 Keyboard.mm

@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

原因分析

游戏重复初始化KeyboardDelegate,触发断言失败。

解决方法

这个断言失败并不影响游戏运行,直接屏蔽+[KeyboardDelegate Initialize]即可。

BUNDLE_ID=com.companyname.appname
EXECUTABLE=~/Library/Containers/io.playcover.PlayCover/Applications/$BUNDLE_ID.app/Frameworks/UnityFramework.framework/UnityFramework
FUNC_ADDR=$(otool -oV $EXECUTABLE | awk '/KeyboardDelegate/{found=1} found && /Initialize/{f=1} f && /imp/{print $2; exit}')
printf '\xC0\x03\x5F\xD6' | dd of=$EXECUTABLE bs=1 seek=$(($FUNC_ADDR)) conv=notrunc
codesign -fs- $EXECUTABLE