声明三个变量,分别为Vector2、Vector3和Vector4类型,然后给他们的x分量设为带三位小数的0.375,用Debug.Log输出。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestVector : MonoBehaviour
{
void Start()
{
Vector2 vector2 = new Vector2(0.375f, 0);
Vector3 vector3 = new Vector3(0.375f, 0, 0);
Vector4 vector4 = new Vector4(0.375f, 0, 0, 0);
Debug.Log($"vector2:{vector2},vector2.x:{vector2.x}");
Debug.Log($"vector3:{vector3},vector3.x:{vector3.x}");
Debug.Log($"vector4:{vector4},vector4.x:{vector4.x}");
}
}
由下面的输出结果,我们发现直接打印vector变量时,它的精度会被改变,由0.375四舍五入到0.38,但如果打印它的x分量,发现数值精度并没有改变。
推断是因为Debug.Log使用Vector的ToString()方法时,做了一个只保留两位小数的处理,实际上Vector保存的每个分量的精度没有被改变。
综上,要谨慎使用Vector类型的ToString()方法,某些情况如果将Vector保存成文本类型再重新读取可能会出现问题。