BsPrefabUtility.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. 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 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 void generatePrefabIds(const HSceneObject& sceneObject);
  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] linkedInstanceData A map of link IDs to instance data, returned by "recordInstanceData" method.
  91. *
  92. * @note Does not recurse into child prefab instances.
  93. */
  94. static void restoreLinkedInstanceData(const HSceneObject& so, UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData);
  95. /**
  96. * @brief Restores instance data in the provided hierarchy, but only for objects without a link id.
  97. * Since the objects do not have a link ID we rely on their sequential order to find out
  98. * which instance data belong to which object.
  99. *
  100. * @param[in] so Object to traverse and restore the instance data.
  101. * @param[in] proxy Hierarchy containing instance data for all objects and components, returned by
  102. * "recordInstanceData" method.
  103. *
  104. * @note Does not recurse into child prefab instances.
  105. */
  106. static void restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy);
  107. };
  108. }