ResolutionSwitchController.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is responsible for detecting which definition of atlases and textures to use
  5. /// It is partly an NGUI Atlas Resolution switch controller adapted from http://www.tasharen.com/forum/index.php?topic=832.0
  6. /// and also a Unity quality setter (http://answers.unity3d.com/questions/28515/dynamically-choosing-a-high-resolution-texture-at.html)
  7. /// Note that atlas textures need to be set as "GUI" as Texture Type. Otherwise the quality settings would scale them down.
  8. /// </summary>
  9. public class ResolutionSwitchController : MonoBehaviour
  10. {
  11. public UIAtlas[] referenceAtlases;
  12. public UIFont[] referenceFonts;
  13. public bool disableInEditor = true;
  14. public int HDResolutionCutOff = 800;
  15. public int SHDResolutionCutOff = 1500;
  16. private static bool _isHD = false;
  17. private static bool _isSD = false;
  18. private static bool _isSHD = false;
  19. public static bool IsHD { get { return _isHD; } }
  20. public static bool IsSD { get { return _isSD; } }
  21. public static bool IsSHD { get { return _isSHD; } }
  22. void Awake()
  23. {
  24. NotificationCenter.AddListener(OnApplyAtlasBundle, NotificationType.ApplyAtlasBundle);
  25. #if UNITY_WEBPLAYER
  26. _isHD = true;
  27. #else
  28. if (disableInEditor && Application.isEditor)
  29. {
  30. return;
  31. }
  32. int screenWidth = Screen.width;
  33. AVDebug.Log("Screen resolution is "+Screen.width+"x"+Screen.height);
  34. //Debug.LogError("Screen resolution is " + Screen.width + "x" + Screen.height);
  35. //Set Quality Settings
  36. if (screenWidth >= SHDResolutionCutOff)
  37. {
  38. #if UNITY_ANDROID
  39. QualitySettings.SetQualityLevel(1);
  40. AVDebug.Log("Setting texture to SHD");
  41. _isSHD = true;
  42. #else
  43. QualitySettings.SetQualityLevel(2);
  44. AVDebug.Log("Setting texture to SHD");
  45. _isSHD = true;
  46. #endif
  47. }
  48. else
  49. if (screenWidth >= HDResolutionCutOff)
  50. {
  51. QualitySettings.SetQualityLevel(1);
  52. AVDebug.Log("Setting texture to HD");
  53. _isHD = true;
  54. }
  55. else
  56. {
  57. QualitySettings.SetQualityLevel(0);
  58. AVDebug.Log("Setting texture to SD");
  59. _isSD = true;
  60. }
  61. #endif
  62. }
  63. void OnDestroy()
  64. {
  65. NotificationCenter.RemoveListener(OnApplyAtlasBundle, NotificationType.ApplyAtlasBundle);
  66. }
  67. void OnApplyAtlasBundle(Notification note)
  68. {
  69. AtlasBundle atlasBundle = (AtlasBundle)note.data;
  70. referenceAtlases[0].replacement = atlasBundle.atlasPrefab;
  71. //Note: the following order is extremely important. It is dependent on how they are dragged in the component in Unity Editor in referenceFonts variable
  72. referenceFonts[0].replacement = atlasBundle.buttonFontPrefab;
  73. referenceFonts[1].replacement = atlasBundle.textFontPrefab;
  74. referenceFonts[2].replacement = atlasBundle.scoreFontPrefab;
  75. }
  76. }