UnityReferenceHelper.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using UnityEngine;
  2. namespace Pathfinding {
  3. [ExecuteInEditMode]
  4. /// <summary>
  5. /// Helper class to keep track of references to GameObjects.
  6. /// Does nothing more than to hold a GUID value.
  7. /// </summary>
  8. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_unity_reference_helper.php")]
  9. public class UnityReferenceHelper : MonoBehaviour {
  10. [HideInInspector]
  11. [SerializeField]
  12. private string guid;
  13. public string GetGUID () {
  14. return guid;
  15. }
  16. public void Awake () {
  17. Reset();
  18. }
  19. public void Reset () {
  20. if (string.IsNullOrEmpty(guid)) {
  21. guid = Pathfinding.Util.Guid.NewGuid().ToString();
  22. Debug.Log("Created new GUID - " + guid, this);
  23. } else if (gameObject.scene.name != null) {
  24. // Create a new GUID if there are duplicates in the scene.
  25. // Don't do this if this is a prefab (scene.name == null)
  26. foreach (UnityReferenceHelper urh in FindObjectsOfType(typeof(UnityReferenceHelper)) as UnityReferenceHelper[]) {
  27. if (urh != this && guid == urh.guid) {
  28. guid = Pathfinding.Util.Guid.NewGuid().ToString();
  29. Debug.Log("Created new GUID - " + guid, this);
  30. return;
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }