日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Unity3D開發之獲取所有的子對象的方法詳解_C#教程

作者:恬靜的小魔龍 ? 更新時間: 2023-03-28 編程語言

一、前言

這個問題還是比較簡單的,無非就是一個for循環就可以全部獲取到了,但是我喜歡簡單直達,有沒有直接就能獲取到所有的子對象函數呢,搜了好久都沒有,所以我準備寫一個擴展函數,來自己補充這個函數,一起來看一下吧。

二、如何獲取所有子對象

第一種方法

使用foreach循環,找到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;
        }
    }
}

這樣就將所有的子對象保存了下來。

三、使用擴展方法獲取所有子對象

總感覺獲取個子對象還要用for循環有點麻煩,那么咱們就可以寫一個擴展方法,直接獲取到所有的子對象

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

這有兩個函數,一個是獲取所有子對象的List集合,一個是獲取所有子對象的數組集合,按需使用。

擴展方法的使用可以參考文末補充內容

3、使用擴展方法

使用m_ParObj.GetChild()就可以調用擴展方法:

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

這樣就可以通過一個函數就可以獲取到所有的子對象了。

知識補充

Unity3D日常開發之擴展方法的使用

在程序開發中,可能會遇到現有類型的方法中沒有我們想要的方法,這時候就可以使用擴展方法給已有類型添加新的方法,而無需創建新的派生類、重新編譯或者其他方式修改原始類型的代碼。

擴展方法需要定義成靜態方法,通過實例方法語法進行調用,參數類型就是制定方法作用于哪個類型,該參數使用this修飾符為前綴

為System.String類添加擴展方法

下面的示例演示為 System.String 類定義的一個擴展方法。 請注意,它是在非嵌套的、非泛型靜態類內部定義的:

public static class MyExtensions
{
    public static int ReturnWordCount(this string str)
    {
        return str.Split(new char[] { ' ', '.', '?' }, System.StringSplitOptions.RemoveEmptyEntries).Length;
    }
}

調用該擴展方法:

using UnityEngine;

public class Test_Extend : MonoBehaviour
{
    void Start()
    {
        string str = "Hello Extension Methods";
        int count = str.ReturnWordCount();
        Debug.Log(count);
    }
}

編譯結果:

為UnityEngine.GameObject類添加擴展方法

為游戲對象一次添加兩個組件

public static class MyExtensions
{
    public static void AddBRComponent(this GameObject obj)
    {
        obj.AddComponent<BoxCollider>();
        obj.AddComponent<Rigidbody>();
    }
}

調用該擴展方法:

using UnityEngine;

public class Test_Extend : MonoBehaviour
{
    void Start()
    {
        GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        obj.AddBRComponent();
    }
}

原文鏈接:https://itmonon.blog.csdn.net/article/details/110849582

欄目分類
最近更新