Advertisement
AkitoApocalypse

Untitled

Aug 16th, 2022
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. public static class Unitilities {
  2.     // Given originating GameObject and path, retrieve respective type (or null) from given path
  3.     public static GameObject? retrieveFromPath(GameObject originating, string path) {
  4.         // Split path and recursively call retrieval using recursiveRetrieve
  5.         string[] parts = path.Split("/"); // "/" instead of "\"
  6.         GameObject? result = originating;
  7.         foreach (string part in parts) {
  8.             result = recursiveRetrieve(result, part);
  9.             if (result == null) {
  10.                 // Null result, can't traverse anymore
  11.                 break;
  12.             }
  13.         }
  14.  
  15.         return result;
  16.     }
  17.  
  18.     // Recursively retrieve GameObject relative to current
  19.     // Path represents nothing ("."), parent (".."), or destination (anything else)
  20.     private static GameObject? recursiveRetrieve(GameObject current, string part) {
  21.         GameObject? result;
  22.         switch (part) {
  23.             case ".": { // Don't move
  24.                 result = current; break;
  25.             }
  26.             case "..": { // Ascend to parent (if defined)
  27.                 result = current.transform.parent?.gameObject;
  28.                 break;
  29.             }
  30.             default: {
  31.                 result = current.transform.Find(part)?.gameObject;
  32.                 break;
  33.             }
  34.         }
  35.  
  36.         return result;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement