Unity3D动态变量声明

本文关键字:声明 变量 动态 Unity3D | 更新日期: 2023-09-27 18:08:30

我在尝试创建一个动态变量时遇到了麻烦,就像下面的例子:

→ public TYPE_UNKNOW myType;
void Awake(){
//i want to make myType as SpriteRenderer Or Image or int float etc.
}

感谢您的回复。

Unity3D动态变量声明

这被称为隐式类型,当你想要这样做时,你只需要声明一个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>();