Version: 2017.2

Assert

class in UnityEngine.Assertions

切换到手册

描述

Assert 类包含用于在代码中设置不变量的断言方法。

所有方法调用均将按条件仅包含在开发 版中,除非明确指定(请参阅 BuildOptions.ForceEnableAssertions)。 断言的包含由 UNITY_ASSERTIONS 定义控制。

A failure of an assertion method does not break the control flow of the execution. On a failure, an assertion message is logged (LogType.Assert) and the execution continues. If Assert.raiseExceptions is set to true, an AssertionException is thrown instead of logging a message.

If a debugger is attached to the project (System.Diagnostics.Debugger.IsAttached is true), AssertionException will be thrown in order to pause the excecution and invoke the debugger.

For shorter and more readable syntax see Assertions.Must.MustExtensions.

using UnityEngine;
using UnityEngine.Assertions;

public class ExampleClass : MonoBehaviour { public int health; public GameObject go; void Update() { //You expect the health never to be equal to zero Assert.AreNotEqual(0, health);

//The referenced GameObject should be always (in every frame) be active Assert.IsTrue(go.activeInHierarchy); } }

For different assertions syntax see Assertions.Must.MustExtensions.

静态变量

raiseExceptionsShould an exception be thrown on a failure.

静态函数

AreApproximatelyEqual断言值近似相等。使用绝对错误检查执行近似相等性检查(|a-b| < 公差)。默认公差为 0.00001f。注意:每次在指定公差的情况下调用此方法时,都会创建新的 FloatComparer 实例。出于性能原因,您可能需要实例化自己的比较器并将其传递到 AreEqual 方法。如果未指定公差,则使用默认比较器,并且问题不会出现。
AreEqual断言值相等。如果未指定比较器,则使用 EqualityComparer<T>.Default。
AreNotApproximatelyEqual断言值近似不相等。使用绝对错误检查执行近似相等性检查(|a-b| < 公差)。默认公差为 0.00001f。
AreNotEqual断言值不相等。
IsFalse断言条件是 false。
IsNotNull断言值不为空。
IsNull断言值为空。
IsTrue断言条件是 true。