Unity3D动态变量声明

时间:2016-09-22 13:11:46

标签: c# .net unity3d unityscript

我在尝试制作像这个例子的动态变量时遇到了麻烦:

→ public TYPE_UNKNOW myType;

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

}

感谢您的所有回复。

1 个答案:

答案 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>();

Source