unity3d 屏幕适配 iPhone X 解决方案

添加获取手机型号方法

#import 
+ (NSString*)getDeviceVersion
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceVersion = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    return deviceVersion;
}

在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;
}

说明一下 以上是unity4.7 的修改方法 不同版本的unity 修改的地方可能不一样

你可能感兴趣的:(unity3d,屏幕适配,unity3d,适配)