Compass.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using System.Linq;
  7. public class Compass : MonoBehaviour
  8. {
  9. [SerializeField] Text text;
  10. [SerializeField] Text text1;
  11. [SerializeField] Text text2;
  12. [SerializeField] Text text3;
  13. [SerializeField] Image compass;
  14. [SerializeField] Transform scene;
  15. [SerializeField] Text sceneText;
  16. public LineRenderer X;
  17. public LineRenderer Z;
  18. List<float> list_rot;
  19. bool state;
  20. float dushu = 0;//记录北方度数
  21. float tempdushu = 0;//临时记录数据来判断角度变化是否大于2
  22. // Update is called once per frame
  23. // Use this for initialization
  24. void Start()
  25. {
  26. list_rot = new List<float>();
  27. Input.compass.enabled = true;
  28. showLine();
  29. state = true;
  30. }
  31. //百度一下利用Input.compass实现指北针
  32. //OnGUI的调用次数
  33. void OnGUI()
  34. {
  35. //Input.location.Start();
  36. //地理北极就是北方,也是磁南极
  37. //GUILayout.Label(" rawVector: " + Input.compass.rawVector.ToString() //用microteslas测量的原始地磁数据
  38. // + " trueHeading: " + Input.compass.trueHeading.ToString() //相对应地理北极的度数
  39. // + " headingAccuracy: " + Input.compass.headingAccuracy.ToString() //标题度数的准确度
  40. // + " magneticHeading: " + Input.compass.magneticHeading.ToString(), GUILayout.Width(5000), GUILayout.Width(200));//相对于磁北极的度数
  41. }
  42. void FixedUpdate()
  43. {
  44. //如何确定参照物
  45. //当度数为 358-2度 手机的正前方就是北方
  46. //Input.location.Start();
  47. //text.text = " rawVector: " + Input.compass.rawVector.ToString();//用microteslas测量的原始地磁数据
  48. ////相对应地理北极的度数 手机头正对方向 北方360/0 东方90 西方180 南方270
  49. //text1.text = " trueHeading: " + Input.compass.trueHeading.ToString();
  50. //text2.text = " headingAccuracy: " + Input.compass.headingAccuracy.ToString(); //标题度数的准确度
  51. //text3.text = " magneticHeading: " + Input.compass.magneticHeading.ToString(); //相对于磁北极的度数
  52. /*trueHeading image/z
  53. 北方358 360 0 2 0
  54. 东方88 92 90
  55. 南方269 272 270
  56. 西方180 180
  57. */
  58. //为防止抖动 度数变化超过二的时候才赋值
  59. //if (Mathf.Abs(tempdushu - dushu) > 3)
  60. //{
  61. // //tempdushu = dushu;
  62. // //compass.transform.eulerAngles = new Vector3(0, 0, dushu);
  63. // scene.eulerAngles = new Vector3(0, dushu, 0);
  64. // sceneText.text = dushu.ToString();
  65. //}
  66. if (list_rot.Count < 40)
  67. {
  68. list_rot.Add(Input.compass.trueHeading);
  69. }
  70. else
  71. if (API_SVR.GetHead() != null && state)
  72. {
  73. state = false;
  74. dushu = list_rot.Average();
  75. // textMesh.text = dushu.ToString();
  76. scene.eulerAngles = new Vector3(0, 355f - dushu, 0);
  77. }
  78. //if (API_SVR.GetHead()!=null && state)
  79. //{
  80. // state = false;
  81. // dushu = Input.compass.trueHeading;
  82. // text.text = dushu.ToString();
  83. // scene.eulerAngles = new Vector3(0, 359.9f - dushu, 0);
  84. //}
  85. text.text = scene.eulerAngles.y.ToString();
  86. showLine();
  87. }
  88. void showLine()
  89. {
  90. X.positionCount = 2;
  91. X.SetPosition(0, transform.position + new Vector3(10, -1, 0));
  92. X.SetPosition(1, transform.position - new Vector3(10, 1, 0));
  93. Z.positionCount = 2;
  94. Z.SetPosition(0, transform.position + new Vector3(0, -1, 10));
  95. Z.SetPosition(1, transform.position - new Vector3(0, 1, 10));
  96. }
  97. }