寻找目标Transform
/// <summary>
/// 查找根Transform(组件)下的目标Transform,返回布尔值
/// 递归查找
/// </summary>
/// <param name="Root"> 根Transform </param>
/// <param name="Objective"> 目标Transform </param>
/// <param name="setTransform"> 需要赋值的Transform </param>
/// <returns> 返回bool值 </returns>
bool IsRootFind(Transform Root, string Objective, out Transform setTransform)
{
//根与目标相同
if (Root.name == Objective)
{
setTransform = Root; //修改out变量
return true; //返回为真
}
//循环遍历子transform
//foreach (Transform n in Root) n不会遍历到孙子
foreach (Transform n in Root)
{
//调用自己,以自己的子Transform为根
//判断子transform下是否存在
if (IsRootFind(n, Objective, out setTransform))
{
return true;//返回为真(找到的时候已经修改为out变量,此处无需修改)
}
}
//所有子与孙都未找到
setTransform = null; //修改out为空
return false; //返回为空
}
bool IsRootFind(Transform Root, Transform Objective)
{
if(Root == Objective)
{
return true;
}
foreach (Transform n in Root)
{
if (IsRootFind(n, Objective))
{
return true;
}
}
return false;
}
bool IsRootFind(Transform Root, string Objective)
{
if (Root.name == Objective)
{
return true;
}
foreach (Transform n in Root)
{
if (IsRootFind(n, Objective))
{
return true;
}
}
return false;
}