Unity学习笔记(2)

日志输出:1.在代码中用Print(1)输出即可需继承MonoBehaver
2.Debug.Log(1),Debug.LogWarning(),Debug.LogError();任意地方都可使用


数据和数据类型
http://www.cnblogs.com/tonney/archive/2011/03/18/1987577.html


运算符
https://wenku.baidu.com/view/93c32317a76e58fafab00341.html


if语句
if(boolean_expression)
{
   
}
if else 语句
if(boolean_expression)
{
   
}
else
{
  
}
数组
声明方式:1.int [] hps={1,2,3};
2.int [] hps2= new int[10];
例如:int [] hps=null;print(hps)--->null
     int [] hps2={ };print(hps2)--->int32(数据类型) 
3.int [] hps3=new int[3]{1,2,3};


枚举类型(可以直接知道哪种类型,定义一些和类型有关的东西,敌人类型,职业类型):
enum RoleType{
Mag,//魔法师
Soldier,//战士
Wizard//巫师


}
RoleType rt = RoleType.mag;
rt = RoleType.Soldier;




参数的作用:给方法传递一些数据,方法可以对数据做相应的处理.


for循环
while循环
方法 void test(){};   int Test1(int a, int b){ return a+b};
类的定义:class Enemy{
string name;
int hp;
}
Enemy enemy1(这是类的声明)= new Enemy()(这是类的创建);


类中属性和方法的调用
Input.GetAxis("Horizontal")//unity可以得到水平按键.


UnityAPI手册:help-->Scripting Reference!




Switch语句:
switch (i){
case 1: .... ; break;
case 2: .... ; break;
case 3: .... ; break;
default: .... ; break;
}


得到子物体:  Transform:游戏物体
Transform[] children  =transform.GetComponentsInChildren<Transform>();


销毁游戏物体:GameObject.Destroy(children[i].gameObject);


while循环执行的次数>=0
do while循环执行的次数>=1


foreach循环:
foreach(Transform t in children){
    print(t);
}
2.18 29.1 -12.5
组件的获取: GetComponent方法(不管组件是否被激活,都能得到)
获取Transform组件:Transform t = GetComponent<Transform>();
获取Collider组件:collider[] c=GetComponents<Collider>();


组件的禁用和激活:先得到组件,在设置enabled属性.
BoxCollider collider = GetComponent<BoxCollider>();
collider.enable=false;


获取游戏物体的四种方式:
1.拖拽的方式:  public GameObject camerMain; 然后在Unity中拖拽
2.transform.Find方法(查找子物体):transform.Find("GameObject(1)/GameObject");-----获取GameObject(1)下的GameObject
3.GameObject GO=GameObject.Find("名字")----名字可以重复,有多个只会返回第一个,不推荐使用,在整个场景挨个查找,遍历范围大,耗费性能;
4.根据标签查找:
Gameobject player=GameObject.FindWithTag("Player");

猜你喜欢

转载自blog.csdn.net/qq_38411133/article/details/80612964