unity3D培訓實戰:獲取游戲對象幾種方法如下
來源:
奇酷教育 發表于:
對于前幾篇對unity3D的介紹,包括什么是unity3D?學習unity3D培訓能做什么?unity3D界面操作等。今天咱們來說說unity3D培訓實戰:獲取
對于前幾篇對
unity3D的介紹,包括什么是unity3D?
學習unity3D培訓能做什么?
unity3D界面操作等。今天咱們來說說unity3D培訓實戰:獲取游戲對象幾種方法。
一:通過對象名稱(Find方法)
二:通過標簽獲取單個游戲對象(FindWithTag方法)
三:通過標簽獲取多個游戲對象(FindGameObjectsWithTags方法)
四:通過類型獲取單個游戲對象(FindObjectOfType方法)
五:通過類型獲取多個游戲對象(FindObjectsOfType方法)
下面以Find方法為例,為大家演示下。
static GameObject Find (string name)
傳入的name可以是單個的對象的名字,也可以是hierarchy中的一個路徑名,如果找到會返回該對象(活動的),如果找不到就返回null。
[csharp] view plain copy
var cubeF = GameObject.Find("/CubeFather");
if (null != cubeF)
{
Debug.Log("find cube father~");
}
cubeF = GameObject.Find("CubeFather");
if (null != cubeF)
{
Debug.Log("find cube father, no /~");
}
var cubeS = GameObject.Find("/CubeFather/CubeSon");
if (null != cubeS)
{
Debug.Log("find cube son~");
}
cubeS = GameObject.Find("CubeFather/CubeSon");
if (null != cubeS)
{
Debug.Log("find cube son, no /~");
}
cubeS = GameObject.Find("CubeSon");
if (null != cubeS)
{
Debug.Log("find cube son, no one /~");
}
以上就是
奇酷小編為大家講解的unity3D培訓實戰:獲取游戲對象幾種方法,想深入了解請聯系我們。