- Home /
Compile Time difference between iPad and iPhone
From: http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html
I can see there's a tag "UNITY_IPHONE", which let you do in your code
#if UNITY_PHONE
#endif
And it will automatically include or exclude code paths that are relevant to the current platform.
However, I can't find a way to do that difference between an iPad and an iPhone. Both have different screen resolution, which means I have to layout my UI differently. The iOS store specifically support different version for iPad vs iPhone, but Unity would not support such differentiation?
Answer by Joyrider · Aug 11, 2013 at 08:36 PM
the UNITY_IPHONE, actually means iOS, not a specific device.
It is for platform (not device) dependant compiling. And both iPad and iPhone actually use the same platform (iOS), and thus have the same inner workings.
As for your UI, use screen resolutions to set it up.
I know the Screen
class. However, doing a layout on iPad vs on iPhone is not the same. The screen ratio isn't even the same. So am I forced to do something like if (ratio == 4/3) { } else if (ratio == 16/9) { }
?
you could yes. We on the other hand, usually think our GUI with anchors and sizefactors, like:
float sizeFactor = Screen.width/1024; // if your original UI positions/sizes are for the iPad2 for example
GUI.Button(new Rect(Screen.width-(10+btnSize.x)*sizeFactor, Screen.height - (10+btnSize.y)*sizeFactor,btnSize.x*sizeFactor,btnSize.y*sizeFactor),"$$anonymous$$yButton");
So a button would always be at (10,10) from the bottom right of the screen for example if it has 1024 pixels horizontally.
So ins$$anonymous$$d of giving all your buttons a fixed position, they all have a fixed position compared to an anchor point; and thus adapt to the screen.
I built a library with many methods doing just that, which use position/size from whatever screen borders I want independently of the screen size. I never place anything on screen using "pixels". The problem is not the size itself, but the screen ratio. Something that looks good on a 4/3 may look horrible in a 16/9 giving space between items or overlapping them. Adding empty space on each side of my layout to pad the difference between resolution is not a good solution.
Example of what I use to position UI items;
/// <summary>
/// Return a rectangle which position is relative to the screen size.
/// </summary>
/// <param name="x">% horizontal</param>
/// <param name="y">% vertical</param>
/// <param name="width">% width</param>
/// <param name="height">% height</param>
/// <param name="position">The position is relative to which screen sides</param>
/// <param name="scale">The scale is relative to which screen sides</param>
public static Rect Relative(float x, float y, float width, float height, Bounds position, Bounds scale)
{
Rect result = new Rect(x, y, width, height);
if (((scale & Bounds.Left) == Bounds.Left) || ((scale & Bounds.Right) == Bounds.Right))
result.width = Screen.width * width;
if (((scale & Bounds.Left) == Bounds.Left) || ((scale & Bounds.Right) == Bounds.Right))
result.height = Screen.height * height;
if ((position & (Bounds.Right | Bounds.Left)) == (Bounds.Right | Bounds.Left))
result.x = (Screen.width * x - (result.width / 2));
else if ((position & Bounds.Left) == Bounds.Left)
result.x = Screen.width * x;
else if ((position & Bounds.Right) == Bounds.Right)
result.x = Screen.width * x - result.width;
if ((position & (Bounds.Top | Bounds.Bottom)) == (Bounds.Top | Bounds.Bottom))
result.y = (Screen.height * y - (result.height / 2));
else if ((position & Bounds.Top) == Bounds.Top)
result.y = Screen.height * y;
else if ((position & Bounds.Bottom) == Bounds.Bottom)
result.y = Screen.height * y - result.height;
return result;
}
Well then I guess your if(ratio){} does the trick. ;) This is anyway possible for iOS. I'm more used to Android, and since there are so many different aspect ratios, we just build our GUI to look good whatever the AR is. Because handling all possible ARs can be quite time consu$$anonymous$$g otherwise.
Your answer
Follow this Question
Related Questions
Unity modules for iOS? 1 Answer
iPhone Controller Asset 0 Answers
iOS: Handheld.PlayFullScreenMovie 2 Answers
How to drag a scrollView with TouchPhase.Moved? 0 Answers
How to identify each iOS device? 1 Answer