以下の内容はhttps://baba-s.hatenablog.com/entry/2014/12/25/211108より取得しました。


【Unity】インターフェイスを指定して子オブジェクトから複数のコンポーネントを取得する拡張メソッド

ソースコード

using System.Collections.Generic;
using UnityEngine;

public static class GameObjectExtensions
{
    /// <summary>
    /// インターフェイスを指定して子オブジェクトから複数のコンポーネントを取得します
    /// </summary>
    public static T[] GetInterfacesOfComponentInChildren<T>( 
        this GameObject self, 
        bool includeInactive 
    ) where T : class
    {
        var result = new List<T>();
        foreach ( var n in self.GetComponentsInChildren<Component>( includeInactive ) )
        {
            var component = n as T;
            if ( component != null )
            {
                result.Add( component );
            }
        }
        return result.ToArray();
    }
    
    /// <summary>
    /// インターフェイスを指定して子オブジェクトから複数のコンポーネントを取得します
    /// </summary>
    public static T[] GetInterfacesOfComponentInChildren<T>( this GameObject self ) where T : class 
    {
        return self.GetInterfacesOfComponentInChildren<T>( false );
    }
    
}

関連記事




以上の内容はhttps://baba-s.hatenablog.com/entry/2014/12/25/211108より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14