using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using System.Linq; public class Compass : MonoBehaviour { [SerializeField] Text text; [SerializeField] Text text1; [SerializeField] Text text2; [SerializeField] Text text3; [SerializeField] Image compass; [SerializeField] Transform scene; [SerializeField] Text sceneText; public LineRenderer X; public LineRenderer Z; List list_rot; bool state; float dushu = 0;//记录北方度数 float tempdushu = 0;//临时记录数据来判断角度变化是否大于2 // Update is called once per frame // Use this for initialization void Start() { list_rot = new List(); Input.compass.enabled = true; showLine(); state = true; } //百度一下利用Input.compass实现指北针 //OnGUI的调用次数 void OnGUI() { //Input.location.Start(); //地理北极就是北方,也是磁南极 //GUILayout.Label(" rawVector: " + Input.compass.rawVector.ToString() //用microteslas测量的原始地磁数据 // + " trueHeading: " + Input.compass.trueHeading.ToString() //相对应地理北极的度数 // + " headingAccuracy: " + Input.compass.headingAccuracy.ToString() //标题度数的准确度 // + " magneticHeading: " + Input.compass.magneticHeading.ToString(), GUILayout.Width(5000), GUILayout.Width(200));//相对于磁北极的度数 } void FixedUpdate() { //如何确定参照物 //当度数为 358-2度 手机的正前方就是北方 //Input.location.Start(); //text.text = " rawVector: " + Input.compass.rawVector.ToString();//用microteslas测量的原始地磁数据 ////相对应地理北极的度数 手机头正对方向 北方360/0 东方90 西方180 南方270 //text1.text = " trueHeading: " + Input.compass.trueHeading.ToString(); //text2.text = " headingAccuracy: " + Input.compass.headingAccuracy.ToString(); //标题度数的准确度 //text3.text = " magneticHeading: " + Input.compass.magneticHeading.ToString(); //相对于磁北极的度数 /*trueHeading image/z 北方358 360 0 2 0 东方88 92 90 南方269 272 270 西方180 180 */ //为防止抖动 度数变化超过二的时候才赋值 //if (Mathf.Abs(tempdushu - dushu) > 3) //{ // //tempdushu = dushu; // //compass.transform.eulerAngles = new Vector3(0, 0, dushu); // scene.eulerAngles = new Vector3(0, dushu, 0); // sceneText.text = dushu.ToString(); //} if (list_rot.Count < 40) { list_rot.Add(Input.compass.trueHeading); } else if (API_SVR.GetHead() != null && state) { state = false; dushu = list_rot.Average(); // textMesh.text = dushu.ToString(); scene.eulerAngles = new Vector3(0, 355f - dushu, 0); } //if (API_SVR.GetHead()!=null && state) //{ // state = false; // dushu = Input.compass.trueHeading; // text.text = dushu.ToString(); // scene.eulerAngles = new Vector3(0, 359.9f - dushu, 0); //} text.text = scene.eulerAngles.y.ToString(); showLine(); } void showLine() { X.positionCount = 2; X.SetPosition(0, transform.position + new Vector3(10, -1, 0)); X.SetPosition(1, transform.position - new Vector3(10, 1, 0)); Z.positionCount = 2; Z.SetPosition(0, transform.position + new Vector3(0, -1, 10)); Z.SetPosition(1, transform.position - new Vector3(0, 1, 10)); } }