[Unity3D] 01 - Try Unity3D

01. Move and Rotate 

标准全局坐标系


 

Keyboard

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKey(KeyCode.W)){
            this.transform.Translate(Vector3.up, Space.World);
            
        }else if(Input.GetKey(KeyCode.S)){
            this.transform.Translate(Vector3.down, Space.World);
            
        }else if(Input.GetKey(KeyCode.A)){
            this.transform.Translate(Vector3.left, Space.World);
            
        }else if(Input.GetKey(KeyCode.D)){
            this.transform.Translate(Vector3.right, Space.World);
            
        }else if(Input.GetKey(KeyCode.F)){
            this.transform.Rotate(Vector3.right, Space.Self); // 左手坐标系
            
        }else if(Input.GetKeyDown(KeyCode.F)){
            this.transform.Rotate(Vector3.right, Space.Self); // 左手坐标系
            
        }
    }
}

 

Mouse

if(Input.GetMouseButton(1)){  // 0:left button; 1: right button
    this.transform.Rotate(Vector3.right, Space.Self);

 

RotateAround

public Transform TranRotateObj;

this.transform.RotateAround(TranRotateObj.position,Vector3.up, 1F);

围绕的目标:TranRotateObj 可以通过IDE编辑。

 

 

02. terrain and rendering

Easy and simple, but enough for me.


 

View

 

 

03. create GameObject

Game Objects are added dynamically.


 

动态生成克隆对象

using UnityEngine;
using System.Collections;

public class Demo3 : MonoBehaviour {

    private GameObject go=null; // Global value
    GameObject goClone=null;

    void Start () 
    {
        //创建Cube
        go=GameObject.CreatePrimitive(PrimitiveType.Cube);
        //渲染为红色。
        go.renderer.material.color=Color.red;

    }//Start_end
    
    void Update () 
    {
        //学习克隆(拷贝)
        if(Input.GetKeyDown(KeyCode.C))
        {
            goClone=(GameObject)Instantiate(go);
            //改变克隆体的位置
            goClone.transform.position=goClone.transform.position+new Vector3(2F,0F,0F);
        }

        //学习销毁
        if(Input.GetKeyDown(KeyCode.D))
        {
            Destroy(goClone);
        }


    }//Update_end

}//Class_end

 

 

动态导入其他脚本 - 调用其他脚本内的方法

  1. 自动调用start(),update().

using UnityEngine;
using System.Collections;

public class Demo4 : MonoBehaviour {

    public GameObject goObj;           //需要进行赋值的游戏对象
  
    void Start () 
    {
    
    }//Start_end
    
    void Update () 
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            //动态加载脚本。
            goObj.AddComponent("RotateSelf");
        }

        //动态销毁游戏对象所属的脚本。
        if(Input.GetKey(KeyCode.D))
        {
            Destroy(goObj.GetComponent("RotateSelf"));
        }
    }//Update_end

}//Class_end

 

2. 定时调用本类中的方法 - 回调函数

using UnityEngine;
using System.Collections;

public class Demo7 : MonoBehaviour {


    void Start () 
    {
        //回调函数
        //Invoke("DisplayN",5F);  //5秒后执行“DisplayNum”  方法
        InvokeRepeating("RepeatingDisplay",2F,0.5F);  //第一个参数表示启动时间,第二个参数表示间隔时间。

    }//Start_end

    void DisplayN()
    {
        Debug.Log("显示数字  22 222222。。。。");
    }

    void RepeatingDisplay()
    {
        Debug.Log("我反复被执行, 你相信吗? ");
    }

    void Update () 
    {
    
    }//Update_end

}//Class_end

 

3. 调用其他类中的变量

using UnityEngine;
using System.Collections;

public class Demo6 : MonoBehaviour {

    public GameObject goObj;       //需要操作的对象

    void Start () 
    {
        int intTempNum= goObj.GetComponent<Demo5>().IntNum;  // goObj上的Demo5,那么至少要实例化Demo5,并挂在Demo6上(Go Obj: Demo6)
        Debug.Log("[Demo6.cs]从Demo5.cs 文件中得到的数值: "+intTempNum);
    }//Start_end
    
    void Update () 
    {
    
    }//Update_end

}//Class_end

 

posted @ 2015-02-09 22:32  郝壹贰叁  阅读(255)  评论(0编辑  收藏  举报