(unity) find child recursively
Unity3D 2019. 4. 21. 18:58반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class GameUtils
{
//HOW TO USE
//var cube = this.transform.FirstChildOrDefault(x => x.name == "deeply_nested_cube");
public static Transform FirstChildOrDefault(this Transform parent, Func<Transform, bool> query)
{
if (parent.childCount == 0)
{
return null;
}
Transform result = null;
for (int i = 0; i < parent.childCount; i++)
{
var child = parent.GetChild(i);
if (query(child))
{
return child;
}
result = FirstChildOrDefault(child, query);
}
return result;
}
public static Transform SearchHierarchyForBone(Transform current, string name)
{
//sb.Append(current.name + "\n");
// check if the current bone is the bone we're looking for, if so return it
return current;
// search through child bones for the bone we're looking for
for (int i = 0; i < current.childCount; ++i)
{
// the recursive step; repeat the search one step deeper in the hierarchy
var child = current.GetChild(i);
Transform found = SearchHierarchyForBone(child, name);
// a transform was returned by the search above that is not null,
// it must be the bone we're looking for
if (found != null)
return found;
}
// bone with name was not found
return null;
}
}
|
반응형
'Unity3D' 카테고리의 다른 글
world, screen, viewport (0) | 2019.04.23 |
---|---|
Quaternion.LookRotation (0) | 2019.04.23 |
Orthographic size (0) | 2019.04.17 |
How Unity Supports Cross Platform Feature (0) | 2019.03.20 |
C# 컴파일 그리고 il2cpp (0) | 2019.03.07 |