DeviceSN.cs 2.0 KB

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