Kaynağa Gözat

Added an utility for searching all components of a certain type in the scene object tree

BearishSun 8 yıl önce
ebeveyn
işleme
65b502721c

+ 9 - 0
Source/BansheeCore/Include/BsUtility.h

@@ -35,6 +35,15 @@ namespace bs
 		 */
 		static Vector<ResourceDependency> findResourceDependencies(IReflectable& object, bool recursive = true);
 
+		/**
+		 * Finds all components of a specific type on a scene object and any of its children.
+		 * 
+		 * @param[in]	object		Object which to search for components. All children will be searched as well.
+		 * @param[in]	typeId		RTTI type ID of the component type to search for.
+		 * @return					A list of all components of the specified type.
+		 */
+		static Vector<HComponent> findComponents(const HSceneObject& object, UINT32 typeId);
+
 		/** Calculates how deep in the scene object hierarchy is the provided object. Zero means root. */
 		static UINT32 getSceneObjectDepth(const HSceneObject& so);
 

+ 27 - 0
Source/BansheeCore/Source/BsUtility.cpp

@@ -137,6 +137,33 @@ namespace bs
 		rtti->onSerializationEnded(&obj, dummyParams);
 	}
 
+	Vector<HComponent> Utility::findComponents(const HSceneObject& object, UINT32 typeId)
+	{
+		Vector<HComponent> output;
+
+		Stack<HSceneObject> todo;
+		todo.push(object);
+
+		while(!todo.empty())
+		{
+			HSceneObject curSO = todo.top();
+			todo.pop();
+
+			const Vector<HComponent>& components = curSO->getComponents();
+			for(auto& entry : components)
+			{
+				if (entry->getRTTI()->getRTTIId() == typeId)
+					output.push_back(entry);
+			}
+
+			UINT32 numChildren = curSO->getNumChildren();
+			for (UINT32 i = 0; i < numChildren; i++)
+				todo.push(curSO->getChild(i));
+		}
+
+		return output;
+	}
+
 	bool Utility::hasReflectableChildren(RTTITypeBase* type)
 	{
 		UINT32 numFields = type->getNumFields();