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 variable of the Test script in the

本文介绍了Unity 5.1+引入的断言库,阐述了为何在开发阶段使用断言以尽早发现错误,提供代码示例展示如何在Unity中启用断言,并讲解了设置宏开关及控制台显示异常的情况。
最低0.47元/天 解锁文章
448

被折叠的 条评论
为什么被折叠?



