76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
|
|
public class ChaoXiang : MonoBehaviour
|
|
{
|
|
public Text text;
|
|
|
|
private bool compassEnabled = false;
|
|
|
|
private bool isFirstLoad = true;
|
|
|
|
public float heading;
|
|
|
|
private bool hasDebugged = false;
|
|
|
|
void Start()
|
|
{
|
|
if (isFirstLoad)
|
|
{
|
|
// 启用设备指南针
|
|
Input.compass.enabled = true;
|
|
compassEnabled = Input.compass.enabled;
|
|
|
|
if (!compassEnabled)
|
|
{
|
|
text.text = "设备不支持指南针功能";
|
|
}
|
|
|
|
isFirstLoad = false;
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (compassEnabled)
|
|
{
|
|
heading = Input.compass.trueHeading;
|
|
string direction = GetDirectionFromHeading(heading);
|
|
text.text = $"指南针状态: {(Input.compass.enabled ? "已启用" : "未启用")}\n";
|
|
text.text += $"原始数据: {Input.compass.rawVector}\n";
|
|
text.text += $"当前朝向:{direction} ({heading:F1}°)";
|
|
|
|
|
|
// 只在第一次获取到角度时添加调试信息
|
|
if (!hasDebugged&&heading!=0)
|
|
{
|
|
transform.parent.rotation = Quaternion.Euler(0, heading, 0);
|
|
hasDebugged = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
private string GetDirectionFromHeading(float heading)
|
|
{
|
|
if (heading >= 337.5f || heading < 22.5f)
|
|
return "北";
|
|
else if (heading >= 22.5f && heading < 67.5f)
|
|
return "东北";
|
|
else if (heading >= 67.5f && heading < 112.5f)
|
|
return "东";
|
|
else if (heading >= 112.5f && heading < 157.5f)
|
|
return "东南";
|
|
else if (heading >= 157.5f && heading < 202.5f)
|
|
return "南";
|
|
else if (heading >= 202.5f && heading < 247.5f)
|
|
return "西南";
|
|
else if (heading >= 247.5f && heading < 292.5f)
|
|
return "西";
|
|
else if (heading >= 292.5f && heading < 337.5f)
|
|
return "西北";
|
|
|
|
return "未知";
|
|
}
|
|
}
|