Unity3D開發(fā)之獲取所有的子對象的方法詳解
一、前言
這個問題還是比較簡單的,無非就是一個for循環(huán)就可以全部獲取到了,但是我喜歡簡單直達(dá),有沒有直接就能獲取到所有的子對象函數(shù)呢,搜了好久都沒有,所以我準(zhǔn)備寫一個擴(kuò)展函數(shù),來自己補(bǔ)充這個函數(shù),一起來看一下吧。
二、如何獲取所有子對象
第一種方法
使用foreach循環(huán),找到transform下所有的子物體
foreach(Transform child in transform)
{
Debug.Log(child.gameObject.name);
}
比如說,我有一個父物體:m_ParObj,我如何獲取到所有的子對象呢:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SplitTest : MonoBehaviour
{
public GameObject m_ParObj;
private void Start()
{
List<GameObject> m_Child = new List<GameObject>();
foreach (Transform child in m_ParObj.transform)
{
//Debug.Log(child.gameObject.name);
m_Child.Add(child.gameObject);
}
}
}
這樣就將所有的子對象保存了下來。
第二種方法
通過transform.GetChild(i)來獲取到所有的子對象:
for (int i = 0; i < transform.childCount; i++)
{
Debug.Log(transform.GetChild(i).name);
}
比如說,我有一個父物體:m_ParObj,我如何獲取到所有的子對象呢:
using UnityEngine;
public class SplitTest : MonoBehaviour
{
public GameObject m_ParObj;
private void Start()
{
GameObject[] m_Child = new GameObject[m_ParObj.transform.childCount];
for (int i = 0; i < m_Child.Length; i++)
{
m_Child[i] = m_ParObj.transform.GetChild(i).gameObject;
}
}
}
這樣就將所有的子對象保存了下來。
三、使用擴(kuò)展方法獲取所有子對象
總感覺獲取個子對象還要用for循環(huán)有點(diǎn)麻煩,那么咱們就可以寫一個擴(kuò)展方法,直接獲取到所有的子對象
1、首先新建一個MyExtensions.cs腳本
using System.Collections.Generic;
using UnityEngine;
public static class MyExtensions
{
}
2、編寫腳本
using System.Collections.Generic;
using UnityEngine;
public static class MyExtensions
{
public static List<GameObject> GetChild(this GameObject obj)
{
List<GameObject> tempArrayobj = new List<GameObject>();
foreach (Transform child in obj.transform)
{
tempArrayobj.Add(child.gameObject);
}
return tempArrayobj;
}
public static GameObject[] GetChildArray(this GameObject obj)
{
GameObject[] tempArrayobj = new GameObject[obj.transform.childCount];
for (int i = 0; i < obj.transform.childCount; i++)
{
tempArrayobj[i] = obj.transform.GetChild(i).gameObject;
}
return tempArrayobj;
}
}
這有兩個函數(shù),一個是獲取所有子對象的List集合,一個是獲取所有子對象的數(shù)組集合,按需使用。
擴(kuò)展方法的使用可以參考文末補(bǔ)充內(nèi)容
3、使用擴(kuò)展方法
使用m_ParObj.GetChild()就可以調(diào)用擴(kuò)展方法:
using System.Collections.Generic;
using UnityEngine;
public class SplitTest : MonoBehaviour
{
public GameObject m_ParObj;
private void Start()
{
List<GameObject> m_Child = m_ParObj.GetChild();
for (int i = 0; i < m_Child.Count; i++)
{
Debug.Log(m_Child[i].gameObject.name);
}
}
}
這樣就可以通過一個函數(shù)就可以獲取到所有的子對象了。
知識補(bǔ)充
Unity3D日常開發(fā)之?dāng)U展方法的使用
在程序開發(fā)中,可能會遇到現(xiàn)有類型的方法中沒有我們想要的方法,這時候就可以使用擴(kuò)展方法給已有類型添加新的方法,而無需創(chuàng)建新的派生類、重新編譯或者其他方式修改原始類型的代碼。
擴(kuò)展方法需要定義成靜態(tài)方法,通過實(shí)例方法語法進(jìn)行調(diào)用,參數(shù)類型就是制定方法作用于哪個類型,該參數(shù)使用this修飾符為前綴
為System.String類添加擴(kuò)展方法
下面的示例演示為 System.String 類定義的一個擴(kuò)展方法。 請注意,它是在非嵌套的、非泛型靜態(tài)類內(nèi)部定義的:
public static class MyExtensions
{
public static int ReturnWordCount(this string str)
{
return str.Split(new char[] { ' ', '.', '?' }, System.StringSplitOptions.RemoveEmptyEntries).Length;
}
}
調(diào)用該擴(kuò)展方法:
using UnityEngine;
public class Test_Extend : MonoBehaviour
{
void Start()
{
string str = "Hello Extension Methods";
int count = str.ReturnWordCount();
Debug.Log(count);
}
}
編譯結(jié)果:

為UnityEngine.GameObject類添加擴(kuò)展方法
為游戲?qū)ο笠淮翁砑觾蓚€組件
public static class MyExtensions
{
public static void AddBRComponent(this GameObject obj)
{
obj.AddComponent<BoxCollider>();
obj.AddComponent<Rigidbody>();
}
}
調(diào)用該擴(kuò)展方法:
using UnityEngine;
public class Test_Extend : MonoBehaviour
{
void Start()
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.AddBRComponent();
}
}
到此這篇關(guān)于Unity3D開發(fā)之獲取所有的子對象的方法詳解的文章就介紹到這了,更多相關(guān)Unity獲取子對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件的示例代碼
標(biāo)簽PDF文件包含描述文檔結(jié)構(gòu)和各種文檔元素順序的元數(shù)據(jù),是一種包含后端提供的可訪問標(biāo)記,管理閱讀順序和文檔內(nèi)容表示的邏輯結(jié)構(gòu)的PDF文件。本文將用C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件,需要的可以參考一下2022-08-08
C#使用TensorFlow.NET訓(xùn)練自己的數(shù)據(jù)集的方法
這篇文章主要介紹了C#使用TensorFlow.NET訓(xùn)練自己的數(shù)據(jù)集的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解
List和Dictionary想必是我們平常用到最多的C#容器了,他們使用起來都很簡單,這篇文章主要給大家介紹了關(guān)于C#中Dictionary與List的用法區(qū)別以及聯(lián)系的相關(guān)資料,需要的朋友可以參考下2023-11-11
C# SynchronizationContext以及Send和Post使用解讀
這篇文章主要介紹了C# SynchronizationContext以及Send和Post使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
在C# WPF下自定義滾動條ScrollViewer樣式的操作
這篇文章主要介紹了在C# WPF下自定義滾動條ScrollViewer樣式的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

