Unity 5.1+ ,加入了“断言库”,在 Asset
类中可以方便的找到需要使用断言的函数。
为何使用断言
使用断言可以让错误在产品“开发阶段”尽快的暴露,减少Debug的时间,这个阶段打开“断言”。“发布阶段”则关闭断言。
代码示例
using UnityEngine;
//使用Assert类需要这个包
using UnityEngine.Assertions;
public class Test : MonoBehaviour
{
//airport有可能为null
public Airport airport;
void Update()
{
//断言不为Null,如果为Null就会报错
//UnassignedReferenceException: The variable airport of Test has not been assigned.
//You probably need to assign the airport va