1 任务35for循环不理解,涉及很多没学过的知识
1 任务35for循环不理解,涉及很多没学过的知识
ctrl + k ctrl + c 注释
print 只能在组件中输出
Debug.Log 可以在任意类型中输出、
组件的获取
GetComponent<transfrom>();
GetComponentsIn
forreach(Transform t in children){
if(t!=transform)
destory(t.gameObject);
}
t相当于遍历的每个孩子
switch(Herotype){
case Herotype.Solider:
printf("1");
break;
case Herotype.Tank:
printf("1");
break;
Default:
break;
}
switch(Herotype) {
case Herotype.Solider:
printf("1");
break;
}
组件的获取
访问自身组件
GetComponent<>();
关于transform组件
transform是一个组间变量,包含position,localPosition,eularAngles,rotation,parent,root等属性
碰撞器
Collider[] colliders=GetComponents<Collider>();
获得子物体组件
GetComponentInChildren<>();
获得其他物体身上的组件
public GameObject player;
player.GetComponent<>();
然后把inspector中想要获取的物体拖拽到
foreach(Transform t in children)
t代表children中的每个元素
for while dowhile
while(条件){}//只要条件为真则会一直执行
do{}while(条件);先执行一次,在判断是否满足条件
if变形
if(){}
else if(){}
枚举类型
调用的时候枚举类型名字.元素
举例
enum HeroType{
Soldier
}
HeroType heroType=HeroType.Soldier;
if(heroType==HeroType.Soldier){}
脚本中变量的定义
在脚本中定义的初始值,如果后续改变的话,程序会默认以inspector中的值为准
public和private
希望在inspector或者其他程序中修改的变量定义为public
在未声明private和public时默认为Private
创建校本和附加脚本
创建校本
1.直接在project中右键create
2.在选中的组件中add component
附加脚本
1.创建并附加
2.拖拽到组件中
3.在add component中
类中的方法
如果需要在类外进行调用的话,务必设置为public
利用类声明的变量,可以叫做对象
比如:enemy1
Enemy enemy1 = new Enemy();//构造对象
public:只有声明时是公开的才能在对象里访问
类和类之间可以继承
public class A:B{}
其中A可以叫做主类,A继承B,B中的东西A也拥有
类的声明
比如说现在已经创建好一个类Enemy了
Enemy enemy1 = new Enemy();
方法 method
返回值 方法名(参数){
//方法体
}
for循环遍历数组
数组名.Length可以直接获得数组长度
bool类型:true和false
字符
char
//单行注释
/* */多行注释