Made GameObject an IEnumerable to follow Unity's Transform interface for iterating through children

This commit is contained in:
Kah Wei 2022-11-08 18:32:47 +08:00
parent 9be58e7e5d
commit 8512c658f9
3 changed files with 35 additions and 1 deletions

View File

@ -265,6 +265,9 @@ namespace SHADE
System::Collections::Generic::IEnumerable<GameObject>^ GameObject::GetChildren() System::Collections::Generic::IEnumerable<GameObject>^ GameObject::GetChildren()
{ {
// Validity Checks
if (!valid)
throw gcnew System::NullReferenceException();
return ChildListCache::GetChildList(entity); return ChildListCache::GetChildList(entity);
} }
@ -390,4 +393,21 @@ namespace SHADE
{ {
return !(lhs == rhs); return !(lhs == rhs);
} }
/*---------------------------------------------------------------------------------*/
/* IEnummerable */
/*---------------------------------------------------------------------------------*/
System::Collections::Generic::IEnumerator<GameObject>^ GameObject::GetEnumerator()
{
System::Collections::Generic::IEnumerable<GameObject>^ childList = GetChildren();
if (childList == nullptr)
return System::Linq::Enumerable::Empty<GameObject>()->GetEnumerator();
else
return childList->GetEnumerator();
}
System::Collections::IEnumerator^ GameObject::GetEnumeratorNonGeneric()
{
return GetEnumerator();
}
} }

View File

@ -32,8 +32,9 @@ namespace SHADE
/// Lightweight object for an Entity that allows for easy access to Component and /// Lightweight object for an Entity that allows for easy access to Component and
/// Script operations. /// Script operations.
/// Can be set to a invalid/null GameObject by default construction. /// Can be set to a invalid/null GameObject by default construction.
/// Can also be iterated to access children.
/// </summary> /// </summary>
public value class GameObject : public System::IEquatable<GameObject> public value class GameObject : public System::IEquatable<GameObject>, public System::Collections::Generic::IEnumerable<GameObject>
{ {
public: public:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -386,6 +387,14 @@ namespace SHADE
/// <param name="rhs">Another GameObject to check with.</param> /// <param name="rhs">Another GameObject to check with.</param>
/// <returns>True if both Components are different.</returns> /// <returns>True if both Components are different.</returns>
static bool operator!=(GameObject lhs, GameObject rhs); static bool operator!=(GameObject lhs, GameObject rhs);
/*-----------------------------------------------------------------------------*/
/* IEnummerable */
/*-----------------------------------------------------------------------------*/
/// <inheritdoc/>
System::Collections::Generic::IEnumerator<GameObject>^ GetEnumerator() override;
/// <inheritdoc/>
System::Collections::IEnumerator^ GetEnumeratorNonGeneric() override = System::Collections::IEnumerable::GetEnumerator;
}; };
} }

View File

@ -27,6 +27,11 @@ public class RaccoonShowcase : Script
Debug.LogError("Transform is NULL!"); Debug.LogError("Transform is NULL!");
} }
foreach (var child in Owner)
{
Debug.Log(child.Name);
}
originalScale = Transform.LocalScale.z; originalScale = Transform.LocalScale.z;
} }
protected override void update() protected override void update()