unity3D的基本造作和总结

来源:互联网 发布:手机淘宝女款鞋子 编辑:程序博客网 时间:2024/05/08 18:10
1、创建三个正方体,点击鼠标逐个消失

a、public class DestoryCube : MonoBehaviour {
 
     public GameObject[] cube;   //创建一个数组的cube
 
     int i = 0;     //数组的下标是从零开始的
 
     float fireTime = 0.5f;//发射时间
 
     float nextTime = 0.0f;//间隔时间
 
     Update is called once per frame
 
     void Update ()
 
    {
 
       if (Input.GetButton("Fire1") && Time.time>nextTime){
 
       nextTime = fireTime + Time.time;
 
       Destroy(cube[i]);
 
       i++;
 
       print(i);
 
    }
 
b、if (Input.GetButtonDown("Fire1"))
 
   {
 
        Destroy(cube[i]);
 
        i++;
 
    }
 
2、克隆子弹来发射子弹

 
   public class Fire : MonoBehaviour {
 
   public GameObject bullet;  //这是在游戏对象中创建一个bullet
 
   public float fireTime = 0.5f;//发射时间
 
   float nextTime = 0.0f;//间隔时间
 
   void Update () {
 
   if(Input.GetButton("Fire1") && Time.time>nextTime){
 
   nextTime = fireTime + Time.time;
 
   GameObject.Instantiate(bullet);
 
   GameObject go= GameObject.Instantiate(bullet,new Vector3(0,3,0),Quaternion.identity) as
 
   GameObject;
 
   go.rigidbody.AddForce(0,0,1000);
 
   }
 
   if (Input.GetButtonDown("Fire1"))
 
  {
 
    GameObject.Instantiate(bullet);
 
  }




1、键盘判断  
   
       GetKey:当通过名称指定的按键被用户按住时返回true
       GetKeyDown:当用户按下指定名称的按键时的那一帧返回true。
       GetKeyUp:在用户释放给定名字的按键的那一帧返回true。
       GetAxis(“Horizontal")和GetAxis(“Verical”) :用方向键或WASD键来模拟-1到1的平滑输入 
       代码:
       if (Input.GetKey(“up”)) {
           print("up arrow key is held down"); 
       }
       if (Input.GetKey(“down”)) {
           print("down arrow key is held down");
       }
       If(Input.GetKeyDown(KeyCode.Escape)){
            print(“按下Esc键”);
       }
       If(Input.GetKeyDown(KeyCode.A)){
            print(“按下A键”);
       }
       If(Input.GetKeyUp(KeyCode.Escape)){
           print(“松开Esc键”);
       }
       If(Input.GetKeyUp(KeyCode.D)){
           print(“松开D键”);
       }
       GetAxis是float型,不能用if判断
       print (Input .GetAxis ("Horizontal"));
       print (Input.GetAxis ("Vertical"));

2、鼠标判断

       GetButton:根据按钮名称返回true当对应的虚拟按钮被按住时。 
       GetButtonDown:在给定名称的虚拟按钮被按下的那一帧返回true。
       GetButtonUp:在用户释放指定名称的虚拟按钮时返回true。
       if(Input.GetButton("Fire1")){
              print(“按下鼠标左键”);
       }
       if (Input.GetMouseButton(0)) {
             Debug.Log("按下鼠标左键");
       }
       if (Input.GetMouseButton(1)) {
             Debug.Log("按下鼠标右键"); 
       }
       if (Input.GetMouseButton(2)) {
             Debug.Log("按下鼠标中键");
       }

3、项目工程分层设计

       Scenes——  存放场景
       Scripts——  存放脚本
       Resources——  资源包
       Textures——  贴图 
       Materials——  材质(主要为.mat文件)
       Audios——  声音
       Models——  模型(存放FBX等文件)

4、Mono Develop脚本编辑器介绍

       (Unity 自带脚本编辑器)
  a、Unity3d目前支持三种语言的脚本程序,包括c#,JavaScript,boo;其中使用最多为c#。
       注意:
       (1)项目运行过程中的修改不会保存
       (2)脚本只能依附于游戏对象或由其他脚本调用才能运行
       (3)一个脚本可以放在多个游戏对象上面,是多个实例
       (4)一个脚本的多个实例和其他脚本之间互不干扰
  b、初始化不要放在构造函数中,而是放在start()方法中。
  c、放到游戏对象中的脚本(实例化)可以在Inspector中修改属性,gett写法属性不被识别,直接public字段就能当属性用,不写修饰符就是私有private

5、创建、克隆、销毁游戏对象

       程序(动态)创建游戏对象 
       eg: 
       GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 
       cube.name = "cube1";//设定名字 
       cube.renderer.material.color = Color.red; 
      程序(动态)克隆对象 
       eg:
       GameObject obj = (GameObject)Instantiate(cube); 
       程序(动态)销毁对象 
       eg: 
       Destroy(obj,1); 
      特点:
  a、动态创建出来的对象运行时在Hierarchy中可以看到。
  b、克隆游戏对象产生复制对象的效率高,调用instantiate来生成对象,Destroy(obj)是立即销毁游戏对象。
  c、动态添加脚本的方法:只要把脚本名称传给AddComponen即可

6、给游戏对象添加名称、刚体、碰撞器。

  a、给游戏物体添加名为FoobarScript的脚本
       GameObject.AddComponent ("FoobarScript");
  b、给游戏物体添加刚体
       Rigidbody  rb = GameObject.AddComponent ("Rigidbody"); 
  c、给游戏物体添加球体碰撞器
       SphereCollider  sc = GameObject.AddComponent ("SphereCollider");

7、常用核心类

  (a)通过名称来查找:
       GameObject  player = new GameObject("Player");
       GameObject  go=GameObject.Find(“Player”);
  (b)通过tag标签获取单个游戏对象:
       GameObject  go=GameObject.FindWithTag(“Player”);
       GameObject go=GameObject..FindGameObjectWithTag (“Player”);
  (c)通过游戏标签获取多组游戏对象:
       GameObject[]  go=GameObject.FindGameObjectsWithTag (“Player”);

8、Time类

       属性: deltaTime 
       说明: 
       方法Update(): 受当前渲染效率的影响,有时快有时慢,帧率会变化,Update被调用的时间间隔就发生变化,Time. deltaTime获得自上次Update一来经历的时间,只应该在Update()中使用deltaTime ,OnGUI、FiexedUpdate等里面不要用。
       按下A键,游戏物体向左移动;按下D键,游戏物体向右移动                  
       float move_h=0;
       if (Input.GetKey (KeyCode.A)) {
             move_h-=Time.deltaTime; 
       } 
       if (Input.GetKey (KeyCode.D)) {
             move_h+=Time.deltaTime;
       }
       transform.Translate(new Vector3(move_h,0,move_v));

9、Random类(随机数)

       float a=Random.value; 
       int b=Random.Range(0,100)    包括最大和最小 
       float c=Random.Range(0.0f,1.0f);   包括最小但不包括最大

10、Yield

       Yield语句是一种特殊类型的Return(返回)语句,它可以确保函数在下一次被执行时,不是从头开始,而是从Yield语句处开始。
       可以实现将一段程序延迟执行或者将其各个部分分布在一个时间段内连续执行。
       eg:
       void Start () {
           print("Start开始");
           StartCoroutine(Do());
           print("Do调用后");
       }
       IEnumerator Do()
       {
           print("Do开始");
           yield return  new WaitForSeconds(3.0f);
           print("暂停3秒后");
       }

11、消息传递函数

       Message相关有3条指令:
       SendMessage ("函数名",参数,SendMessageOptions) //GameObject自身的Script
       BroadcastMessage ("函数名",参数,SendMessageOptions)  //自身和子Object的Script
       SendMessageUpwards ("函数名",参数,SendMessageOptions)  //自身和父Object的Script



更多精彩请关注:http://www.gopedu.com/

关注我的博客会有不一样的惊喜啊!

博客:http://unity.gopedu.com/home.php?mod=space&do=blog&view=me
0 0
原创粉丝点击