专门遍历数组:
foreach(Transform t in children){
print(t);}
(t代表每一次遍历的)
专门遍历数组:
foreach(Transform t in children){
print(t);}
(t代表每一次遍历的)
Transform:改变
Transform[]children
=transform.GetComponentsInChildren<Transform>();(包含当前物体)获物体
GameObject.Destroy(children[i].gameObject);
int i=0;
while(i<children.length){
if(children[i]!=transform){
GameObject.Destroy(children[i].gameObject);
}
i++;
}
do{
}
while();
switch case
int i=3;
switch(i){
case1:
break;
case2:
break;
default:
break;
}
switch(heroType){
case HeroType.Soldier:
print("123");
break;
}
int hp=100;
if(hp<=0&&hp<=20){
print(“可以用医疗险”);
}
enum HeroType{
Soldier,
Master,
Assassin,
Tank,
Shooter
}
HeroType heroType=HeroType.Soldier;
数学运算符:+ - * / %
赋值:= += -= *= /= %=
比较运算符:==等等
逻辑运算符:
&& (bool && bool)两边都为true则为true,其余情况为false; and
|| (bool || bool)两边随便一个为true则结果为true;
!(把bool值/变量 ! 取反,一般用于if)
利用类声明的变量,叫对象
类的数据需要加public才能访问
Enemy enemy1=new Enemy();
print(enemy1.name);
public string name;
类:
public class ABC:MomoBehaviour{
void Start(){
int hp=100;
Enemy enemy1= ew Enemy(); //声明类名,构造类
}
}
class Enemy{
string name;
int hp;
}//创建一个类
int add(int a,int b){
int res=a+b;
return res;
}
int res=add(10,20);
void CreaEnemy(Vector 3 pos){
print(“创建敌人的位置”+pos);
}
CreateEnemy(new Vector3(1,1,1));
枚举类型:多种类型的敌人
int i=0;//0魔法师,1战士,2巫师,3坦克……
类名外命名:
enum RoleType{
Mag,
Soldier,
Wizard
}
RoleType rt=RoleType.Mag;
rt=RoleType.
method:方法
返回值 方法名 (参数){方法体}
void Start(){
Test();//调用方法
}
定义方法
void Test(){
print(“Test被调用了”)
}
void CreatEnemy(){
}
for循环遍历数组
for(int i=0,i)
hps.L
for循环:创建多个敌人
更改数组内容:hp[1]=50;
数组声明的第二种方式:
new:构造的意思
int[] hps=new int[10];数组长度为10;num为0-9;
数组:存储一堆同样的数据,比如保存一堆敌人的血量;
int[]hp={100,20,80,90,30};数组长度为5
通过索引访问数据
数组名[索引的num],如果数组长度为5则,num最大为4。
IndexOutOfRangeException:Array index is out of range.索引输出异常数组索引超出范围
比较运算符返回结果是bool型,数字1 运算符 操作数
一般用于变量比较
bool res=hp>0;
hp+=10;让hp加10;
hp++;自增运算符,加1
两个整数相除,小数部分会被舍弃
多种数据类型运算,返回的结果类型是数据类型较大的
加法还可以连接字符串
bool:true 或 false,一般用于判断状态
char A='c';单字符
string name=”哈哈hhh“字符串,一般中文用string
print只能用于MonoBehavior中
Debug.Log均适用
Input.GetAxis("Horizontal");获得水平方向键的指令
Input.GetAxis("Vertical");获得竖直方向
help中有API手册,学会尝试。
在其他类中调用一个自己写的类中的量时,须在自己写的类中将该量设为public型。
long:整型
float hp=2.1f,要写f,2.1