Skip to content

Class organisation #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 1 addition & 97 deletions Scripts/GameObjectExtensions.cs → Scripts/CreateChild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Duck.HieriarchyBehaviour
{
public static class GameObjectExtensions
public static partial class GameObjectExtensions
{
/// <summary>
/// Creates a new GameObject as a child transform.
Expand Down Expand Up @@ -145,101 +145,5 @@ public static TComponent CreateChild<TComponent, TArgs>(this GameObject parent,
behaviour.Initialize(args);
return behaviour;
}

/// <summary>
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
/// IHierarchyBehaviour's will be initialized.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy)
where TComponent : Component
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent>();
}

/// <summary>
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
/// TComponent will be initialized with the given arguements.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="args">The TArgs object to be passed in on initialization.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TArgs args)
where TComponent : Component, IHierarchyBehaviour<TArgs>
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent, TArgs>(args);
}

/// <summary>
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
/// IHierarchyBehaviour's will be initialized.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="path">The path to the resourced asset.</param>
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, string path, bool worldPositionStays = true)
where TComponent : Component
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent>(path, worldPositionStays);
}

/// <summary>
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
/// TComponent will be initialized with the given arguements.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="path">The path to the resourced asset.</param>
/// <param name="args">The TArgs object to be passed in on initialization.</param>
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, string path, TArgs args, bool worldPositionStays = true)
where TComponent : Component, IHierarchyBehaviour<TArgs>
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent, TArgs>(path, args, worldPositionStays);
}

/// <summary>
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
/// IHierarchyBehaviour's will be initialized.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="toClone">The GameObject to clone.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, TComponent toClone)
where TComponent : Component
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild(toClone);
}

/// <summary>
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
/// TComponent will be initialized with the given arguements.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="toClone">The GameObject to clone.</param>
/// <param name="args">The TArgs object to be passed in on initialization.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TComponent toClone, TArgs args)
where TComponent : Component, IHierarchyBehaviour<TArgs>
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild(toClone, args);
}
}
}
File renamed without changes.
103 changes: 103 additions & 0 deletions Scripts/ReplaceChild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using UnityEngine;

namespace Duck.HieriarchyBehaviour
{
public static partial class GameObjectExtensions
{
/// <summary>
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
/// IHierarchyBehaviour's will be initialized.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy)
where TComponent : Component
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent>();
}

/// <summary>
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
/// TComponent will be initialized with the given arguements.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="args">The TArgs object to be passed in on initialization.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TArgs args)
where TComponent : Component, IHierarchyBehaviour<TArgs>
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent, TArgs>(args);
}

/// <summary>
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
/// IHierarchyBehaviour's will be initialized.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="path">The path to the resourced asset.</param>
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, string path, bool worldPositionStays = true)
where TComponent : Component
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent>(path, worldPositionStays);
}

/// <summary>
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
/// TComponent will be initialized with the given arguements.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="path">The path to the resourced asset.</param>
/// <param name="args">The TArgs object to be passed in on initialization.</param>
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, string path, TArgs args, bool worldPositionStays = true)
where TComponent : Component, IHierarchyBehaviour<TArgs>
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild<TComponent, TArgs>(path, args, worldPositionStays);
}

/// <summary>
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
/// IHierarchyBehaviour's will be initialized.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="toClone">The GameObject to clone.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, TComponent toClone)
where TComponent : Component
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild(toClone);
}

/// <summary>
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
/// TComponent will be initialized with the given arguements.
/// </summary>
/// <param name="toDestroy">The child Component to destroy.</param>
/// <param name="toClone">The GameObject to clone.</param>
/// <param name="args">The TArgs object to be passed in on initialization.</param>
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
/// <returns>The new TComponent</returns>
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TComponent toClone, TArgs args)
where TComponent : Component, IHierarchyBehaviour<TArgs>
{
Utils.DestroyChild(parent, toDestroy);
return parent.CreateChild(toClone, args);
}
}
}
13 changes: 13 additions & 0 deletions Scripts/ReplaceChild.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 154 additions & 0 deletions Tests/CreateChildTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using Duck.HieriarchyBehaviour.Tests.Behaviours;
using NUnit.Framework;
using UnityEngine;

namespace Duck.HieriarchyBehaviour.Tests
{
[TestFixture]
internal partial class GameObjectExtensionsTests
{
[Test]
public void Expect_CreateChild_GameObject_AsChild()
{
var gameObject = root.gameObject.CreateChild();
Assert.IsNotNull(gameObject);
Assert.AreEqual(root.transform, gameObject.transform.parent);
}

[Test]
public void Expect_CreateChild_GameObject_With_Name_AsChild()
{
const string GAME_OBJECT_NAME = "Test Name";
var gameObject = root.gameObject.CreateChild(GAME_OBJECT_NAME);
Assert.IsNotNull(gameObject);
Assert.AreEqual(GAME_OBJECT_NAME, gameObject.name);
Assert.AreEqual(root.transform, gameObject.transform.parent);
}

[Test]
public void Expect_CreateChild_Cloned_GameObject_AsChild()
{
var toClone = new GameObject("GameObject To Clone");
toClone.transform.SetParent(root.transform);
var gameObject = root.gameObject.CreateChild(toClone);
Assert.AreEqual(root.transform, gameObject.transform.parent);
}

[Test]
public void Expect_CreateChild_GameObject_FromResources_AsChild()
{
var gameObject = root.gameObject.CreateChild(path: PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
Assert.AreEqual(root.transform, gameObject.transform.parent);
}

[Test]
public void Expect_CreateChild_New_ToInitialize()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>();
Assert.IsTrue(behaviour.DidInitialize);
}

[Test]
public void Expect_CreateChild_New_AsChild()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>();
Assert.AreEqual(root.transform, behaviour.transform.parent);
}

[Test]
public void Expect_CreateChild_New_WithArgs_ToInitialize()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(TEST_ARGS);
Assert.IsTrue(behaviour.DidInitialize);
}

[Test]
public void Expect_CreateChild_New_WithArgs_AsChild()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(TEST_ARGS);
Assert.AreEqual(root.transform, behaviour.transform.parent);
}

[Test]
public void Expect_CreateChild_New_WithArgs_ToInitialize_WithArgs()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(TEST_ARGS);
Assert.AreEqual(TEST_ARGS, behaviour.Args);
}

[Test]
public void Expect_CreateChild_FromResources_ToInitialize()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
Assert.IsTrue(behaviour.DidInitialize);
}

[Test]
public void Expect_CreateChild_FromResources_AsChild()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
Assert.AreEqual(root.transform, behaviour.transform.parent);
}

[Test]
public void Expect_CreateChild_FromResources_WithArgs_ToInitialize()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(PREFAB_WITH_ARGS_RESOURCE_PATH, TEST_ARGS);
Assert.IsTrue(behaviour.DidInitialize);
}

[Test]
public void Expect_CreateChild_FromResources_WithArgs_AsChild()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(PREFAB_WITH_ARGS_RESOURCE_PATH, TEST_ARGS);
Assert.AreEqual(root.transform, behaviour.transform.parent);
}

[Test]
public void Expect_CreateChild_FromResources_WithArgs_ToInitialize_WithArgs()
{
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(PREFAB_WITH_ARGS_RESOURCE_PATH, TEST_ARGS);
Assert.AreEqual(TEST_ARGS, behaviour.Args);
}

[Test]
public void Expect_CreateChild_FromLoaded_ToInitialize()
{
var loadedBehaviour = Resources.Load<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
var behaviour = root.gameObject.CreateChild(loadedBehaviour);
Assert.IsTrue(behaviour.DidInitialize);
}

[Test]
public void Expect_CreateChild_FromLoaded_AsChild()
{
var loadedBehaviour = Resources.Load<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
var behaviour = root.gameObject.CreateChild(loadedBehaviour);
Assert.AreEqual(root.transform, behaviour.transform.parent);
}

[Test]
public void Expect_CreateChild_FromLoaded_WithArgs_ToInitialize()
{
var loadedBehaviour = Resources.Load<TestHierarchyBehaviourWithArgs>(PREFAB_WITH_ARGS_RESOURCE_PATH);
var behaviour = root.gameObject.CreateChild(loadedBehaviour, TEST_ARGS);
Assert.IsTrue(behaviour.DidInitialize);
}

[Test]
public void Expect_CreateChild_FromLoaded_WithArgs_AsChild()
{
var loadedBehaviour = Resources.Load<TestHierarchyBehaviourWithArgs>(PREFAB_WITH_ARGS_RESOURCE_PATH);
var behaviour = root.gameObject.CreateChild(loadedBehaviour, TEST_ARGS);
Assert.AreEqual(root.transform, behaviour.transform.parent);
}

[Test]
public void Expect_CreateChild_FromLoaded_WithArgs_ToInitialize_WithArgs()
{
var loadedBehaviour = Resources.Load<TestHierarchyBehaviourWithArgs>(PREFAB_WITH_ARGS_RESOURCE_PATH);
var behaviour = root.gameObject.CreateChild(loadedBehaviour, TEST_ARGS);
Assert.AreEqual(TEST_ARGS, behaviour.Args);
}
}
}
File renamed without changes.
Loading