Unity3D如何获取对象和子对象

在Unity3d中获取游戏对象有三种方法:

一:获取对象

1.通过对象名称获取:objCube=GameObject.Find("Cube"); 

private var objCube:GameObject;
private var isCubeRoate=false;

function Start () {
  objCube=GameObject.Find("Cube");
}

function Update(){
  if(isCubeRoate){
     objCube.transform.Rotate(0.0f,Time.deltaTime*200,0.0f);
  }
}

function OnGUI(){
  if(GUILayout.Button("旋转",GUILayout.Height(50))){
     isCubeRoate=true;
  }
}

2.通过tag标签获取单个游戏对象:objCube=GameObject.FindWithTag("Finish");

3.通过游戏标签获取多组游戏对象:objCube=GameObject.FindGameObjectsWithTag("Finish");

 

二:子对象

//获取所有子对象
foreach (Transform child in transform)
{
    Debug.Log(child.gameObject.name);
}
  
 //销毁所有子对象
foreach(Transform child in transform){
    Destroy(child.gameObject);
}
posted @ 2014-03-17 18:04  Tonge  阅读(24657)  评论(0编辑  收藏  举报