1. 程式人生 > >unity3d iPhone X 適屏程式碼 螢幕適應方案

unity3d iPhone X 適屏程式碼 螢幕適應方案

文章原地址:http://blog.csdn.net/qqo_aa/article/details/78487775?readlog

新增獲取手機型號方法

#import <sys/utsname.h>
+ (NSString*)getDeviceVersion
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceVersion = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    return
deviceVersion; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在unity匯出的xcode工程裡面 查詢路徑 Unity-iPhone/Classes/Unity/DisplayManager.mm修改方法如下

- (void)updateScreenSize
{
    CGSize layerSize    = _view.layer.bounds.size;
    NSString *deviceVersion = [這裡呼叫上面的獲取型號方法];
    if ([deviceVersion isEqualToString:@"iPhone10,3"] || [deviceVersion isEqualToString:@"iPhone10,6"
]) { layerSize = CGSizeMake(744, 375); } CGFloat scale = UnityScreenScaleFactor(_screen); _screenSize = CGSizeMake(layerSize.width * scale, layerSize.height * scale); } - (id)createView:(BOOL)useForRendering showRightAway:(BOOL)showRightAway; { if(_view == nil) { NSString
*deviceVersion = [這裡呼叫上面的獲取型號方法]; if ([deviceVersion isEqualToString:@"iPhone10,3"] || [deviceVersion isEqualToString:@"iPhone10,6"]) { CGRect bounds = CGRectMake(34, 0, 744, 375); _window = [[UIWindow alloc] initWithFrame:bounds]; } else { _window = [[UIWindow alloc] initWithFrame: _screen.bounds]; } _window.screen = _screen; if(_screen == [UIScreen mainScreen]) { _view = [GetAppController() initUnityView]; NSAssert([_view isKindOfClass:[UnityView class]], @"You MUST use UnityView subclass as unity view"); } else { _view = [(useForRendering ? [UnityRenderingView alloc] : [UIView alloc]) initWithFrame: _screen.bounds]; } _view.contentScaleFactor = UnityScreenScaleFactor(self.screen); [self updateScreenSize]; if(showRightAway) { [_window addSubview:_view]; [_window makeKeyAndVisible]; } } return self; }