首页 > 代码库 > unity学习之总结比较
unity学习之总结比较
1、for与while循环
while 循环用于循环次数不是确定的情况
for 循环用于循环次数确定的情况下使用
for:
for ( int i = 0; i < 100; i++)
{
Debug.Log("第:"+(i+1)+"次输出!");
}
while:
int i=0;
while(i<100)
{
Debug.Log("第:"+(i+1)+"次输出!");
i++;
}
http://wanjushu.blog.163.com/blog/static/8217553120129150616435/
http://wenwen.sogou.com/z/q104785129.htm
2、array和list存储
数组的容量是固定的,您只能一次获取或设置一个元素的值,而List的容量可根据需要自动扩充、修改、删除或插入数据。
array:
C# array数组的用法范例:type[] typename=new type[size];
int a=1,b=3,c=5;
int []array=new int[100];
array[0] = a;
array[1] = b;
array[2] = c;
Debug.Log(array[2]);
list:
unity中使用list列表要先引用using System.Collections.Generic;
list<列表类型>列表名=new list<列表类型>();
这样给他实例化一个对象,用for循环添加数
public List<int> value = http://www.mamicode.com/new List
void Start()
{
for ( int i = 0; i < 100; i++)
{
value.Add(i+1);
}
Debug.Log(value[6]);
}
http://blog.csdn.net/zhang_xinxiu/article/details/8657431
http://jingyan.baidu.com/album/e73e26c0f1742d24adb6a7c9.html
http://zeroclint.blog.163.com/blog/static/69213130201121410059116/
3、if-else和switch选择语句
switch可做开关语句作用在一个条件上时简单方便,if-else可作用于多个条件
if-else:
public int a = 7;
void Start()
{
if (a == 8)//括号里为条件判断
{
Debug.Log("a是8!");
}
else
{
Debug.Log("a是几?");
}
}
switch:
public enum WeekDay
{
SunDay = 0,
MonDay,
TuesDay,
WednesDay,
ThursDay,
FriDay,
SaturDay
}
public WeekDay _myWeekDay = WeekDay.SunDay;
switch (_myWeekDay)
{
case WeekDay.MonDay:
Debug.Log("今天是星期一");
break;
case WeekDay.TuesDay:
Debug.Log("今天是星期二");
break;
case WeekDay.WednesDay:
Debug.Log("今天是星期三");
break;
case WeekDay.ThursDay:
Debug.Log("今天是星期四");
break;
case WeekDay.FriDay:
Debug.Log("今天是星期五");
break;
case WeekDay.SaturDay:
Debug.Log("今天是星期六");
break;
case WeekDay.SunDay:
Debug.Log("今天是星期日");
break;
}
http://blog.sina.com.cn/s/blog_5011e60b0101a9s3.html
http://wenku.baidu.com/link?url=Ah_u2IwLrhd-LqiP3qgAQXcI8bp5S9Z_9__7CGqSvUISUqjvMybliIT-QstpWPBvShPZgKmAbqvmSQcIws15QJcE6hGWFwzCOJIlg1J11Ne
http://zhidao.baidu.com/link?url=kf98VIwD4MNKnzlDSWBMdKaf2hwP7gUHbciqT3JEPgFYtyK9VAulf6SifMjnDUh0Ss87icpDM9FZdPoDX7HJQK