Version: 2017.1

Assert.AreApproximatelyEqual

切换到手册
public static void AreApproximatelyEqual (float expected, float actual);
public static void AreApproximatelyEqual (float expected, float actual, string message);
public static void AreApproximatelyEqual (float expected, float actual, float tolerance);
public static void AreApproximatelyEqual (float expected, float actual, float tolerance, string message);

参数

tolerance 近似公差。

描述

断言值近似相等。使用绝对错误检查执行近似相等性检查(|a-b| < 公差)。默认公差为 0.00001f。

注意:每次在指定公差的情况下调用此方法时,都会创建新的 FloatComparer 实例。出于性能原因,您可能需要实例化自己的比较器并将其传递到 AreEqual 方法。如果未指定公差,则使用默认比较器,并且问题不会出现。

using UnityEngine;
using UnityEngine.Assertions;

public class AssertionExampleClass : MonoBehaviour { void Update() { //Make sure the positino of the GameObject is always in the center of the scene. //AreApproximatelyEqual should be used for comparing floating point variables. //Unless specified, default error tolerance will be used. Assert.AreApproximatelyEqual(0.0f, transform.position.x); Assert.AreApproximatelyEqual(0.0f, transform.position.y); Assert.AreApproximatelyEqual(0.0f, transform.position.z); } }