DeviceInfo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. public enum eNetType
  4. {
  5. NetType_None = 0, //没有联网
  6. NetType_4G = 1, //4G网络状态
  7. NetType_WIFI = 2, //WIFI状态
  8. NetType_Other = 3, //其他网络状态
  9. }
  10. public class DeviceInfo
  11. {
  12. public enum eDeviceState
  13. {
  14. BAD_DEVICE = 1, //性能差的设备
  15. NORMAL_DEVICE = 2, //性能一般的设备
  16. GOOD_DEVICE = 3 //性能好的设备
  17. }
  18. /// <summary>
  19. /// 设备性能状态
  20. /// </summary>
  21. public static eDeviceState m_DeviceState = eDeviceState.GOOD_DEVICE;
  22. /// <summary>
  23. /// 设备内存大小
  24. /// </summary>
  25. public static int m_DeviceMem = 0;
  26. /// <summary>
  27. /// CPU类型
  28. /// </summary>
  29. public static string m_CpuType = "";
  30. //显卡型号
  31. public static string m_GraphicType = "";
  32. public static string m_deviceId = "";
  33. static int m_OrgBrightness = 0;
  34. /// <summary>
  35. /// 当前屏幕亮度
  36. /// </summary>
  37. static int m_CurrentBrightness = 100;
  38. /// <summary>
  39. /// 设备详细信息
  40. /// </summary>
  41. public static string s_DeviceDetailStr = "";
  42. /// <summary>
  43. /// 判断是否为好设备
  44. /// </summary>
  45. public static bool IsGoodDevice
  46. {
  47. get { return m_DeviceState == eDeviceState.GOOD_DEVICE; }
  48. }
  49. /// <summary>
  50. /// 获取网络状态: 0: 表示没有联网; 1:表示当前为WIFI状态;2:表示状态为2G状态; 3:表示当前为3G状态
  51. /// </summary>
  52. /// <returns></returns>
  53. public static eNetType GetNetType()
  54. {
  55. eNetType netType = (eNetType)Application.internetReachability;
  56. return netType;
  57. }
  58. /// <summary>
  59. /// 获取设备信息并按照信息进行设备级别划分
  60. /// 低端机配置:内存 2G, 显存:512M
  61. /// 中端机配置: 内存 3G, 显存:1G
  62. /// 高端机配置: 内存大于3G, 显存大于1G
  63. /// </summary>
  64. public static void GetDeviceState()
  65. {
  66. string m_DeviceName = SystemInfo.deviceModel;
  67. string m_GraphicName = SystemInfo.graphicsDeviceName;
  68. int m_DeviceVMem = SystemInfo.graphicsMemorySize;
  69. m_DeviceMem = SystemInfo.systemMemorySize;
  70. m_deviceId = SystemInfo.deviceUniqueIdentifier;
  71. s_DeviceDetailStr = string.Format("设备信息: Name={0}, 显卡={1}, 内存={2}M, 显存={3}M, 分辨率={4} * {5}",
  72. m_deviceId,
  73. m_GraphicName,
  74. m_DeviceMem,
  75. m_DeviceVMem,
  76. Screen.width,
  77. Screen.height);
  78. Debug.Log(s_DeviceDetailStr);
  79. string deviceInfo = string.Format("supportsComputeShaders = {0},supportsInstancing={1},supports32bitsIndexBuffer ={2},supportedRenderTargetCount ={3},supportsMultisampledTextures={4}, maxTextureSize={5},supportsVertexPrograms={6}"
  80. ,SystemInfo.supportsComputeShaders
  81. ,SystemInfo.supportsInstancing
  82. ,SystemInfo.supports32bitsIndexBuffer
  83. ,SystemInfo.supportedRenderTargetCount
  84. ,SystemInfo.supportsMultisampledTextures
  85. ,SystemInfo.maxTextureSize
  86. ,SystemInfo.supportsVertexPrograms);
  87. Debug.Log(deviceInfo);
  88. #if UNITY_ANDROID && !UNITY_EDITOR
  89. //根据设备信息进行分类
  90. if(m_DeviceMem>=4000 && m_DeviceVMem>=1024)
  91. {
  92. m_DeviceState = eDeviceState.GOOD_DEVICE;
  93. }else if(m_DeviceMem>=3000 && m_DeviceVMem>512)
  94. {
  95. m_DeviceState = eDeviceState.NORMAL_DEVICE;
  96. }else
  97. {
  98. m_DeviceState = eDeviceState.BAD_DEVICE;
  99. }
  100. if(m_DeviceState != eDeviceState.BAD_DEVICE)
  101. {
  102. int graphicLevel = 0; //0:底,1中,2高
  103. if (m_GraphicName.Contains("Mali"))
  104. {
  105. if (m_GraphicName.Contains("Mali-G"))
  106. {
  107. string versionStr = m_GraphicName.Replace("Mali-G", "");
  108. if (!string.IsNullOrEmpty(versionStr))
  109. {
  110. int ver = 0;
  111. if (int.TryParse(versionStr, out ver))
  112. {
  113. if (ver <= 51)
  114. {
  115. graphicLevel = 0;
  116. }
  117. else if (ver <= 71)
  118. {
  119. graphicLevel = 1;
  120. }
  121. else
  122. {
  123. graphicLevel = 2;
  124. }
  125. }
  126. else
  127. {
  128. graphicLevel = -1;
  129. }
  130. }
  131. }
  132. else if (m_GraphicName.Contains("Mali-T"))
  133. {
  134. string versionStr = m_GraphicName.Replace("Mali-T", "");
  135. if (!string.IsNullOrEmpty(versionStr))
  136. {
  137. int ver = 0;
  138. if (int.TryParse(versionStr, out ver))
  139. {
  140. if (ver <= 760)
  141. {
  142. graphicLevel = 0;
  143. }
  144. else if (ver <= 880)
  145. {
  146. graphicLevel = 1;
  147. }
  148. else
  149. {
  150. graphicLevel = 2;
  151. }
  152. }
  153. else
  154. {
  155. graphicLevel = -1;
  156. }
  157. }
  158. }
  159. }
  160. else if (m_GraphicName.Contains("Adreno"))
  161. {
  162. string versionStr = m_GraphicName.Replace("Adreno (TM) ", "");
  163. if (!string.IsNullOrEmpty(versionStr))
  164. {
  165. int ver = 0;
  166. if (int.TryParse(versionStr, out ver))
  167. {
  168. if (ver <= 506)
  169. {
  170. graphicLevel = 0;
  171. }
  172. else if (ver <= 610)
  173. {
  174. graphicLevel = 1;
  175. }
  176. else
  177. {
  178. graphicLevel = 2;
  179. }
  180. }
  181. else
  182. {
  183. graphicLevel = -1;
  184. }
  185. }
  186. }
  187. if (graphicLevel == 0)
  188. {
  189. m_DeviceState = DeviceInfo.eDeviceState.BAD_DEVICE;
  190. }else if(graphicLevel == 1)
  191. {
  192. m_DeviceState = DeviceInfo.eDeviceState.NORMAL_DEVICE;
  193. }
  194. }
  195. if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.BAD_DEVICE
  196. || DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE)
  197. {
  198. float ratio = 0.7f;
  199. if(Screen.width > 1080)
  200. {
  201. ratio = 0.5f;
  202. }
  203. int wid = (int)(Screen.width * ratio);
  204. int hei = (int)(Screen.height * ratio);
  205. int newW = 0,newH;
  206. FindNearestResolution(wid,hei,out newW,out newH);
  207. Screen.SetResolution(newW, newH, true);
  208. Debug.LogError("newW:" + newW + " newH:" + newH);
  209. }
  210. #elif UNITY_IOS && !UNITY_EDITOR
  211. UnityEngine.iOS.DeviceGeneration iosGen = UnityEngine.iOS.Device.generation;
  212. if (iosGen >= UnityEngine.iOS.DeviceGeneration.iPhoneXS ||
  213. iosGen == UnityEngine.iOS.DeviceGeneration.iPad5Gen)
  214. DeviceInfo.m_DeviceState = DeviceInfo.eDeviceState.GOOD_DEVICE;
  215. else if (iosGen == UnityEngine.iOS.DeviceGeneration.iPhone7Plus ||
  216. iosGen == UnityEngine.iOS.DeviceGeneration.iPhone8Plus ||
  217. iosGen == UnityEngine.iOS.DeviceGeneration.iPhone8Plus ||
  218. iosGen == UnityEngine.iOS.DeviceGeneration.iPadPro10Inch2Gen)
  219. DeviceInfo.m_DeviceState = DeviceInfo.eDeviceState.NORMAL_DEVICE;
  220. else
  221. DeviceInfo.m_DeviceState = DeviceInfo.eDeviceState.BAD_DEVICE;
  222. if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.BAD_DEVICE
  223. || DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE)
  224. {
  225. float ratio = 0.7f;
  226. if(Screen.width > 1080)
  227. {
  228. ratio = 0.5f;
  229. }
  230. int wid = (int)(Screen.width * ratio);
  231. int hei = (int)(Screen.height * ratio);
  232. int newW = 0,newH;
  233. FindNearestResolution(wid,hei,out newW,out newH);
  234. Screen.SetResolution(newW, newH, true);
  235. Debug.LogError("newW:" + newW + " newH:" + newH);
  236. }
  237. #endif
  238. m_CurrentBrightness = m_OrgBrightness = GetBrightness();
  239. }
  240. public static void FindNearestResolution(int wid,int hei, out int oWid, out int oHei)
  241. {
  242. oWid = wid;
  243. oHei = hei;
  244. for (int idx=0; idx < Screen.resolutions.Length;idx++)
  245. {
  246. Resolution res = Screen.resolutions[idx];
  247. if(hei >= res.height)
  248. {
  249. oWid = res.width;
  250. oHei = res.height;
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// 还原为原始的屏幕亮度
  256. /// </summary>
  257. public static void RestoreBrightness()
  258. {
  259. SetBrightness(m_OrgBrightness);
  260. }
  261. /// <summary>
  262. /// 使屏幕变暗
  263. /// </summary>
  264. public static bool Dimming()
  265. {
  266. if (m_OrgBrightness <= 30)
  267. return false;
  268. SetBrightness(30);
  269. return true;
  270. }
  271. /// <summary>
  272. /// 设置屏幕亮度值:数值[0~100]
  273. /// </summary>
  274. /// <param name="val">屏幕亮度数值</param>
  275. static void SetBrightness(int val)
  276. {
  277. return;
  278. }
  279. /// <summary>
  280. /// 获取屏幕当前亮度
  281. /// </summary>
  282. /// <returns></returns>
  283. static int GetBrightness()
  284. {
  285. return 100;
  286. }
  287. }