我在尝试制作像这个例子的动态变量时遇到了麻烦:
→ public TYPE_UNKNOW myType;
void Awake(){
//i want to make myType as SpriteRenderer Or Image or int float etc.
}
感谢您的所有回复。
答案 0 :(得分:0)
这称为隐式类型,当您想要这样做时,您只需声明 var 类型变量。 var 关键字告诉编译器从初始化语句右侧的表达式推断变量的类型。
示例:
// i is compiled as an int
var i = 5;
// s is compiled as a string
var s = "Hello";
// a is compiled as int[]
var a = new[] { 0, 1, 2 };
// expr is compiled as IEnumerable<Customer>
// or perhaps IQueryable<Customer>
var expr =
from c in customers
where c.City == "London"
select c;
// anon is compiled as an anonymous type
var anon = new { Name = "Terry", Age = 34 };
// list is compiled as List<int>
var list = new List<int>();