Unity3D:查找子物体及子物体上的组件

大腿Plus 2018年9月17日11:58:16Unity3D之项目开发评论1,790阅读模式

在开发过程中用到最多的就是查找子物体或者子物体上的组件了,由于不能一一将组件或者子物体拖到脚本上。所以就需要查找方法,Unity的Transform提供的方法只能查找一级下的子物体。座椅就有了今天我说的方法。文章源自大腿Plus-https://www.shijunzh.com/archives/1002

主要用到了递归方法,虽然这个方法很耗时,但是目前也没有什么方法可以代替这个方法,下面就直接上代码。文章源自大腿Plus-https://www.shijunzh.com/archives/1002

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public static class Utilities
{

    public static T GetChild<T>(Transform parent, string childName, UnityEngine.Events.UnityAction BtnClickHandle = null) where T : Component
    {
        T t = null;

        if (parent.Find(childName) == null)
        {
            for (int i = 0; i < parent.childCount; i++)
            {
                t = GetChild<T>(parent.GetChild(i), childName);
                if (t != null)
                    break;
            }
        }
        else
        {

            GameObject obj = parent.Find(childName).gameObject;
            t = obj.GetComponent<T>();
        }
        if (t != null && t is Button && BtnClickHandle != null)
        {
            (t as Button).onClick.RemoveListener(BtnClickHandle);
            (t as Button).onClick.AddListener(BtnClickHandle);
        }
        return t;
    }

    public static T GetChild<T>(GameObject parent, string childName, UnityEngine.Events.UnityAction BtnClickHandle = null) where T : Component
    {
        T t = null;

        if (parent.transform.Find(childName) == null)
        {
            for (int i = 0; i< parent.transform.childCount; i++)
            {
                t = GetChild<T>(parent.transform.GetChild(i), childName);
                if (t != null)
                    break;
            }
        }
        else
        {

            GameObject obj = parent.transform.Find(childName).gameObject;
            t = obj.GetComponent<T>();
        }
        if (t != null && t is Button && BtnClickHandle != null)
        {
            (t as Button).onClick.RemoveListener(BtnClickHandle);
            (t as Button).onClick.AddListener(BtnClickHandle);
        }
        return t;
    }
    /// <summary>
    /// 查找本游戏物体下的特定名称的子物体系统,并将其返回
    /// </summary>
    /// <param name="_target">要在其中进行查找的父物体</param>
    /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param>
    /// <returns></returns>
    public static Transform FindDeepChild(this GameObject _target, string _childName)
    {
        Transform resultTrs = null;
        resultTrs = _target.transform.Find(_childName);
        if (resultTrs == null)
        {
            foreach (Transform trs in _target.transform)
            {
                resultTrs = trs.gameObject.FindDeepChild(_childName);
                if (resultTrs != null)
                    return resultTrs;
            }
        }
        return resultTrs;
    }

    /// <summary>
    /// 查找本游戏物体下的特定名称的子物体系统,并将其返回
    /// </summary>
    /// <param name="_target">要在其中进行查找的父物体</param>
    /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param>
    /// <returns></returns>
    public static Transform FindDeepChild(this Transform _target, string _childName)
    {
        Transform resultTrs = null;
        resultTrs = _target.Find(_childName);
        if (resultTrs == null)
        {
            foreach (Transform trs in _target)
            {
                resultTrs = trs.gameObject.FindDeepChild(_childName);
                if (resultTrs != null)
                    return resultTrs;
            }
        }
        return resultTrs;
    }
    /// <summary>
    /// 查找本游戏物体下的特定名称的子物体系统的特定组件,并将其返回
    /// </summary>
    /// <param name="_target">要在其中进行查找的父物体</param>
    /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param>
    /// <returns>返回找到的符合条件的第一个自物体下的指定组件</returns>
    public static T FindDeepChild<T>(this GameObject _target, string _childName) where T : Component
    {
        Transform resultTrs = _target.FindDeepChild(_childName);
        if (resultTrs != null)
            return resultTrs.gameObject.GetComponent<T>();
        return (T)((object)null);
    }
    /// <summary>
    /// 查找本游戏物体下的特定名称的子物体系统的特定组件,并将其返回
    /// </summary>
    /// <param name="_target">要在其中进行查找的父物体</param>
    /// <param name="_childName">待查找的子物体名称,可以是"/"分割的多级名称</param>
    /// <returns>返回找到的符合条件的第一个自物体下的指定组件</returns>
    public static T FindDeepChild<T>(this Transform _target, string _childName) where T : Component
    {
        Transform resultTrs = _target.FindDeepChild(_childName);
        if (resultTrs != null)
            return resultTrs.gameObject.GetComponent<T>();
        return (T)((object)null);
    }
}

这就是查找子物体及子物体组件的工具类,不管有多少层级,都可以查找到需要的东西。文章源自大腿Plus-https://www.shijunzh.com/archives/1002

大腿Plus
  • 本文由 发表于 2018年9月17日11:58:16
  • 转载请务必保留本文链接:https://www.shijunzh.com/archives/1002

发表评论