unity3d 屏幕适配 iPhone X 解决方案

2018-1-18 雨辰 遇到的一些问题

1:获取手机型号后针对适配

#import <sys/utsname.h>
+ (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;
}


标签: Unity3D-优化性能 Unity3D 技术问题

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号