Browse Source

No longer instantiate scene objects before prefab updating is done. This fixes an issue with managed game objects having different instances pointing to the same game object.

BearishSun 10 years ago
parent
commit
a1a448e85c

+ 7 - 1
BansheeCore/Include/BsPrefab.h

@@ -28,8 +28,14 @@ namespace BansheeEngine
 		 * @brief	Instantiates a prefab by creating an instance of the prefab's
 		 *			scene object hierarchy. The returned hierarchy will be parented
 		 *			to world root by default.
+		 *			
+		 * @param	onlyClone	If true the internal prefab hierarchy will be cloned and returned, but not actually 
+		 * 						instantiated. Caller must ensure to call ::instantiate on the returned scene object. 
+		 * 						If false the returned scene object will be instantiated before returning.
+		 * 						
+		 * @returns	Clone of the prefab's scene object hierarchy.
 		 */
-		HSceneObject instantiate();
+		HSceneObject instantiate(bool onlyClone = false);
 
 		/**
 		 * @brief	Replaces the contents of this prefab with new contents

+ 9 - 6
BansheeCore/Source/BsPrefab.cpp

@@ -87,19 +87,15 @@ namespace BansheeEngine
 		mHash++;
 	}
 
-	HSceneObject Prefab::instantiate()
+	HSceneObject Prefab::instantiate(bool onlyClone)
 	{
 		if (mRoot == nullptr)
 			return HSceneObject();
 
-		HSceneObject clone = mRoot->clone();
-		clone->instantiate();
-		clone->mPrefabHash = mHash;
-
 #if BS_EDITOR_BUILD
 		// Update any child prefab instances in case their prefabs changed
 		Stack<HSceneObject> todo;
-		todo.push(clone);
+		todo.push(mRoot);
 
 		while (!todo.empty())
 		{
@@ -119,6 +115,13 @@ namespace BansheeEngine
 		}
 #endif
 
+		mRoot->mPrefabHash = mHash;
+
+		HSceneObject clone = mRoot->clone();
+
+		if (!onlyClone)
+			clone->instantiate();
+		
 		return clone;
 	}
 

+ 1 - 1
BansheeCore/Source/BsPrefabUtility.cpp

@@ -103,7 +103,7 @@ namespace BansheeEngine
 				HSceneObject parent = current->getParent();
 
 				current->destroy(true);
-				HSceneObject newInstance = prefabLink->instantiate();
+				HSceneObject newInstance = prefabLink->instantiate(true);
 
 				if (prefabDiff != nullptr)
 					prefabDiff->apply(newInstance);