CStaticMethod.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Net.NetworkInformation;
  5. public class CStaticMethod
  6. {
  7. /// <summary>获得系统经过的时间</summary>
  8. /// <returns>int 毫秒</returns>
  9. public static int SystemAfterTime()
  10. {
  11. return (int)(Time.realtimeSinceStartup * 1000);
  12. }
  13. /// <summary>帧时间</summary>
  14. public static int SystemFrameTime()
  15. {
  16. return (int)(Time.deltaTime * 1000);
  17. }
  18. private static System.Random mRandomFloat = null;
  19. /// <summary>获得一个0.0到1.0之间的随机数</summary>
  20. public static float RandomFloat()
  21. {
  22. if(mRandomFloat==null)
  23. {
  24. mRandomFloat = new System.Random();
  25. }
  26. return (float)mRandomFloat.NextDouble();
  27. }
  28. /// <summary>随机一个指定区间的整数</summary>
  29. public static int RandomNext(int nMin, int nMax)
  30. {
  31. if(mRandomFloat==null)
  32. {
  33. mRandomFloat = new System.Random();
  34. }
  35. return mRandomFloat.Next (nMin, nMax);
  36. }
  37. /// <summary>格式化时间</summary>
  38. public static string FormatTime(float secondValue)
  39. {
  40. int intSecond = Mathf.FloorToInt(secondValue);
  41. //小时
  42. int hour = Mathf.FloorToInt(secondValue / 60 / 60);
  43. //分钟
  44. int minute = Mathf.FloorToInt((secondValue - hour * 60) / 60);
  45. //秒
  46. int second = intSecond % 60;
  47. System.Text.StringBuilder strBuf = new System.Text.StringBuilder();
  48. if(hour!=0)
  49. {
  50. strBuf.Append(hour);
  51. strBuf.Append(":");
  52. }
  53. if (minute == 0)
  54. {
  55. strBuf.Append("00");
  56. strBuf.Append(":");
  57. }
  58. else if (minute < 10)
  59. {
  60. strBuf.Append("0");
  61. strBuf.Append(minute);
  62. strBuf.Append(":");
  63. }
  64. else
  65. {
  66. strBuf.Append(minute);
  67. strBuf.Append(":");
  68. }
  69. if (second == 0)
  70. {
  71. strBuf.Append("00");
  72. }
  73. else if (second < 10)
  74. {
  75. strBuf.Append("0");
  76. strBuf.Append(second);
  77. }
  78. else
  79. {
  80. strBuf.Append(second);
  81. }
  82. return strBuf.ToString();
  83. }
  84. /// <summary>格式化时间</summary>
  85. public static string FormatShortTime(float secondValue)
  86. {
  87. int intSecond = Mathf.FloorToInt(secondValue);
  88. //小时
  89. int hour = Mathf.FloorToInt(secondValue / 60 / 60);
  90. //分钟
  91. int minute = Mathf.FloorToInt((secondValue - hour * 60) / 60);
  92. //秒
  93. int second = intSecond % 60;
  94. System.Text.StringBuilder strBuf = new System.Text.StringBuilder ();
  95. if (minute == 0)
  96. {
  97. strBuf.Append("00");
  98. strBuf.Append(":");
  99. }
  100. else if (minute < 10)
  101. {
  102. strBuf.Append("0");
  103. strBuf.Append(minute);
  104. strBuf.Append(":");
  105. }
  106. else
  107. {
  108. strBuf.Append(minute);
  109. strBuf.Append(":");
  110. }
  111. if (second == 0)
  112. {
  113. strBuf.Append("00");
  114. }
  115. else if (second < 10)
  116. {
  117. strBuf.Append("0");
  118. strBuf.Append(second);
  119. }
  120. else
  121. {
  122. strBuf.Append(second);
  123. }
  124. return strBuf.ToString();
  125. }
  126. /// <summary>获得资源的名字</summary>
  127. public static string GetAssetsName(UnityEngine.Object assetsObject, string prefixName)
  128. {
  129. System.Text.StringBuilder strBuf = new System.Text.StringBuilder ();
  130. strBuf.Append (prefixName);
  131. strBuf.Append (assetsObject.name);
  132. return strBuf.ToString ();
  133. }
  134. /// <summary>是否为编辑器环境</summary>
  135. public static bool IsEditor
  136. {
  137. get
  138. {
  139. #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
  140. return true;
  141. #else
  142. return true;
  143. #endif
  144. }
  145. }
  146. /// <summary>调试设备地址</summary>
  147. public static void DebugDeviceMac()
  148. {
  149. NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
  150. for (int i = 0; i < nis.Length; i++)
  151. {
  152. CDebug.Log("name = " + nis[i].Name);
  153. CDebug.Log("des = " + nis[i].Description);
  154. CDebug.Log("type = " + nis[i].NetworkInterfaceType.ToString());
  155. CDebug.Log("mac = " + nis[i].GetPhysicalAddress().ToString());
  156. CDebug.Log("---------------------------");
  157. }
  158. }
  159. public static string GetSubjectNameById(int value)
  160. {
  161. switch(value)
  162. {
  163. case 1: return "化学";
  164. case 2: return "生物";
  165. case 3: return "物理";
  166. default:
  167. return "未知课程";
  168. }
  169. }
  170. public static int GetSelectIndexByAnswer(string value)
  171. {
  172. switch (value)
  173. {
  174. case "A": return 0;
  175. case "B": return 1;
  176. case "C": return 2;
  177. case "D": return 3;
  178. default:
  179. return -1;
  180. }
  181. }
  182. public static string FormatParagraph(string str, int count)
  183. {
  184. var paragraph = "";
  185. for (int i = 0; i < str.Length; i++)
  186. {
  187. paragraph += str[i];
  188. if (paragraph.Length % count == 0)
  189. {
  190. paragraph += "\n";//不需要换行就注掉
  191. }
  192. }
  193. return paragraph;
  194. }
  195. public static string SerialId()
  196. {
  197. #if UNITY_EDITOR
  198. return System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString();
  199. #elif UNITY_ANDROID
  200. AndroidJavaClass jo = new AndroidJavaClass(className: "android.os.Build");
  201. string serial = jo.GetStatic<string>(fieldName: "SERIAL");
  202. return serial;
  203. #elif UNITY_IPHONE
  204. return "UNITY_IPHONE";
  205. #endif
  206. }
  207. public static string DeviceType()
  208. {
  209. #if UNITY_EDITOR
  210. return "windows";
  211. #elif UNITY_ANDROID
  212. return "android";
  213. #elif UNITY_IPHONE
  214. return "ios";
  215. #else
  216. return "default";
  217. #endif
  218. }
  219. public static int CurTimeStamp()
  220. {
  221. return (int)((System.DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  222. }
  223. }