Version: 2021.3

Assert

class in UnityEngine.Assertions

切换到手册

描述

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

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

Assert throws exceptions whenever an assertion fails.

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.

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。