DeviceSN.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using UnityEngine;
  3. public class DeviceSN : Singleton<DeviceSN>
  4. {
  5. private string serial;
  6. /// <summary>
  7. /// 获取设备ID的后十位数字,在Unity中时为预设的设备ID
  8. /// </summary>
  9. /// <returns></returns>
  10. public long SendSerial()
  11. {
  12. AndroidJavaObject jo = new AndroidJavaObject("android.os.Build");
  13. serial = jo.GetStatic<string>("SERIAL");
  14. return SubStringDefault(serial);
  15. }
  16. public string SendSerialBy16()
  17. {
  18. //Debug.Log(Convert.ToString(DeviceSN.GetInstance().SendSerial(), 16));
  19. //return Convert.ToString(DeviceSN.GetInstance().SendSerial(), 16);
  20. #if UNITY_EDITOR
  21. return "ABD1234561";
  22. #elif UNITY_ANDROID
  23. return Convert.ToString(DeviceSN.Instance.SendSerial(), 16);
  24. #endif
  25. }
  26. /// <summary>
  27. /// 获取从后往前数length位的string
  28. /// </summary>
  29. /// <param name="str">需要获取的源字符串</param>
  30. /// <param name="index">从第多少位开始往前数</param>
  31. /// <param name="length">往前数多少位</param>
  32. /// <returns></returns>
  33. public string SubStringByEnd(string str, int index, int length)
  34. {
  35. return str.Substring((index - length) < 0 ? 0 : (index - length), length);
  36. }
  37. /// <summary>
  38. /// 获取从最后一位开始往前数10位的数字
  39. /// </summary>
  40. /// <param name="str">输入源字符串</param>
  41. /// <returns></returns>
  42. public long SubStringDefault(string str)
  43. {
  44. bool r = long.TryParse(SubStringByEnd(str, str.Length, 10),out long result);
  45. if (!r)
  46. {
  47. Debug.LogError($"获取TagID失败 : {result}");
  48. }
  49. return result;
  50. }
  51. }