Skip to content

Instantly share code, notes, and snippets.

@AlexTiTanium
Created January 11, 2013 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexTiTanium/4509294 to your computer and use it in GitHub Desktop.
Save AlexTiTanium/4509294 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using System.Collections;
public class Device {
//**************************************************************************
//
// Const
//
//**************************************************************************
const float ASPECT_IPHONE = 3f / 4f;
const float ASPECT_IPHONE5 = 71f / 40f;
const float ASPECT_IPAD = 4f / 3f;
//**************************************************************************
//
// Constructor static!
//
//**************************************************************************
static Device()
{
if (Const.FORCE_DEVICE_TYPE != null)
{
current = Const.FORCE_DEVICE_TYPE;
return;
}
float aspect = GetDeviceAspect();
if (Mathf.Approximately(aspect, ASPECT_IPHONE5))
{
current = DevicesType.iPhone5;
}
else if (Mathf.Approximately(aspect, ASPECT_IPAD))
{
current = DevicesType.iPad;
}
else
{
current = DevicesType.iPhone;
}
}
//**************************************************************************
//
// Public
//
//**************************************************************************
// Devices types, divided by aspect
public enum DevicesType { iPhone, iPhone5, iPad }
//**************************************************************************
// If for device need passive
public static bool IsNeedPassiveBackground()
{
return Current == DevicesType.iPad || Current == DevicesType.iPhone5;
}
//**************************************************************************
// If for device need passive background on top
public static bool IsPassiveBackgroundOnTop()
{
return Current == DevicesType.iPad;
}
//**************************************************************************
// If for device need passive background on left and right side
public static bool IsPassiveBackgroundOnSides()
{
return Current == DevicesType.iPhone5;
}
//**************************************************************************
// Active background width size in world coords
public static float GetActiveBackgroundWidth()
{
return GetActiveBackgroundAspect() * ActiveBackgroundHeight();
}
//**************************************************************************
// Active background heigt size in world coords
public static float GetActiveBackgroundHeigt()
{
return GetActiveBackgroundWidth() / Const.BACKGROUND_ACTIVE_ASPECT;
}
//**************************************************************************
//
// Private
//
//**************************************************************************
// Static constructor detect current device type by aspect and set this property
private static readonly DevicesType current;
//**************************************************************************
// Return current device type, detected by aspect
private static DevicesType Current
{
get { return current; }
}
//**************************************************************************
// Return current device aspect
private static float GetDeviceAspect()
{
return (float)Screen.width / Screen.height;
}
//**************************************************************************
// Return current active background aspect that device depend
private static float GetActiveBackgroundAspect()
{
if (Current == DevicesType.iPhone)
{
return Const.BACKGROUND_ACTIVE_ASPECT_SMARTPHONE;
}
return Const.BACKGROUND_ACTIVE_ASPECT;
}
//**************************************************************************
// Return current active background aspect that device depend
private static float ActiveBackgroundHeight()
{
if (Current == DevicesType.iPhone)
{
return Const.BACKGROUND_ACTIVE_SMARTPHONE_HEIGHT;
}
return Const.BACKGROUND_ACTIVE_HEIGHT;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment