Hey everybody,
I have seen quite a few people on this forum trying to figure out the best way to switch the atlas at runtime. I have created a script that handles that, and thought I would share it here. There is some setup required to get this to work properly, but really it is pretty painless.
you will need:
-Reference Atlas - build your entire UI off this atlas, and then have this atlas point to one of these below
-Reference Font
-SD atals - (iphone 3gs and below)
-SD font
-HD atlas - (ipad 1&2, iphone 4+)
-HD font
-SHD atlas - (ipad 3) --- SHD = Super High Def (didn't know what else to call this. Also this is not needed if you don't want to support it)
-SHD font
You will need to store all three "real" atlases and corresponding fonts (not reference) in your "resources" folder under the following folder structure "GUI/SD", "GUI/HD", "GUI/SHD".
For clarity I put all things related to the atlas in each of these folders (prefab, image, material).
Attach this script to an empty gameObject in your scene.
**********
*IMPORTANT*
**********
In Unity go to "Edit -> Project Settings -> Script Execution Order" Now add "ResolutionSwitchController" to the list and move it up towards the top. You want this script to be one of the very first scripts to run. ESPECIALLY before any of the NGUI scripts (I have mine set to the second in the list just below "localization").
Notes:
-script assumes you are in landscape (all screen resolution checks are based on width -- this could/should probably be changed)
-the two labels are for debugging purposes, to ensure that you are running the resolution that you think you are on your device
-script can be set to not run in the editor, as this can screw up all of your references.
Please feel free to use, update, and modify this script to fit your needs. Hope this helps somebody!
using UnityEngine;
using System.Collections;
public class ResolutionSwitchController : MonoBehaviour {
public bool disableInEditor = true;
public int HDResolutionCutOff = 960;
public int SHDResolutionCutOff = 2048;
public UIAtlas ReferenceAtlas;
public string SDAtlasName;
public string HDAtlasName;
public string SHDAtlasName;
public UIFont ReferenceFont;
public string SDFontName;
public string HDFontName;
public string SHDFontName;
public GameObject ReplacementAtlas;
public GameObject ReplacementFont;
public UILabel screenWidthLabel;
public UILabel screenHeightLabel;
// Use this for initialization
void Awake () {
if(disableInEditor && Application.isEditor)
{
return;
}
int ScreenHeight = Screen.height;
int ScreenWidth = Screen.width;
if(screenHeightLabel)
{
screenHeightLabel.text = ScreenHeight.ToString();
}
if(screenWidthLabel)
{
screenWidthLabel.text = ScreenWidth.ToString();
}
if( ScreenWidth >= SHDResolutionCutOff )
{
ReplacementAtlas = Resources.Load("GUI/SHD/" + SHDAtlasName ) as GameObject;
ReplacementFont = Resources.Load("GUI/SHD/" + SHDFontName ) as GameObject;
Debug.Log("Setting texture to SHD");
}else if( ScreenWidth >= HDResolutionCutOff )
{
ReplacementAtlas = Resources.Load("GUI/HD/" + HDAtlasName ) as GameObject;
ReplacementFont = Resources.Load("GUI/HD/" + HDFontName ) as GameObject;
Debug.Log("Setting texture to HD");
}else {
ReplacementAtlas = Resources.Load("GUI/SD/" + SDAtlasName ) as GameObject;
ReplacementFont = Resources.Load("GUI/SD/" + SDFontName ) as GameObject;
Debug.Log("Setting texture to SD");
}
ReferenceAtlas.replacement = ReplacementAtlas.GetComponent<UIAtlas>();
ReferenceFont.replacement = ReplacementFont.GetComponent<UIFont>();
}
}