- Home /
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 AppController.mm, change this function to this version.
OpenEAGL_UnityCallback:
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;
}
CreateWindowSurface
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( @"\n\nwidth = %d, height = %d\n\n\n", 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);
CHECK_GL_ERROR();
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.
It's strange. I got this to work with the FPS Unity demo project but when I try it with my own project I keep getting an error in xcode t line 508 on view.contentScaleFactor = 2.0f; saying error: request for member 'contentScaleFactor' in 'view', which is of non-class type 'EAGLView*' Any ideas on what I'm doing wrong?
You'll probably need to set your Base SD$$anonymous$$ to 4.0.
Answer by Tetrad · Jul 08, 2010 at 08:16 PM
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 { #if UNITY_IPHONE static Vector2 lastKnownCursorPosition = Vector2.zero; static bool isIphone4 = false; #endif
public static Vector2 cursorPosition
{
get
{
if UNITY_IPHONE
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;
else
return Input.mousePosition;
endif
}
}
}
I'm sure similar logic could be used to adjust touch positions.
I'm going to have a go at using something similar as this right now. I can confirm that this touch input position is a problem with Unity 3 which natively supports the iPhone 4 and "just works" out of the box. Sadly it seems the touches are similarly mapped as you experienced.
Answer by Dimitris 1 · Sep 07, 2010 at 05:17 PM
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? :)
Answer by Tore · Sep 02, 2010 at 03:03 PM
Where do you put the "class as a wrapper to Input.mousePosition" ? xCode or Unity?