您的位置:首页 > 移动开发 > Unity3D

unity学习之总结

2014-12-22 22:44 337 查看
常用的必然事件

Void  Awake( ){ };   void  Start( ){ };   void Update( ){  }

Void  FixedUpdate( ){  };  void  lateUpdate(  ){  };

Void  OnGUI(   ){   }

Transform

tranform  改变游戏对象的位移,旋转和伸缩。

改变位移有2中方法

例如:向前移动

第一种:

transform.Translate(Vector .forward);

第二种:

Transform.Translate(new Vector(0,0,1));

旋转(围绕z轴旋转)

transform.Rotate(new Vector(0,0,1));

伸缩(沿z轴伸缩)

transform+=new Vector(0,0,1);

Input 

按方向上键

if(Input.GetKey(“up ”)){

}

按A键

if(Input.GetKey(KeyCode.A)){

}

按下和松开

if(Input.GetKeyDown(KeyCode.S)){

}

if(Input .GetKeyUp(KeyCode.S)){

}

按上下左右键后WSAD键

transform.transform(Input.GetAxis(“Horizontal”) ,0,Input.GetAxis(“Vertical”))

鼠标

按下鼠标左键

if(GetMouseButton(0)){

}

按下鼠标右键

if(GetMouseButton(1)){

}

按下鼠标中键

if(GetMouseButton(2)){

}

按下鼠标左键和 松开鼠标右键

GetMouseButtonDown(0)和GetMouseButtonUp(1)

自定义按钮

if(Input.GetBottom(“Fire1”)){

}

“Fire1”代表鼠标左键

可以在unity软件里面看Edit-->Project-->Input-->Axes

给物体加一个力

GameObject a;

a.Rigidbody.AddForce(0,0,100);

GameObject

创建游戏对象

GameObject a=new GameObjectPrimitive(PrimitiveType.Cube);

命名

a.name=”cube”;

添加颜色

a.render.material.color=Color.blue;

查找

第一种:通过名字找

GameObject a=GameObject.Find(“cube”);

第二种:通过标签tag来寻找

GameObject a=GameObject.FindWithTag(“Player”);

寻找多个对象

GameObject[] a=GameObject.FindGameObjectWithTag(“Player”);

克隆

GameObject go=GameObject.instantiate(cube,transform.position,Quaternion .identity)

Cube要克隆的对象

Transform.position所放的位置

Quaternion .identity是否旋转

销毁游戏对象

Destory(a,1);

a 代表游戏对象,   1 代表时间

Time

一般用到的属相是deltaTime,主要用来倒计时

Float a=5.0f;

Void  Update(){

a-=Time.detaTime;

if(a>=0){

print(a);

}

}

协同程序(Coroutine)和Yield

游戏对象从一种运行到另一种运行

Void  Update(){

startCoroutine (“Do”);

}

IEnumerator Do(){

yield return new WaitForSeconds(3.0f);

transform.Tronslate(Vector.forward);

}

发送信息

向自身或同级别发送

Sphere:

GameObject a;

Void  Start(){

a.SendMessage(“Do”,”ssssssss”);

}

Cube:

Void  Do(string message){

print (message);



向子类传递信息

a.BroadcastMessage(“Do”,”ffffffffffff”);

向父类传递信息

a.SendMessageUpwards(“Do”,”uuuuu”);

随机数Random

4个不重复的数

Random(0,100)包括最小值不报括最大值只有在最小值等于最大值的时候包括最大值。

Random(0.0f,1.0f)包括最小值但不包括最大值

int a=Random.Range(0,9);

int b=Random.Range(0,9);

int c=Random.Range(0,9);

int d=Random.Range(0,9);

if(a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d){

print (a+”  ”+b+”  “+c+”  ”+d);

}

Test2访问Test1的脚本

Test1

{

public int  x=3;

}

Void  Start(){

Test1 a=GameObject.GetComponent<Test1>();

print (a.x);

}

碰撞检测

检测碰撞发生的方式:

1、利用碰撞器

2、利用触发器

触发器

Void  OnTriggerEnter(Collider  x){

判断游戏对象的方法有2种,一种是通过名字判断

if(x.gameobject.name==”Cube”){

}

一种是通过标签tag来判断

if(x.tag==”Player”){

}

}

碰撞器

Void  OnCollisionEnter(Collision  a){

}

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息