Skip to content

Commit fc8105e

Browse files
Merge pull request #17 from dubit/dev_split
Class organisation
2 parents 8c71414 + 8f04924 commit fc8105e

11 files changed

+544
-482
lines changed

Scripts/GameObjectExtensions.cs renamed to Scripts/CreateChild.cs

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Duck.HieriarchyBehaviour
44
{
5-
public static class GameObjectExtensions
5+
public static partial class GameObjectExtensions
66
{
77
/// <summary>
88
/// Creates a new GameObject as a child transform.
@@ -145,101 +145,5 @@ public static TComponent CreateChild<TComponent, TArgs>(this GameObject parent,
145145
behaviour.Initialize(args);
146146
return behaviour;
147147
}
148-
149-
/// <summary>
150-
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
151-
/// IHierarchyBehaviour's will be initialized.
152-
/// </summary>
153-
/// <param name="toDestroy">The child Component to destroy.</param>
154-
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
155-
/// <returns>The new TComponent</returns>
156-
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy)
157-
where TComponent : Component
158-
{
159-
Utils.DestroyChild(parent, toDestroy);
160-
return parent.CreateChild<TComponent>();
161-
}
162-
163-
/// <summary>
164-
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
165-
/// TComponent will be initialized with the given arguements.
166-
/// </summary>
167-
/// <param name="toDestroy">The child Component to destroy.</param>
168-
/// <param name="args">The TArgs object to be passed in on initialization.</param>
169-
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
170-
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
171-
/// <returns>The new TComponent</returns>
172-
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TArgs args)
173-
where TComponent : Component, IHierarchyBehaviour<TArgs>
174-
{
175-
Utils.DestroyChild(parent, toDestroy);
176-
return parent.CreateChild<TComponent, TArgs>(args);
177-
}
178-
179-
/// <summary>
180-
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
181-
/// IHierarchyBehaviour's will be initialized.
182-
/// </summary>
183-
/// <param name="toDestroy">The child Component to destroy.</param>
184-
/// <param name="path">The path to the resourced asset.</param>
185-
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
186-
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
187-
/// <returns>The new TComponent</returns>
188-
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, string path, bool worldPositionStays = true)
189-
where TComponent : Component
190-
{
191-
Utils.DestroyChild(parent, toDestroy);
192-
return parent.CreateChild<TComponent>(path, worldPositionStays);
193-
}
194-
195-
/// <summary>
196-
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
197-
/// TComponent will be initialized with the given arguements.
198-
/// </summary>
199-
/// <param name="toDestroy">The child Component to destroy.</param>
200-
/// <param name="path">The path to the resourced asset.</param>
201-
/// <param name="args">The TArgs object to be passed in on initialization.</param>
202-
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
203-
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
204-
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
205-
/// <returns>The new TComponent</returns>
206-
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, string path, TArgs args, bool worldPositionStays = true)
207-
where TComponent : Component, IHierarchyBehaviour<TArgs>
208-
{
209-
Utils.DestroyChild(parent, toDestroy);
210-
return parent.CreateChild<TComponent, TArgs>(path, args, worldPositionStays);
211-
}
212-
213-
/// <summary>
214-
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
215-
/// IHierarchyBehaviour's will be initialized.
216-
/// </summary>
217-
/// <param name="toDestroy">The child Component to destroy.</param>
218-
/// <param name="toClone">The GameObject to clone.</param>
219-
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
220-
/// <returns>The new TComponent</returns>
221-
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, TComponent toClone)
222-
where TComponent : Component
223-
{
224-
Utils.DestroyChild(parent, toDestroy);
225-
return parent.CreateChild(toClone);
226-
}
227-
228-
/// <summary>
229-
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
230-
/// TComponent will be initialized with the given arguements.
231-
/// </summary>
232-
/// <param name="toDestroy">The child Component to destroy.</param>
233-
/// <param name="toClone">The GameObject to clone.</param>
234-
/// <param name="args">The TArgs object to be passed in on initialization.</param>
235-
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
236-
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
237-
/// <returns>The new TComponent</returns>
238-
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TComponent toClone, TArgs args)
239-
where TComponent : Component, IHierarchyBehaviour<TArgs>
240-
{
241-
Utils.DestroyChild(parent, toDestroy);
242-
return parent.CreateChild(toClone, args);
243-
}
244148
}
245149
}
File renamed without changes.

Scripts/ReplaceChild.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using UnityEngine;
2+
3+
namespace Duck.HieriarchyBehaviour
4+
{
5+
public static partial class GameObjectExtensions
6+
{
7+
/// <summary>
8+
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
9+
/// IHierarchyBehaviour's will be initialized.
10+
/// </summary>
11+
/// <param name="toDestroy">The child Component to destroy.</param>
12+
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
13+
/// <returns>The new TComponent</returns>
14+
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy)
15+
where TComponent : Component
16+
{
17+
Utils.DestroyChild(parent, toDestroy);
18+
return parent.CreateChild<TComponent>();
19+
}
20+
21+
/// <summary>
22+
/// Destroys the child GameObject and creates a new child GameObject with the given TComponent component.
23+
/// TComponent will be initialized with the given arguements.
24+
/// </summary>
25+
/// <param name="toDestroy">The child Component to destroy.</param>
26+
/// <param name="args">The TArgs object to be passed in on initialization.</param>
27+
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
28+
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
29+
/// <returns>The new TComponent</returns>
30+
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TArgs args)
31+
where TComponent : Component, IHierarchyBehaviour<TArgs>
32+
{
33+
Utils.DestroyChild(parent, toDestroy);
34+
return parent.CreateChild<TComponent, TArgs>(args);
35+
}
36+
37+
/// <summary>
38+
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
39+
/// IHierarchyBehaviour's will be initialized.
40+
/// </summary>
41+
/// <param name="toDestroy">The child Component to destroy.</param>
42+
/// <param name="path">The path to the resourced asset.</param>
43+
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
44+
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
45+
/// <returns>The new TComponent</returns>
46+
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, string path, bool worldPositionStays = true)
47+
where TComponent : Component
48+
{
49+
Utils.DestroyChild(parent, toDestroy);
50+
return parent.CreateChild<TComponent>(path, worldPositionStays);
51+
}
52+
53+
/// <summary>
54+
/// Destroys the child GameObject and creates a new child GameObject, by loading from resources and instantiating.
55+
/// TComponent will be initialized with the given arguements.
56+
/// </summary>
57+
/// <param name="toDestroy">The child Component to destroy.</param>
58+
/// <param name="path">The path to the resourced asset.</param>
59+
/// <param name="args">The TArgs object to be passed in on initialization.</param>
60+
/// <param name="worldPositionStays">Will the instantiated GameObject stay in its world position or be set to local origin.</param>
61+
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
62+
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
63+
/// <returns>The new TComponent</returns>
64+
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, string path, TArgs args, bool worldPositionStays = true)
65+
where TComponent : Component, IHierarchyBehaviour<TArgs>
66+
{
67+
Utils.DestroyChild(parent, toDestroy);
68+
return parent.CreateChild<TComponent, TArgs>(path, args, worldPositionStays);
69+
}
70+
71+
/// <summary>
72+
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
73+
/// IHierarchyBehaviour's will be initialized.
74+
/// </summary>
75+
/// <param name="toDestroy">The child Component to destroy.</param>
76+
/// <param name="toClone">The GameObject to clone.</param>
77+
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
78+
/// <returns>The new TComponent</returns>
79+
public static TComponent ReplaceChild<TComponent>(this GameObject parent, Component toDestroy, TComponent toClone)
80+
where TComponent : Component
81+
{
82+
Utils.DestroyChild(parent, toDestroy);
83+
return parent.CreateChild(toClone);
84+
}
85+
86+
/// <summary>
87+
/// Destroys the child GameObject and creates a clone of the given TComponent as a child transform.
88+
/// TComponent will be initialized with the given arguements.
89+
/// </summary>
90+
/// <param name="toDestroy">The child Component to destroy.</param>
91+
/// <param name="toClone">The GameObject to clone.</param>
92+
/// <param name="args">The TArgs object to be passed in on initialization.</param>
93+
/// <typeparam name="TComponent">The type of Component to be created.</typeparam>
94+
/// <typeparam name="TArgs">The type of arguements to be given on initialization.</typeparam>
95+
/// <returns>The new TComponent</returns>
96+
public static TComponent ReplaceChild<TComponent, TArgs>(this GameObject parent, Component toDestroy, TComponent toClone, TArgs args)
97+
where TComponent : Component, IHierarchyBehaviour<TArgs>
98+
{
99+
Utils.DestroyChild(parent, toDestroy);
100+
return parent.CreateChild(toClone, args);
101+
}
102+
}
103+
}

Scripts/ReplaceChild.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/CreateChildTests.cs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using Duck.HieriarchyBehaviour.Tests.Behaviours;
2+
using NUnit.Framework;
3+
using UnityEngine;
4+
5+
namespace Duck.HieriarchyBehaviour.Tests
6+
{
7+
[TestFixture]
8+
internal partial class GameObjectExtensionsTests
9+
{
10+
[Test]
11+
public void Expect_CreateChild_GameObject_AsChild()
12+
{
13+
var gameObject = root.gameObject.CreateChild();
14+
Assert.IsNotNull(gameObject);
15+
Assert.AreEqual(root.transform, gameObject.transform.parent);
16+
}
17+
18+
[Test]
19+
public void Expect_CreateChild_GameObject_With_Name_AsChild()
20+
{
21+
const string GAME_OBJECT_NAME = "Test Name";
22+
var gameObject = root.gameObject.CreateChild(GAME_OBJECT_NAME);
23+
Assert.IsNotNull(gameObject);
24+
Assert.AreEqual(GAME_OBJECT_NAME, gameObject.name);
25+
Assert.AreEqual(root.transform, gameObject.transform.parent);
26+
}
27+
28+
[Test]
29+
public void Expect_CreateChild_Cloned_GameObject_AsChild()
30+
{
31+
var toClone = new GameObject("GameObject To Clone");
32+
toClone.transform.SetParent(root.transform);
33+
var gameObject = root.gameObject.CreateChild(toClone);
34+
Assert.AreEqual(root.transform, gameObject.transform.parent);
35+
}
36+
37+
[Test]
38+
public void Expect_CreateChild_GameObject_FromResources_AsChild()
39+
{
40+
var gameObject = root.gameObject.CreateChild(path: PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
41+
Assert.AreEqual(root.transform, gameObject.transform.parent);
42+
}
43+
44+
[Test]
45+
public void Expect_CreateChild_New_ToInitialize()
46+
{
47+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>();
48+
Assert.IsTrue(behaviour.DidInitialize);
49+
}
50+
51+
[Test]
52+
public void Expect_CreateChild_New_AsChild()
53+
{
54+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>();
55+
Assert.AreEqual(root.transform, behaviour.transform.parent);
56+
}
57+
58+
[Test]
59+
public void Expect_CreateChild_New_WithArgs_ToInitialize()
60+
{
61+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(TEST_ARGS);
62+
Assert.IsTrue(behaviour.DidInitialize);
63+
}
64+
65+
[Test]
66+
public void Expect_CreateChild_New_WithArgs_AsChild()
67+
{
68+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(TEST_ARGS);
69+
Assert.AreEqual(root.transform, behaviour.transform.parent);
70+
}
71+
72+
[Test]
73+
public void Expect_CreateChild_New_WithArgs_ToInitialize_WithArgs()
74+
{
75+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(TEST_ARGS);
76+
Assert.AreEqual(TEST_ARGS, behaviour.Args);
77+
}
78+
79+
[Test]
80+
public void Expect_CreateChild_FromResources_ToInitialize()
81+
{
82+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
83+
Assert.IsTrue(behaviour.DidInitialize);
84+
}
85+
86+
[Test]
87+
public void Expect_CreateChild_FromResources_AsChild()
88+
{
89+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
90+
Assert.AreEqual(root.transform, behaviour.transform.parent);
91+
}
92+
93+
[Test]
94+
public void Expect_CreateChild_FromResources_WithArgs_ToInitialize()
95+
{
96+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(PREFAB_WITH_ARGS_RESOURCE_PATH, TEST_ARGS);
97+
Assert.IsTrue(behaviour.DidInitialize);
98+
}
99+
100+
[Test]
101+
public void Expect_CreateChild_FromResources_WithArgs_AsChild()
102+
{
103+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(PREFAB_WITH_ARGS_RESOURCE_PATH, TEST_ARGS);
104+
Assert.AreEqual(root.transform, behaviour.transform.parent);
105+
}
106+
107+
[Test]
108+
public void Expect_CreateChild_FromResources_WithArgs_ToInitialize_WithArgs()
109+
{
110+
var behaviour = root.gameObject.CreateChild<TestHierarchyBehaviourWithArgs, string>(PREFAB_WITH_ARGS_RESOURCE_PATH, TEST_ARGS);
111+
Assert.AreEqual(TEST_ARGS, behaviour.Args);
112+
}
113+
114+
[Test]
115+
public void Expect_CreateChild_FromLoaded_ToInitialize()
116+
{
117+
var loadedBehaviour = Resources.Load<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
118+
var behaviour = root.gameObject.CreateChild(loadedBehaviour);
119+
Assert.IsTrue(behaviour.DidInitialize);
120+
}
121+
122+
[Test]
123+
public void Expect_CreateChild_FromLoaded_AsChild()
124+
{
125+
var loadedBehaviour = Resources.Load<TestHierarchyBehaviour>(PREFAB_WITHOUT_ARGS_RESOURCE_PATH);
126+
var behaviour = root.gameObject.CreateChild(loadedBehaviour);
127+
Assert.AreEqual(root.transform, behaviour.transform.parent);
128+
}
129+
130+
[Test]
131+
public void Expect_CreateChild_FromLoaded_WithArgs_ToInitialize()
132+
{
133+
var loadedBehaviour = Resources.Load<TestHierarchyBehaviourWithArgs>(PREFAB_WITH_ARGS_RESOURCE_PATH);
134+
var behaviour = root.gameObject.CreateChild(loadedBehaviour, TEST_ARGS);
135+
Assert.IsTrue(behaviour.DidInitialize);
136+
}
137+
138+
[Test]
139+
public void Expect_CreateChild_FromLoaded_WithArgs_AsChild()
140+
{
141+
var loadedBehaviour = Resources.Load<TestHierarchyBehaviourWithArgs>(PREFAB_WITH_ARGS_RESOURCE_PATH);
142+
var behaviour = root.gameObject.CreateChild(loadedBehaviour, TEST_ARGS);
143+
Assert.AreEqual(root.transform, behaviour.transform.parent);
144+
}
145+
146+
[Test]
147+
public void Expect_CreateChild_FromLoaded_WithArgs_ToInitialize_WithArgs()
148+
{
149+
var loadedBehaviour = Resources.Load<TestHierarchyBehaviourWithArgs>(PREFAB_WITH_ARGS_RESOURCE_PATH);
150+
var behaviour = root.gameObject.CreateChild(loadedBehaviour, TEST_ARGS);
151+
Assert.AreEqual(TEST_ARGS, behaviour.Args);
152+
}
153+
}
154+
}
File renamed without changes.

0 commit comments

Comments
 (0)