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。 |