BsPrefabUtility.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGameObject.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Performs various prefab specific operations.
  8. */
  9. class BS_CORE_EXPORT PrefabUtility
  10. {
  11. private:
  12. /**
  13. * @brief Contains saved Component instance data.
  14. */
  15. struct ComponentProxy
  16. {
  17. GameObjectInstanceDataPtr instanceData;
  18. UINT32 linkId;
  19. };
  20. /**
  21. * @brief Contains saved SceneObject instance data, as well as
  22. * saved instance data for all its children and components.
  23. */
  24. struct SceneObjectProxy
  25. {
  26. GameObjectInstanceDataPtr instanceData;
  27. UINT32 linkId;
  28. Vector<ComponentProxy> components;
  29. Vector<SceneObjectProxy> children;
  30. };
  31. public:
  32. /**
  33. * @brief Remove any instance specific changes to the object or its hierarchy from the
  34. * provided prefab instance and restore it to the exact copy of the linked prefab.
  35. *
  36. * @param[in] so Object to revert.
  37. */
  38. static void revertToPrefab(const HSceneObject& so);
  39. /**
  40. * @brief Updates all of the objects belonging to the same prefab instance
  41. * as the provided object (if any). The update will apply any changes
  42. * from the linked prefab to the hierarchy (if any).
  43. *
  44. * @param[in] so Object to update.
  45. */
  46. static void updateFromPrefab(const HSceneObject& so);
  47. /**
  48. * @brief Generates prefab "link" ID that can be used for tracking which game object
  49. * in a prefab instance corresponds to an object in the prefab.
  50. *
  51. * @note If any children of the provided object belong to another prefab they will
  52. * not have IDs generated.
  53. */
  54. static UINT32 generatePrefabIds(const HSceneObject& sceneObject, UINT32 startingId);
  55. /**
  56. * @brief Clears all prefab "link" IDs in the provided object and its children.
  57. *
  58. * @note If any of its children belong to another prefab they will not be cleared.
  59. */
  60. static void clearPrefabIds(const HSceneObject& sceneObject, bool recursive = true);
  61. /**
  62. * @brief Updates the internal prefab diff data by recording the difference
  63. * between the current values in the provided prefab instance and its prefab.
  64. *
  65. * @note If the provided object contains any child prefab instances, this will be
  66. * done recursively for them as well.
  67. */
  68. static void recordPrefabDiff(const HSceneObject& sceneObject);
  69. private:
  70. /**
  71. * @brief Traverses the object hierarchy, finds all child objects and components
  72. * and records their instance data, as well as their original place in the hierarchy.
  73. * Instance data essentially holds the object's "identity" and by restoring it we
  74. * ensure any handles pointing to the object earlier will still point to the new version.
  75. *
  76. * @param[in] so Object to traverse and record.
  77. * @param[out] output Contains the output hierarchy of instance data.
  78. * @param[out] linkedInstanceData A map of link IDs to instance data. Objects without
  79. * link IDs will not be included here.
  80. *
  81. * @note Does not recurse into child prefab instances.
  82. */
  83. static void recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  84. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData);
  85. /**
  86. * @brief Restores instance data in the provided hierarchy, using link ids to determine
  87. * what data maps to which objects.
  88. *
  89. * @param[in] so Object to traverse and restore the instance data.
  90. * @param[in] proxy Hierarchy containing instance data for all objects and components, returned by
  91. * "recordInstanceData" method.
  92. * @param[in] linkedInstanceData A map of link IDs to instance data, returned by "recordInstanceData" method.
  93. *
  94. * @note Does not recurse into child prefab instances.
  95. */
  96. static void restoreLinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy,
  97. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData);
  98. /**
  99. * @brief Restores instance data in the provided hierarchy, but only for objects without a link id.
  100. * Since the objects do not have a link ID we rely on their sequential order to find out
  101. * which instance data belongs to which object.
  102. *
  103. * @param[in] so Object to traverse and restore the instance data.
  104. * @param[in] proxy Hierarchy containing instance data for all objects and components, returned by
  105. * "recordInstanceData" method.
  106. *
  107. * @note Does not recurse into child prefab instances.
  108. */
  109. static void restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy);
  110. };
  111. }