Hide
GetComponentByPath

Deprecated: use GetByXPath instead.

Automatically assign a component to a field by a given path.

  • (Optional)EGetComp config

    Options are:

    • EGetComp.ForceResign: when the target changed (e.g. you delete/create one), automatically resign the new correct component.
    • EGetComp.NoResignButton: do not display a re-sign button when the target mismatches.
  • string paths...

    Paths to search.

  • AllowMultiple: Yes. But not necessary.

The path is a bit like html's XPath but with less function:

PathMeaning
/Separator. Using at start means the root of the current scene.
//Separator. Any descendant children
.Node. Current node
..Node. Parent node
*All nodes
nameNode. Any nodes with this name
[last()]Index Filter. Last of results
[index() > 1]Index Filter. Node index that is greater than 1
[0]Index Filter. First node in the results

For example:

  • ./sth or sth: direct child object of current object named sth
  • .//sth: any descendant child under current. (descendant::sth)
  • ..//sth: first go to parent, then find the direct child named sth
  • /sth: top level node in current scene named sth
  • //sth: first go to top level, then find the direct child named sth
  • ///sth: first go to top level, then find any node named sth
  • ./get/sth[1]: the child named get of current node, then the second node named sth in the direct children list of get
using SaintsField; // starting from root, search any object with name "Dummy" [GetComponentByPath("///Dummy")] public GameObject dummy; // first child of current object [GetComponentByPath("./*[1]")] public GameObject direct1; // child of current object which has index greater than 1 [GetComponentByPath("./*[index() > 1]")] public GameObject directPosTg1; // last child of current object [GetComponentByPath("./*[last()]")] public GameObject directLast; // re-sign the target if mis-match [GetComponentByPath(EGetComp.NoResignButton | EGetComp.ForceResign, "./DirectSub")] public GameObject directSubWatched; // without "ForceResign", it'll display a reload button if mis-match // with multiple paths, it'll search from left to right [GetComponentByPath("/no", "./DirectSub1")] public GameObject directSubMulti; // if no match, it'll show an error message [GetComponentByPath("/no", "///sth/else/../what/.//ever[last()]/goes/here")] public GameObject notExists;

get_component_by_path