GameSettings.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using UnityEngine;
  2. using System.Collections;
  3. public class GameSettings : Singleton<GameSettings>
  4. {
  5. public const int GraphicLevelLow = 0;
  6. public const int GraphicLevelMiddle = 1;
  7. public const int GraphicLevelHigh = 2;
  8. public const int ScreenType4_3 = 1;
  9. public const int ScreenType16_9 = 2;
  10. public const int ScreenTypeOther = 3;
  11. bool mbSettingChanged = false;
  12. bool mbLoaded = false;
  13. /// <summary>
  14. /// Graphic Quality Level, 2 is the best graphic quality.
  15. /// </summary>
  16. int mGraphicLevel = -1; //0 -- Low, 1 good, 2 the best.
  17. bool mbMusicMute = false;
  18. bool mbSoundMute = false;
  19. int mMusicVolume = 60; //60%, ~= 0.6f
  20. int mSoundVolume = 60;
  21. public bool MusicMute
  22. {
  23. get { return mbMusicMute; }
  24. set
  25. {
  26. if (mbMusicMute != value)
  27. {
  28. mbMusicMute = value;
  29. mbSettingChanged = true;
  30. }
  31. }
  32. }
  33. public bool SoundMute
  34. {
  35. get { return mbSoundMute; }
  36. set
  37. {
  38. if (mbSoundMute != value)
  39. {
  40. mbSoundMute = value;
  41. mbSettingChanged = true;
  42. }
  43. }
  44. }
  45. public float MusicVolume
  46. {
  47. get { return mMusicVolume / 100f; }
  48. set
  49. {
  50. if (mMusicVolume != (int)(value * 100))
  51. {
  52. mMusicVolume = (int)(value * 100);
  53. mbSettingChanged = true;
  54. }
  55. }
  56. }
  57. public float SoundVolume
  58. {
  59. get { return mSoundVolume / 100f; }
  60. set
  61. {
  62. if (mSoundVolume != (int)(value * 100))
  63. {
  64. mSoundVolume = (int)(value * 100);
  65. mbSettingChanged = true;
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Graphic Quality Level, 0 is the Low graphic quality, 2 is the best graphic quality.
  71. /// </summary>
  72. public int GraphicLevel
  73. {
  74. get { return mGraphicLevel; }
  75. set
  76. {
  77. if (mGraphicLevel != value && IsGraphicLevelValid(value))
  78. {
  79. mGraphicLevel = value;
  80. mbSettingChanged = true;
  81. if (mGraphicLevel == GraphicLevelLow)
  82. {
  83. QualitySettings.SetQualityLevel(2, true);
  84. }
  85. else if (mGraphicLevel == GraphicLevelMiddle)
  86. {
  87. QualitySettings.SetQualityLevel(3, true);
  88. }
  89. else if (mGraphicLevel == GraphicLevelHigh)
  90. {
  91. QualitySettings.SetQualityLevel(4, true);
  92. }
  93. //DebugHelper.LogError("mGraphicLevel:"+ mGraphicLevel);
  94. }
  95. }
  96. }
  97. public override void Init()
  98. {
  99. base.Init();
  100. Load();
  101. }
  102. public void Load()
  103. {
  104. if (!mbLoaded)
  105. {
  106. int mute = PlayerPrefs.GetInt("musicMute", 0);
  107. mbMusicMute = mute == 1;
  108. mute = PlayerPrefs.GetInt("soundMute", 0);
  109. mbSoundMute = mute == 1;
  110. mMusicVolume = PlayerPrefs.GetInt("musicVolume", 60);
  111. mSoundVolume = PlayerPrefs.GetInt("soundVolume", 60);
  112. if (!PlayerPrefs.HasKey("graphicLevel"))
  113. {
  114. GraphicLevel = ((int)DeviceInfo.m_DeviceState - 1);
  115. }
  116. else
  117. {
  118. mute = PlayerPrefs.GetInt("graphicLevel", 0);
  119. GraphicLevel = mute;
  120. }
  121. mbLoaded = true;
  122. mbSettingChanged = false;
  123. }
  124. }
  125. public void Save()
  126. {
  127. if (mbSettingChanged)
  128. {
  129. try
  130. {
  131. PlayerPrefs.SetInt("musicMute", mbMusicMute ? 1 : 0);
  132. PlayerPrefs.SetInt("soundMute", mbSoundMute ? 1 : 0);
  133. PlayerPrefs.SetInt("musicVolume", mMusicVolume);
  134. PlayerPrefs.SetInt("soundVolume", mSoundVolume);
  135. PlayerPrefs.SetInt("graphicLevel", mGraphicLevel);
  136. }
  137. catch (System.Exception e)
  138. {
  139. DebugHelper.LogError("GameSettings: {0}", e.Message);
  140. }
  141. mbSettingChanged = false;
  142. }
  143. }
  144. bool IsGraphicLevelValid(int level)
  145. {
  146. if (SystemInfo.deviceType == DeviceType.Handheld)
  147. {
  148. #if UNITY_IPHONE
  149. UnityEngine.iOS.DeviceGeneration iosGen = UnityEngine.iOS.Device.generation;
  150. if(level >= 2)
  151. {
  152. if (iosGen < UnityEngine.iOS.DeviceGeneration.iPhone6)
  153. {
  154. return false;
  155. }
  156. }
  157. else if(level == 1)
  158. {
  159. if (iosGen < UnityEngine.iOS.DeviceGeneration.iPhone5)
  160. {
  161. return false;
  162. }
  163. }
  164. #elif UNITY_ANDROID
  165. /*if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.GOOD_DEVICE)
  166. return true;
  167. else if(DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE)
  168. {
  169. return level < GraphicLevelHigh;
  170. }else
  171. {
  172. return level < GraphicLevelMiddle;
  173. }*/
  174. #endif
  175. }
  176. return true;
  177. }
  178. }