using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public static class GameObjectExtensions
{
///
/// 指定した名前のGameObjectが見つかるまで探します。
///
public static GameObject FindDeep(
this GameObject self,
string name,
bool includeInactive = true,
bool parent = false)
{
Transform[] children = null;
if (parent)
{
children = self.GetComponentsInParent(includeInactive);
}
else
{
children = self.GetComponentsInChildren(includeInactive);
}
foreach (var transform in children)
{
if (transform.name == name)
{
return transform.gameObject;
}
}
return null;
}
///
/// 指定した名前のGameObjectが見つかるまでPath指定で探します。
///
public static GameObject FindDeepPath(
this GameObject self,
string name,
bool includeInactive = true,
bool parent = false)
{
GameObject target = self;
string[] nameList = name.Split('/');
foreach (var findName in nameList)
{
GameObject result = target.FindDeep(findName, includeInactive, parent);
if(result != null)
{
target = result;
}
else
{
return null;
}
}
return target;
}
}