Unity编程学习笔记(一)用脚本控制物体移动和旋转(GameObject类和Transform类)

3 篇文章 0 订阅

一、GameObject类的使用

   //Debug.Log(gameObject.name);//查找物体的名字

        // GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);//创建一个Cube

        // //给Cube指定一个父对象

        //transform.parent = cube.transform;

        // //把物体从父节点中移除

        // GameObject sh = GameObject.Find("Sphere");

        // sh.transform.SetParent(null);

        // Debug.Log("孩子节点数" + transform.childCount);

 

        //for (int i = 0; i < transform .childCount ; i++)

        //{

        //    Debug.Log(i + " " + transform.GetChild(i).name);

        //}

 

 

 ///查找游戏对象的位置,根据名字查找

        //GameObject c = GameObject.Find("Cube");

        //Debug.Log(c.transform.position);

        //GameObject A = GameObject.Find("Sphere");

        //Debug.Log(A.transform.position);

        ///找找一组的游戏对象

        //GameObject b = GameObject.FindWithTag("zz");

        //GameObject bj = GameObject.FindGameObjectWithTag("zz");

        //GameObject[] obj = GameObject.FindGameObjectsWithTag("zz");

        //for (int i = 0; i < obj.Length ; i++)

        //{

        //    Debug.Log(obj[i].transform.position);

        //}

        ///设置游戏对象的Tag值

        ///

        //GameObject cylinder = GameObject.Find("Cylinder");

        //cylinder.tag = "zz";

        ///将游戏对象失活(隐藏)

        //cylinder.SetActive(false);

        //cylinder.Destroy(cylinder,3);

        ///克隆游戏对象

        GameObject spheres = GameObject.Instantiate(Spheresprefabs);

        spheres.name = "Spheres";

 

        GameObject spheres1 = GameObject.Instantiate(Spheresprefabs, new Vector3(2, 2, 2), Quaternion.identity) as GameObject;

        spheres1.name = "zz";

 GameObject spheres2 = GameObject.Instantiate<GameObject>(Spheresprefabs);

        spheres2.transform.position = new Vector3(3, 3, 3);

        spheres2.name = "zz";

二、Transform类的使用

//GameObject obj = GameObject.Find("Sphere ()5");

        //obj.transform.SetAsFirstSibling();

        ///把某个物体向前移

        //Transform trans = transform.GetChild(5);

        //int index = trans.GetSiblingIndex();

        //trans.SetSiblingIndex(3);

三、GameObject类和Transform类查找的区别

///查找子物体,能找到是活的物体

        Transform child1 = transform.Find("Sphere");

        Debug.Log(child1.name);

        //查找整个面板,不能到失活的对象

        GameObject child = GameObject.Find("Sphere");

        Debug.Log(child.name);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值