Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Unitilities {
- // Given originating GameObject and path, retrieve respective type (or null) from given path
- public static GameObject? retrieveFromPath(GameObject originating, string path) {
- // Split path and recursively call retrieval using recursiveRetrieve
- string[] parts = path.Split("/"); // "/" instead of "\"
- GameObject? result = originating;
- foreach (string part in parts) {
- result = recursiveRetrieve(result, part);
- if (result == null) {
- // Null result, can't traverse anymore
- break;
- }
- }
- return result;
- }
- // Recursively retrieve GameObject relative to current
- // Path represents nothing ("."), parent (".."), or destination (anything else)
- private static GameObject? recursiveRetrieve(GameObject current, string part) {
- GameObject? result;
- switch (part) {
- case ".": { // Don't move
- result = current; break;
- }
- case "..": { // Ascend to parent (if defined)
- result = current.transform.parent?.gameObject;
- break;
- }
- default: {
- result = current.transform.Find(part)?.gameObject;
- break;
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement