Is it possible to get Unity iPhone 1.7 to work on the iPhone 4 at full resolution?

I've made some progress on getting my unity game to try to get the game to play back at full-res on the iPhone 4.

Here's what I've done so far, in, change this function to this version.


int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight)
    CGRect rect = [[UIScreen mainScreen] bounds];

    // Create a full-screen window
    _window = [[UIWindow alloc] initWithFrame:rect];
    EAGLView* view = [[EAGLView alloc] initWithFrame:rect];
    [_window addSubview:view];

    // Change the content size on the iPhone 4
        int w = 320, h = 480;
        float ver = [[[UIDevice currentDevice] systemVersion] floatValue];
        // Can't detect screen res in pre 3.2 devices, but they are all 320x480 anyway.
        if (ver >= 3.2) 
            UIScreen* mainscr = [UIScreen mainScreen];
            w = mainscr.currentMode.size.width;
            h = mainscr.currentMode.size.height;
        if( w == 640 )  // this is a naive check
            view.contentScaleFactor = 2.0f;

    CAEAGLLayer* eaglLayer = (CAEAGLLayer*)[view layer];
    _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

    if (!_context)
        return false;

    if (![EAGLContext setCurrentContext:_context]) {
        _context = 0;
        return false;

    GLint width;
    GLint height;
    if (!CreateWindowSurface(eaglLayer, GL_RGB565_OES, GL_DEPTH_COMPONENT16_OES, NO, &_surface, width, height )) {
        return false;

    glViewport(0, 0, width, height);
    [_window makeKeyAndVisible];
    [view release];

    *window = _window;
    *screenWidth = _surface.size.width;
    *screenHeight = _surface.size.height;   

    return true;


bool CreateWindowSurface(CAEAGLLayer* eaglLayer, GLuint format, GLuint depthFormat, bool retained, MyEAGLSurface* surface, GLint& width, GLint& height )
    CGSize                  newSize;
    GLuint                  oldRenderbuffer;
    GLuint                  oldFramebuffer;

    surface->format = format;
    surface->depthFormat = depthFormat;

    surface->depthBuffer = 0;
    surface->renderbuffer = 0;
    surface->framebuffer = 0;

    eaglLayer.opaque = YES;
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat, nil];

    newSize = [eaglLayer bounds].size;
    newSize.width = roundf(newSize.width);
    newSize.height = roundf(newSize.height);

    glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, (GLint *) &oldRenderbuffer);
    glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *) &oldFramebuffer);

    glGenRenderbuffersOES(1, &surface->renderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, surface->renderbuffer);

    if(![_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]) {
        glDeleteRenderbuffersOES(1, &surface->renderbuffer);
        glBindRenderbufferOES(GL_RENDERBUFFER_BINDING_OES, oldRenderbuffer);
        return false;

    // Get the renderbuffer size.
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &width);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &height);
    newSize.width = width;
    newSize.height = height;

    NSLog( @"

width = %d, height = %d

", width, height );

    glGenFramebuffersOES(1, &surface->framebuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, surface->framebuffer);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, surface->renderbuffer);
    if (depthFormat) {
        glGenRenderbuffersOES(1, &surface->depthBuffer);
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, surface->depthBuffer);
        glRenderbufferStorageOES(GL_RENDERBUFFER_OES, surface->depthFormat, newSize.width, newSize.height);
        glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, surface->depthBuffer);

    surface->size = newSize;
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, oldRenderbuffer);


    return true;

Now this works great for rendering, at least in my preliminary tests. The problem I'm running into is that my Input seems to be broken. Inside Unity, it reports the Screen.width and Screen.height to be 960x640 (correctly). But, say, if I test Input.mousePosition in the upper right corner of the screen it reports (480, 320). At least in one direction. If I rotate the phone to landscape right, it reports the upper right to be (960, 640), but the lower left to be (480, 320). This causes issues when I try to do input with `ScreenPointToRay`. I'm sure I could work around it, but it would be a complete hack with scaling variables depending on the resolution and orientation of the device. I'll admit I haven't tried using the iPhoneInput class and checking touches yet.

Has anybody gotten input to work on the iPhone 4 at full res?

Edit: copy/pasted code I'm actually using.

Well I managed to hack around the input bugs. With the "render at full res" code in the question, I use this class as a wrapper to Input.mousePosition:

public static class Inputx
    static Vector2 lastKnownCursorPosition =;
    static bool isIphone4 = false;

    public static Vector2 cursorPosition
            lastKnownCursorPosition = Input.mousePosition;

            if( !isIphone4 && ( Screen.width == 960 && Screen.height == 640 ) )  // iPhone 4 gate
                isIphone4 = true;

            if( isIphone4 )
                if( iPhoneSettings.screenOrientation == iPhoneScreenOrientation.LandscapeLeft || iPhoneSettings.screenOrientation == iPhoneScreenOrientation.Portrait )
                    lastKnownCursorPosition *= 2.0f;
                else if( iPhoneSettings.screenOrientation == iPhoneScreenOrientation.LandscapeRight || iPhoneSettings.screenOrientation == iPhoneScreenOrientation.PortraitUpsideDown )
                    lastKnownCursorPosition -= new Vector2( 480, 320 );
                    lastKnownCursorPosition *= 2.0f;

            return lastKnownCursorPosition;
            return Input.mousePosition;

I'm sure similar logic could be used to adjust touch positions.

To fix touch input you can use another "hack" as well, which will also work with Unity GUI objects and it does not require any more code in Unity. Just update the methods in Cocoa that forward the touch events to Unity like this (obviously you should change that hardcoded 2 I have there to 1 if the device is not HD and also make sure you change and forward all touches, not just the 1st one as I do in this example):

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    //scale the touch objects
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    NSLog(@"touch = %@", touch);
    [touch setValue:[NSValue valueWithCGPoint:CGPointMake([touch locationInView:self].x * 2, [touch locationInView:self].y * 2)] forKey:@"locationInWindow"];
    UnitySendTouchesBegin([NSSet setWithObject:touch], event);

Should I also mention that your app is likely to crash on new OS versions? :)

Where do you put the "class as a wrapper to Input.mousePosition" ? xCode or Unity?