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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM