1、打开Unity,新建一个空工程,具体如下图
2、在工程中,新建一个脚本,命名为 Test,并右键脚本 Open C# Project 打开,具体如下图
3、在打开的脚本上编写代码,具体代码和代码说明如下图
4、脚本的具体内容如下:using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test : MonoBehaviour { // Use this for initialization void Start () { //C#语法中一个个问号(?)的运算符是指:可以为 null 的类型 int? a = null; print("? a = " +a); /* * ?? 运算符称为 null 合并运算符,用于定义可以为 null 值的 * 类型和引用类型的默认值。如果此运算符的左操作数不为 null, * 则此运算符将返回左操作数(左边表达式);否则当左操作数为 * null,返回右操作数(右边表达式) */ int b = a ?? 10; print("?? b = " +b); //a??1 等价于 a == null ? 1 : a int? c = (a != null) ? a: 10; print("?: c = " + c); } }
5、脚本编译正确,回到Unity界面,在场景中添加一个 GameObject,并把脚本赋给 GameObject,具体如下图
6、运行场景,控制台Console打印结果,具体如下图
7、到此,《Unity 学习教程 之 C# 编程中?和 ??什么意思》讲解结束,谢谢