Assert 类包含用于在代码中设置不变量的断言方法。
所有方法调用均将按条件仅包含在开发
版中,除非明确指定(请参阅 BuildOptions.ForceEnableAssertions)。
断言的包含由 UNITY_ASSERTIONS 定义控制。
      一个断言方法的失败不会中断执行的控制流程。
      失败时,系统会将断言消息记录在日志中 (LogType.Assert),而执行
      会继续进行。如果 Assert.raiseExceptions 设置为 true,则系统会抛出 AssertionException
        而不是将消息记录在日志中。
      如果调试器附加到项目(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); } }
| raiseExceptions | Should 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。 |