TestVector.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class TestVector : MonoBehaviour
  6. {
  7. //------------------------外部操作接口------------------------------------------
  8. public static TestVector ins;
  9. //控制模型显示数量,模型最多同时支持4个,数组索引对应屏幕的渲染位置0:左上,1:右上;2:左下;3:右下
  10. int modelCount = 4;
  11. public void SetAcc(params Vector3[] accList)
  12. {
  13. for (int i = 0; i < accList.Length; i++)
  14. {
  15. objectsList[i].Find("Acc").GetChild(0).localPosition = accList[i];
  16. }
  17. }
  18. public void SetMag(params Vector3[] magList)
  19. {
  20. for (int i = 0; i < magList.Length; i++)
  21. {
  22. objectsList[i].Find("Mag").GetChild(0).localPosition = magList[i];
  23. }
  24. }
  25. public void Set9AxisRotation(params Quaternion[] quatList)
  26. {
  27. for (int i = 0; i < quatList.Length; i++)
  28. {
  29. //渲染9Axis.update得出的Rotation
  30. objectsList[i].Find("AMesh").localRotation = quatList[i];
  31. }
  32. }
  33. //--------------------------------------------------------------------------
  34. [SerializeField] Button btnTap;
  35. List<Transform> objectsList = new List<Transform>();
  36. bool isDebugging;
  37. void Start()
  38. {
  39. if (ins)
  40. {
  41. Destroy(gameObject);
  42. return;
  43. }
  44. ins = this;
  45. DontDestroyOnLoad(gameObject);
  46. // init
  47. for (int i = 0; i < modelCount; i++)
  48. {
  49. objectsList.Add(transform.Find("Objects" + i));
  50. }
  51. btnTap.onClick.AddListener(() =>
  52. {
  53. isDebugging = !isDebugging;
  54. RenderByDebugging();
  55. });
  56. RenderByDebugging();
  57. StartCoroutine(AsyncCaptureExtraUI());
  58. }
  59. void OnDestroy()
  60. {
  61. if (ins == this) ins = null;
  62. }
  63. // Update is called once per frame
  64. void RenderByDebugging()
  65. {
  66. btnTap.GetComponentInChildren<Text>().text = (isDebugging ? "关闭" : "开启") + "调试";
  67. foreach (var o in objectsList) o.gameObject.SetActive(isDebugging);
  68. transform.Find("Canvas/ellipse-Image").gameObject.SetActive(isDebugging);
  69. transform.Find("Canvas/ExtraUI").gameObject.SetActive(isDebugging);
  70. StartCoroutine(AsyncNoticeBluetoothHolder());
  71. }
  72. IEnumerator AsyncNoticeBluetoothHolder()
  73. {
  74. while (!BluetoothHolder.ins)
  75. {
  76. yield return null;
  77. }
  78. if (isDebugging)
  79. {
  80. BluetoothHolder.ins.ShowMagEllipse(this);
  81. }
  82. else
  83. {
  84. BluetoothHolder.ins.HideMagEllipse(this);
  85. }
  86. }
  87. IEnumerator AsyncCaptureExtraUI()
  88. {
  89. while (!BluetoothHolder.ins)
  90. {
  91. yield return null;
  92. }
  93. Transform a = BluetoothHolder.ins.transform.Find("Canvas/MagCalibrationButton");
  94. Transform b = BluetoothHolder.ins.transform.Find("Canvas/GyrCalibrationButton");
  95. a.parent = transform.Find("Canvas/ExtraUI");
  96. b.parent = transform.Find("Canvas/ExtraUI");
  97. a.localScale = Vector3.one;
  98. b.localScale = Vector3.one;
  99. a.GetComponent<RectTransform>().anchoredPosition = new Vector3(-800, -40, 0);
  100. b.GetComponent<RectTransform>().anchoredPosition = new Vector3(-800, -100, 0);
  101. }
  102. }