Guest User

Untitled

a guest
Oct 1st, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3.  
  4. namespace Skatebirb.Util
  5. {
  6. /// <summary>
  7. /// This is a market component that exists purely to be found by spawning things that need to spawn at a specific position, or under a specific parent
  8. /// </summary>
  9. public class SpawnLocationMarker : MonoBehaviour
  10. {
  11. static private SpawnLocationMarker atLeastOneExists;
  12.  
  13. /// <summary>
  14. /// Finds a spawn location of the given name in the currently loaded scene
  15. /// </summary>
  16. /// <param name="locName">the name of location to find</param>
  17. /// <returns>the location, or NULL if none was found</returns>
  18. static public SpawnLocationMarker FindLocation(string locName)
  19. {
  20. if (atLeastOneExists == null) { return null; }
  21.  
  22. return atLeastOneExists.FindLocationInternal(locName);
  23. }
  24.  
  25. private SpawnLocationMarker FindLocationInternal(string locName)
  26. {
  27. if (string.IsNullOrEmpty(locName)) { return null; }
  28.  
  29. // We're searching by name, let's NOT include case, because for fuck's sake
  30. locName = locName.ToLowerInvariant();
  31.  
  32. // @TODO: We could cache this list on Awake, if we needed to? Though it's unlikely to ever be called that often.
  33. SpawnLocationMarker[] locations = FindObjectsOfType<SpawnLocationMarker>();
  34. foreach(SpawnLocationMarker location in locations)
  35. {
  36. if (location.gameObject.name.ToLowerInvariant() == locName) { return location; }
  37. }
  38.  
  39. return null;
  40. }
  41.  
  42. private void Awake()
  43. {
  44. // We do this so that we can do static operations that would normally require a MonoBehavior (like finding-by-component)
  45. atLeastOneExists = this;
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment