日志输出方法:
Debug.Log()以普通信息方式输出
Debug.LogWarning()以警告信息方式输出
Debug.LogError()以错误信息方式输出
日志输出方法:
Debug.Log()以普通信息方式输出
Debug.LogWarning()以警告信息方式输出
Debug.LogError()以错误信息方式输出
foreach(Transform t in children)
{
if(t != transform)
Destroy(t.gameobject);
}
1.利用循环销毁
void Start(){
Transform [] children = transform.GetComponentsInChildren<Transform>();
//第一种
for(int i=0;i<children.Length;i++)
{
if(children[i]!=transform)
GameObject.Destory(children[i].gameobject);
}
//第二种
int i=0;
while(i<children.Length)
{
if(children[i]!=transform)
GameObject.Destory(children[i].gameobject);
}
//第三种
int i=0;
do
{
if(children[i]!=transform)
GameObject.Destory(children[i].gameobject);
}
i++;
}
while(i<children.Length);
}
方法中的
访问对象的名字的话
在class类里的string必须是public string
Enemy enemy1 = new Enemy();
print(enemy1.name)这样输出是null
没有赋初值的时候string类型输出Null
而像血量这样的数字,没有赋初值得到的值是0
类的定义,在Enemy这个类型里有两个数据
class Enemy{
string name;
int hp;
}
这个类型的调用
Enemy enemy1 = new Enemy();
创建一个关于敌人的类
参数可以在方法里改变单一的量,传递数据
定义函数的方法是和C一样的
void的意思可以理解为没有返回值的函数
有返回值的话只能有一个返回值,这里和C语言是一致的
枚举类型enum RoleType{
Mag,
Soldier,
Wizard}
赋值的时候用
RoleType rt = RoleType.Mag
方法的定义
void Test(){
}
调用直接用Test()
比较像C里的函数
数组名.Length可以代表数组长度
字符串后面+数字 相当于拼接
空数组是没有办法访问的
int[] hps = new int[10];
new是构造的意思
其中里面的值都为0
null为空值
int[] hps = new int[5] {1,2,3
数组的声明和C语言不同
int[] hps = {100,20,80,90,30};
访问和C是一样的 数组名[索引]
3+ 3.2f 会根据数据类型比较大的float
加好在字符串里相当于拼接
bool类型,值只有true和false两种
比如判断角色是否死亡
小数写成float类型要在小数后面跟上f
C#里没有自动把浮点转换为int
比如int hp;
hp=1.1 是错误的,不能运行的。
但是在C语言里是可以运行的。
全部注释 ctrl+k ctrl+c
print可以输出数字和字符串等 只能在MonoBehaviour里面使用
Debug.Log(1)
Debug.Log("234")也是可以的,而且在任何地方都可以使用
Debug.LogWarning是警告输出,在Console里有警醒的作用
Debug.LogError() 错误,显示
//单行注释
/*
*/