DeviceSN.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using QFramework;
  2. using System;
  3. using UnityEngine;
  4. public class DeviceSN : MonoSingleton<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 "ABD123456";
  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. }