Assert

class in UnityEngine.Assertions

切换到手册

描述

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

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

默认情况下,只要断言失败,资源就抛出异常。 不过,可将 Assertions.Assert._raiseExceptions 设置为 false,这样 Unity 会改用 LogType.Assert 记录消息。

如果调试器附加到项目(System.Diagnostics.Debugger.IsAttached 为 true), 将抛出 AssertionException 以暂停执行和 调用调试器。

using UnityEngine;
using UnityEngine.Assertions;

public class AssertionExampleClass : 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); } }

静态函数

AreApproximatelyEqual断言值近似相等。
AreEqual断言值相等。
AreNotApproximatelyEqual断言值近似不相等。
AreNotEqual断言值不相等。
IsFalse当条件不成立时,返回 /true/。否则,返回 /false/。
IsNotNull断言值不为 null。
IsNull断言值为 null。
IsTrue断言条件是 true。