类似Vector3之类的参数使用默认值的方法参考

类似下面这种写法,编译器会报错:

public LoadInfo(string name, string url, string extension = "glb", Vector3 offset = Vector3.zero)
{
	this.name = name;
	this.url = url;
	this.extension = extension;
	this.offset = offset;
}

报错的原因是Vector3.zero不是编译时常量。

最后面的参数offset的默认值应该改成下面写法:

Vector3 offset = default

还有一个,就是发现C#里面有这样的写法,就是类型后面加?意思就是允许值类型有空值,类似这样:

Vector3? offset = null;

就是表示这个offset没有值,就是什么值都没有,连Vector3.zero也不是。

猜你喜欢

转载自blog.csdn.net/ttod/article/details/130551369