Click or drag to resize

HierarchyFindTransformByPath Method (Transform, String, Transform)

Finds a transform in the hirerachy by the provided path.

Namespace:  KSPDev.ModelUtils
Assembly:  KSPDev_Utils.1.0 (in KSPDev_Utils.1.0.dll) Version: 1.0 for KSP v1.6+
Syntax
C#
public static Transform FindTransformByPath(
	Transform parent,
	string[] path,
	Transform defValue = null
)
Request Example View Source

Parameters

parent
Type: UnityEngineTransform
The transfrom to start looking from.
path
Type: SystemString
The path elements. All the special symbols must be unescaped.
defValue (Optional)
Type: UnityEngineTransform
An object to return if the path is not found. This situation will be treated as a danger, and a warning log record will be made.

Return Value

Type: Transform
Transform or null if nothing found.
Remarks
Every element of the path may specify an exact transform name or a partial match pattern:
  • To find an element that contains symbol / (the path components separator), simply double it. E.g. "a//b" will be looking for component, named "a/b" instead fo seacrhing for child "b" in the parent "a".
  • * - any name matches. Such patterns can be nested to specify the desired level of nesting. E.g. */*/a will look for name a in the grandchildren.
  • * as a prefix - the name is matched by suffix. E.g. *a matches any name that ends with a.
  • * as a suffix - the name is matched by prefix. E.g. a* matches any name that starts with a.
  • ** - any path matches. What will eventually be found depends on the pattern to the right of **. E.g. **/a/b will go through all the nodes starting from the parent until path a/b is found. If multiple paths have matched the pattern, then the shortest path will be returned. Be careful with this pattern since in case of not matching anything it will walk thought the whole hirerachy, starting from parent.

All patterns except ** may have a matching index. It can be used to resolve matches when there are multiple objects found with the same name and at the same level. E.g. if there are two objects with name "a" at the root level then the first one can be accessed by pattern a:0, and the second one by pattern a:1.

Path search is slow since it needs walking though the hierarchy nodes. In the worst case all the nodes will be visited. Don't use this method in the performance demanding methods.

Examples
Given the following hierarchy:
// a
// + b
// | + c
// | | + c1
// | | + d
// | + c
// |   + d
// |     + e
// |       + e1
// + abc

Here are some matching examples:

// a/b/c/d/e/e1 => a/b/c/d/e/e1
// a/b/c/c1 => a/b/c/c1
// a/b/*/d/e/e1 => a/b/c/d/e/e1
// a/b/*/*/e/e1 => a/b/c/d/e/e1
// a/b/* => a/b/c, branch a/b/c/c1/d (the first match)
// a/b/*:0 => a/b/c, branch a/b/c/c1/d
// a/b/*:1 => a/b/c, branch a/b/c/d/e/e1
// a/b/c:1/d => a/b/c/d, branch a/b/c/d/e/e1
// **/e1 => a/b/c/d/e/e1
// **/c1 => a/b/c/c1
// **/c/d => a/b/c/d, branch a/b/c/d
// **/*c => a/abc. The other matched branch (a/b/c) will be refused due to the length. 
// a/**/e1 => a/b/c/d/e/e1
// *bc => a/abc
// ab* => a/abc
// *b* => a/abc
See Also