Unity02

来源:互联网 发布:地基承载力的简易算法 编辑:程序博客网 时间:2024/06/17 12:10
/*
 * 1:脚本创建,生命周期
 * 2:Time类的使用,time类的使用,timescale,update,fixedUpdate,lateUpdate
 * 3:脚本查找,获取对象Find,FindWithTag,Transform.Find
 * 4:平移与旋转:注意space.World,Space.Self
 * rotate,rotatearound,translate
 * 5:向量加减,乘法,叉乘,点乘,求法向量,求向量的馍,求向量之间的夹脚

*/

代码:Demo2

public class Demo2 : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
        //Time.timeScale = 0;
        print ("Start");
    }
    
    // Update is called once per frame
    void Update () {
    
        print ("update");
//        transform.Rotate (0,0,1);
//        transform.Rotate (0,0,1,Space.World);

//        transform.Translate (0,0,1*Time.deltaTime,Space.World);
//        Debug.Log (this.GetType().Name);

//        Debug.Log (Time.deltaTime);//两祯之间的时间间隔

//        Debug.Log (Time.fixedDeltaTime);//固定的时间间隔,Edit->Projectseeting打开
//        Debug.Log (Time.time);//deltatime所经历的总时间
//        Debug.Log (Time.fixedTime);//fixeddeltaTime所经历的总时间
//        Debug.Log (Time.timeScale);//时间比例,=0表示暂停,祯间不处理数据,deltatime为0,FixedUpdate 函数不会被执行
                                   //=1表示正常,只影像fixUpdate,不影响update和lateUpdae
                                   //>1变快,<1变慢
    }

    //物理更新
    void FixedUpdate(){//固定在时间间隔执行,根据fixedDeltaTime
    
        Debug.Log ("fixUpdate");
    }

    //摄像机跟踪
    void LateUpdate(){//总是在update后执行
        print ("LateUpdate");
    }

    void Reset(){//还未运行就开始调用,在记载脚本到组件上悲执行
    
        print ("reset");
    }
    void OnEnable(){
    
        print ("enable");
    }
    void OnDisable(){
        print ("disable");
    }
    void OnDestroy(){
        print ("destroy");
    }
    void Awake(){//在start之前执行
        print ("awark");
    }

}
/*
 * 大体分为五个阶段
 * 编辑阶段Reset->开始阶段Awake->OnEnable-Start->
 * 迭代更新阶段FixedUpdate-Update-LateUpdate->
 * 显示阶段OnGUI->清除阶段OnDisable-OnDestroy
*/


代码:Demo3

public class Demo3 : MonoBehaviour {

    private GameObject obj;
    private GameObject[] games;
    private Transform tran;
    private Demo3[] ds;
    // Use this for initialization
    void Start () {
    
//        HelloWorld hello = this.GetComponent<HelloWorld> ();
//        hello.Say ();

//        GameObject obj = GameObject.Find ("Cube");//深度优先遍历,找栈中最后添加的GameObject
//        HelloWorld h= obj.AddComponent <HelloWorld>();
//        h.enabled = false;
//        h.Say ();

//         obj = GameObject.Find ("GameObject/Cube");//通过这种方式找到指定的GameObject
//        obj = GameObject.Find ("Cylinder");
//        obj = GameObject.FindGameObjectWithTag ("cubeflag");//深度优先遍历,找栈中最后添加的GameObject
//        games = GameObject.FindGameObjectsWithTag ("cubeflag");//查找所有为cubeflag标签的GameObject
//        print (games.Length);

        //当父级旋转运动时子级也会跟着运动,注意:父级的重心坐标
//        tran = this.transform.Find ("Cube");//在子级查找,不深度查找
//        tran = this.transform.FindChild ("Cube");//在子级查找,不深度查找
//        tran = this.transform.Find ("Cylinder");//Cylinder/Cube,cube会跟着Cylinder运动


        ds = GameObject.FindObjectsOfType<Demo3> ();//查询所有挂载了Demo3脚本的对象
        print (ds.Length);

        foreach (Demo3 g in ds) {
            print (g.gameObject.name);
        }

    }
    
    // Update is called once per frame
    void Update () {
    

//        foreach(GameObject g in games){
//            g.transform.Rotate (0,0,1);
//        }

//        obj.transform.Rotate (0,1,0);

//        foreach (Transform item in transform) {
//            item.Rotate (0,0,1);
//        }

//        tran.Rotate (0,1,0,Space.World);


    }
}



0 0
原创粉丝点击