本例我在Unity面板上设置了三个text UI,用于检测输出的结果是否正确
Update方法中第一行控制输出的数值小数点后位数为1,第二行为2,第三行为3
Math.Round()的调用一定要引用System,否则无法运行
using System; using UnityEngine; using UnityEngine.UI; public class accelerationScript : MonoBehaviour { public Text textx; public Text texty; public Text textz; private void Update() { textx.text = Math.Round(Input.acceleration.x,1).ToString(); texty.text = Math.Round(Input.acceleration.y,2).ToString(); textz.text = Math.Round(Input.acceleration.z,3).ToString(); } }