2021. 11. 30. 01:54
Unity/C#
공통부모들을 반환
Transform[] GetCommonParents(params Transform[] targetTransforms)
{
if ((targetTransforms == null) || (targetTransforms.Length == 0))
{
return null;
}
var parentsArray = System.Array.ConvertAll(targetTransforms, x => x.GetComponentsInParent<Transform>());
var commonParents = parentsArray[0];
for (int i = 1; i < parentsArray.Length; i++)
{
commonParents = System.Array.FindAll(commonParents, x => parentsArray[i].Contains(x));
}
return commonParents;
}
부모와 자식사이의 노드들을 반환
앞쪽에 있을수록 부모에 가까움
Transform[] GetNodes(Transform parent,Transform child)
{
if ((parent == null) || (child == null))
{
return null;
}
var parents = child.GetComponentsInParent<Transform>();
var parentIndex = System.Array.FindIndex(parents, x=>x==parent);
if (parentIndex < 0)
{
return null;
}
if (parentIndex == 0)
{
return new Transform[] { } ;
}
return parents.Take(parentIndex).Reverse().ToArray();
}
'Unity > C#' 카테고리의 다른 글
유니티 ScriptableObject (0) | 2021.12.16 |
---|---|
C# 메일 (0) | 2021.11.18 |
A Star (0) | 2021.11.06 |