BsPrefab.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "BsPrefab.h"
  2. #include "BsPrefabRTTI.h"
  3. #include "BsResources.h"
  4. #include "BsSceneObject.h"
  5. #include "BsPrefabUtility.h"
  6. namespace BansheeEngine
  7. {
  8. Prefab::Prefab()
  9. :Resource(false), mHash(0), mNextLinkId(0)
  10. {
  11. }
  12. Prefab::~Prefab()
  13. {
  14. if (mRoot != nullptr)
  15. mRoot->destroy(true);
  16. }
  17. HPrefab Prefab::create(const HSceneObject& sceneObject)
  18. {
  19. PrefabPtr newPrefab = createEmpty();
  20. newPrefab->initialize(sceneObject);
  21. HPrefab handle = static_resource_cast<Prefab>(gResources()._createResourceHandle(newPrefab));
  22. newPrefab->mUUID = handle.getUUID();
  23. sceneObject->mPrefabLinkUUID = newPrefab->mUUID;
  24. newPrefab->_getRoot()->mPrefabLinkUUID = newPrefab->mUUID;
  25. return handle;
  26. }
  27. PrefabPtr Prefab::createEmpty()
  28. {
  29. PrefabPtr newPrefab = bs_core_ptr<Prefab>(new (bs_alloc<Prefab>()) Prefab());
  30. newPrefab->_setThisPtr(newPrefab);
  31. return newPrefab;
  32. }
  33. void Prefab::initialize(const HSceneObject& sceneObject)
  34. {
  35. sceneObject->mPrefabDiff = nullptr;
  36. UINT32 newNextLinkId = PrefabUtility::generatePrefabIds(sceneObject, mNextLinkId);
  37. if (newNextLinkId < mNextLinkId)
  38. {
  39. BS_EXCEPT(InternalErrorException, "Prefab ran out of IDs to assign. " \
  40. "Consider increasing the size of the prefab ID data type.");
  41. }
  42. mNextLinkId = newNextLinkId;
  43. // If there are any child prefab instances, make sure to update their diffs so they are saved with this prefab
  44. Stack<HSceneObject> todo;
  45. todo.push(sceneObject);
  46. while (!todo.empty())
  47. {
  48. HSceneObject current = todo.top();
  49. todo.pop();
  50. UINT32 childCount = current->getNumChildren();
  51. for (UINT32 i = 0; i < childCount; i++)
  52. {
  53. HSceneObject child = current->getChild(i);
  54. if (!child->mPrefabLinkUUID.empty())
  55. PrefabUtility::recordPrefabDiff(child);
  56. else
  57. todo.push(child);
  58. }
  59. }
  60. // Clone the hierarchy for internal storage
  61. mRoot = sceneObject->clone(false);
  62. mRoot->mParent = nullptr;
  63. // Remove objects with "dont save" flag
  64. todo.push(mRoot);
  65. while (!todo.empty())
  66. {
  67. HSceneObject current = todo.top();
  68. todo.pop();
  69. if (current->hasFlag(SOF_DontSave))
  70. current->destroy();
  71. else
  72. {
  73. UINT32 numChildren = current->getNumChildren();
  74. for (UINT32 i = 0; i < numChildren; i++)
  75. todo.push(current->getChild(i));
  76. }
  77. }
  78. }
  79. void Prefab::update(const HSceneObject& sceneObject)
  80. {
  81. initialize(sceneObject);
  82. sceneObject->mPrefabLinkUUID = mUUID;
  83. mRoot->mPrefabLinkUUID = mUUID;
  84. mHash++;
  85. }
  86. void Prefab::_updateChildInstances()
  87. {
  88. Stack<HSceneObject> todo;
  89. todo.push(mRoot);
  90. while (!todo.empty())
  91. {
  92. HSceneObject current = todo.top();
  93. todo.pop();
  94. UINT32 childCount = current->getNumChildren();
  95. for (UINT32 i = 0; i < childCount; i++)
  96. {
  97. HSceneObject child = current->getChild(i);
  98. if (!child->mPrefabLinkUUID.empty())
  99. PrefabUtility::updateFromPrefab(child);
  100. else
  101. todo.push(child);
  102. }
  103. }
  104. }
  105. HSceneObject Prefab::instantiate()
  106. {
  107. if (mRoot == nullptr)
  108. return HSceneObject();
  109. #if BS_EDITOR_BUILD
  110. // Update any child prefab instances in case their prefabs changed
  111. _updateChildInstances();
  112. #endif
  113. HSceneObject clone = _clone();
  114. clone->_instantiate();
  115. return clone;
  116. }
  117. HSceneObject Prefab::_clone()
  118. {
  119. if (mRoot == nullptr)
  120. return HSceneObject();
  121. mRoot->mPrefabHash = mHash;
  122. return mRoot->clone();
  123. }
  124. RTTITypeBase* Prefab::getRTTIStatic()
  125. {
  126. return PrefabRTTI::instance();
  127. }
  128. RTTITypeBase* Prefab::getRTTI() const
  129. {
  130. return Prefab::getRTTIStatic();
  131. }
  132. }