BsPrefabUtility.h 4.3 KB

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