com.wallstop-studios.unity-helpers 2.0.0-rc11 → 2.0.0-rc13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Runtime/Core/Attributes/ChildComponentAttribute.cs +124 -31
- package/Runtime/Core/Attributes/ParentComponent.cs +119 -29
- package/Runtime/Core/Attributes/SiblingComponentAttribute.cs +31 -12
- package/Runtime/Core/Extension/LoggingExtensions.cs +2 -2
- package/Tests/Runtime/Attributes/ChildComponentTests.cs +81 -0
- package/Tests/Runtime/Attributes/ChildComponentTests.cs.meta +3 -0
- package/Tests/Runtime/Attributes/Components/ExpectChildSpriteRenderers.cs +28 -0
- package/Tests/Runtime/Attributes/Components/ExpectChildSpriteRenderers.cs.meta +3 -0
- package/Tests/Runtime/Attributes/Components/ExpectParentSpriteRenderers.cs +28 -0
- package/Tests/Runtime/Attributes/Components/ExpectParentSpriteRenderers.cs.meta +3 -0
- package/Tests/Runtime/Attributes/Components.meta +3 -0
- package/Tests/Runtime/Attributes/ParentComponentTests.cs +68 -0
- package/Tests/Runtime/Attributes/ParentComponentTests.cs.meta +3 -0
- package/Tests/Runtime/Attributes.meta +3 -0
- package/package.json +1 -1
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
using System.Linq;
|
|
7
7
|
using System.Reflection;
|
|
8
8
|
using Extension;
|
|
9
|
+
using Helper;
|
|
9
10
|
using JetBrains.Annotations;
|
|
10
11
|
using UnityEngine;
|
|
11
12
|
|
|
@@ -14,6 +15,7 @@
|
|
|
14
15
|
public sealed class ChildComponentAttribute : Attribute
|
|
15
16
|
{
|
|
16
17
|
public bool optional = false;
|
|
18
|
+
public bool onlyDescendents = false;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
public static class ChildComponentExtensions
|
|
@@ -24,14 +26,17 @@
|
|
|
24
26
|
{
|
|
25
27
|
Type componentType = component.GetType();
|
|
26
28
|
FieldInfo[] fields = FieldsByType.GetOrAdd(
|
|
27
|
-
componentType,
|
|
29
|
+
componentType,
|
|
30
|
+
type =>
|
|
28
31
|
{
|
|
29
32
|
FieldInfo[] fields = type.GetFields(
|
|
30
|
-
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
33
|
+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
34
|
+
);
|
|
31
35
|
return fields
|
|
32
|
-
.Where(
|
|
36
|
+
.Where(field => Attribute.IsDefined(field, typeof(ChildComponentAttribute)))
|
|
33
37
|
.ToArray();
|
|
34
|
-
}
|
|
38
|
+
}
|
|
39
|
+
);
|
|
35
40
|
|
|
36
41
|
foreach (FieldInfo field in fields)
|
|
37
42
|
{
|
|
@@ -40,46 +45,134 @@
|
|
|
40
45
|
Type childComponentType = isArray ? fieldType.GetElementType() : fieldType;
|
|
41
46
|
|
|
42
47
|
bool foundChild;
|
|
43
|
-
if (
|
|
48
|
+
if (field.GetCustomAttribute<ChildComponentAttribute>().onlyDescendents)
|
|
44
49
|
{
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
if (isArray)
|
|
51
|
+
{
|
|
52
|
+
List<Component> children = new();
|
|
53
|
+
foreach (Transform child in component.IterateOverAllChildren())
|
|
54
|
+
{
|
|
55
|
+
children.AddRange(
|
|
56
|
+
child.GetComponentsInChildren(childComponentType, true)
|
|
57
|
+
);
|
|
58
|
+
}
|
|
47
59
|
|
|
48
|
-
|
|
49
|
-
Array.Copy(childComponents, correctTypedArray, childComponents.Length);
|
|
50
|
-
field.SetValue(component, correctTypedArray);
|
|
51
|
-
}
|
|
52
|
-
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
|
|
53
|
-
{
|
|
54
|
-
childComponentType = fieldType.GenericTypeArguments[0];
|
|
55
|
-
Type constructedListType = typeof(List<>).MakeGenericType(childComponentType);
|
|
56
|
-
IList instance = (IList)Activator.CreateInstance(constructedListType);
|
|
60
|
+
foundChild = 0 < children.Count;
|
|
57
61
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
Array correctTypedArray = Array.CreateInstance(
|
|
63
|
+
childComponentType,
|
|
64
|
+
children.Count
|
|
65
|
+
);
|
|
66
|
+
Array.Copy(children.ToArray(), correctTypedArray, children.Count);
|
|
67
|
+
field.SetValue(component, correctTypedArray);
|
|
63
68
|
}
|
|
69
|
+
else if (
|
|
70
|
+
fieldType.IsGenericType
|
|
71
|
+
&& fieldType.GetGenericTypeDefinition() == typeof(List<>)
|
|
72
|
+
)
|
|
73
|
+
{
|
|
74
|
+
childComponentType = fieldType.GenericTypeArguments[0];
|
|
75
|
+
Type constructedListType = typeof(List<>).MakeGenericType(
|
|
76
|
+
childComponentType
|
|
77
|
+
);
|
|
78
|
+
IList instance = (IList)Activator.CreateInstance(constructedListType);
|
|
79
|
+
|
|
80
|
+
foundChild = false;
|
|
81
|
+
foreach (
|
|
82
|
+
Component childComponent in component
|
|
83
|
+
.IterateOverAllChildren()
|
|
84
|
+
.SelectMany(child =>
|
|
85
|
+
child.GetComponentsInChildren(childComponentType, true)
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
{
|
|
89
|
+
instance.Add(childComponent);
|
|
90
|
+
foundChild = true;
|
|
91
|
+
}
|
|
64
92
|
|
|
65
|
-
|
|
93
|
+
field.SetValue(component, instance);
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
{
|
|
97
|
+
foundChild = false;
|
|
98
|
+
Component childComponent = null;
|
|
99
|
+
foreach (Transform child in component.IterateOverAllChildren())
|
|
100
|
+
{
|
|
101
|
+
childComponent = child.GetComponent(childComponentType);
|
|
102
|
+
if (childComponent != null)
|
|
103
|
+
{
|
|
104
|
+
foundChild = true;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (foundChild)
|
|
109
|
+
{
|
|
110
|
+
field.SetValue(component, childComponent);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
66
113
|
}
|
|
67
114
|
else
|
|
68
115
|
{
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
116
|
+
if (isArray)
|
|
117
|
+
{
|
|
118
|
+
Component[] childComponents = component.GetComponentsInChildren(
|
|
119
|
+
childComponentType,
|
|
120
|
+
true
|
|
121
|
+
);
|
|
122
|
+
foundChild = 0 < childComponents.Length;
|
|
123
|
+
|
|
124
|
+
Array correctTypedArray = Array.CreateInstance(
|
|
125
|
+
childComponentType,
|
|
126
|
+
childComponents.Length
|
|
127
|
+
);
|
|
128
|
+
Array.Copy(childComponents, correctTypedArray, childComponents.Length);
|
|
129
|
+
field.SetValue(component, correctTypedArray);
|
|
130
|
+
}
|
|
131
|
+
else if (
|
|
132
|
+
fieldType.IsGenericType
|
|
133
|
+
&& fieldType.GetGenericTypeDefinition() == typeof(List<>)
|
|
134
|
+
)
|
|
135
|
+
{
|
|
136
|
+
childComponentType = fieldType.GenericTypeArguments[0];
|
|
137
|
+
Type constructedListType = typeof(List<>).MakeGenericType(
|
|
138
|
+
childComponentType
|
|
139
|
+
);
|
|
140
|
+
IList instance = (IList)Activator.CreateInstance(constructedListType);
|
|
141
|
+
|
|
142
|
+
foundChild = false;
|
|
143
|
+
foreach (
|
|
144
|
+
Component childComponent in component.GetComponentsInChildren(
|
|
145
|
+
childComponentType,
|
|
146
|
+
true
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
{
|
|
150
|
+
instance.Add(childComponent);
|
|
151
|
+
foundChild = true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
field.SetValue(component, instance);
|
|
155
|
+
}
|
|
156
|
+
else
|
|
72
157
|
{
|
|
73
|
-
|
|
158
|
+
Component childComponent = component.GetComponentInChildren(
|
|
159
|
+
childComponentType,
|
|
160
|
+
true
|
|
161
|
+
);
|
|
162
|
+
foundChild = childComponent != null;
|
|
163
|
+
if (foundChild)
|
|
164
|
+
{
|
|
165
|
+
field.SetValue(component, childComponent);
|
|
166
|
+
}
|
|
74
167
|
}
|
|
75
168
|
}
|
|
76
169
|
|
|
77
170
|
if (!foundChild)
|
|
78
171
|
{
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
172
|
+
if (
|
|
173
|
+
field.GetCustomAttributes(typeof(ChildComponentAttribute), false)[0]
|
|
174
|
+
is ChildComponentAttribute { optional: false } _
|
|
175
|
+
)
|
|
83
176
|
{
|
|
84
177
|
component.LogError($"Unable to find child component of type {fieldType}");
|
|
85
178
|
}
|
|
@@ -87,4 +180,4 @@
|
|
|
87
180
|
}
|
|
88
181
|
}
|
|
89
182
|
}
|
|
90
|
-
}
|
|
183
|
+
}
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
public sealed class ParentComponentAttribute : Attribute
|
|
15
15
|
{
|
|
16
16
|
public bool optional = false;
|
|
17
|
+
public bool onlyAncestors = false;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
public static class ParentComponentExtensions
|
|
@@ -24,14 +25,19 @@
|
|
|
24
25
|
{
|
|
25
26
|
Type componentType = component.GetType();
|
|
26
27
|
FieldInfo[] fields = FieldsByType.GetOrAdd(
|
|
27
|
-
componentType,
|
|
28
|
+
componentType,
|
|
29
|
+
type =>
|
|
28
30
|
{
|
|
29
31
|
FieldInfo[] fields = type.GetFields(
|
|
30
|
-
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
32
|
+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
33
|
+
);
|
|
31
34
|
return fields
|
|
32
|
-
.Where(
|
|
35
|
+
.Where(field =>
|
|
36
|
+
Attribute.IsDefined(field, typeof(ParentComponentAttribute))
|
|
37
|
+
)
|
|
33
38
|
.ToArray();
|
|
34
|
-
}
|
|
39
|
+
}
|
|
40
|
+
);
|
|
35
41
|
|
|
36
42
|
foreach (FieldInfo field in fields)
|
|
37
43
|
{
|
|
@@ -40,44 +46,128 @@
|
|
|
40
46
|
Type parentComponentType = isArray ? fieldType.GetElementType() : fieldType;
|
|
41
47
|
|
|
42
48
|
bool foundParent;
|
|
43
|
-
if (
|
|
49
|
+
if (field.GetCustomAttribute<ParentComponentAttribute>().onlyAncestors)
|
|
44
50
|
{
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
Transform parent = component.transform.parent;
|
|
52
|
+
if (parent == null)
|
|
53
|
+
{
|
|
54
|
+
foundParent = false;
|
|
55
|
+
}
|
|
56
|
+
else if (isArray)
|
|
57
|
+
{
|
|
58
|
+
Component[] parentComponents = parent.GetComponentsInParent(
|
|
59
|
+
parentComponentType,
|
|
60
|
+
true
|
|
61
|
+
);
|
|
62
|
+
foundParent = 0 < parentComponents.Length;
|
|
47
63
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
Array correctTypedArray = Array.CreateInstance(
|
|
65
|
+
parentComponentType,
|
|
66
|
+
parentComponents.Length
|
|
67
|
+
);
|
|
68
|
+
Array.Copy(parentComponents, correctTypedArray, parentComponents.Length);
|
|
69
|
+
field.SetValue(component, correctTypedArray);
|
|
70
|
+
}
|
|
71
|
+
else if (
|
|
72
|
+
fieldType.IsGenericType
|
|
73
|
+
&& fieldType.GetGenericTypeDefinition() == typeof(List<>)
|
|
74
|
+
)
|
|
75
|
+
{
|
|
76
|
+
parentComponentType = fieldType.GenericTypeArguments[0];
|
|
77
|
+
Type constructedListType = typeof(List<>).MakeGenericType(
|
|
78
|
+
parentComponentType
|
|
79
|
+
);
|
|
80
|
+
IList instance = (IList)Activator.CreateInstance(constructedListType);
|
|
81
|
+
|
|
82
|
+
foundParent = false;
|
|
83
|
+
foreach (
|
|
84
|
+
Component parentComponent in parent.GetComponentsInParent(
|
|
85
|
+
parentComponentType,
|
|
86
|
+
true
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
{
|
|
90
|
+
instance.Add(parentComponent);
|
|
91
|
+
foundParent = true;
|
|
92
|
+
}
|
|
57
93
|
|
|
58
|
-
|
|
59
|
-
|
|
94
|
+
field.SetValue(component, instance);
|
|
95
|
+
}
|
|
96
|
+
else
|
|
60
97
|
{
|
|
61
|
-
|
|
62
|
-
|
|
98
|
+
Component childComponent = parent.GetComponentInParent(
|
|
99
|
+
parentComponentType,
|
|
100
|
+
true
|
|
101
|
+
);
|
|
102
|
+
foundParent = childComponent != null;
|
|
103
|
+
if (foundParent)
|
|
104
|
+
{
|
|
105
|
+
field.SetValue(component, childComponent);
|
|
106
|
+
}
|
|
63
107
|
}
|
|
64
|
-
|
|
65
|
-
field.SetValue(component, instance);
|
|
66
108
|
}
|
|
67
109
|
else
|
|
68
110
|
{
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
111
|
+
if (isArray)
|
|
112
|
+
{
|
|
113
|
+
Component[] parentComponents = component.GetComponentsInParent(
|
|
114
|
+
parentComponentType,
|
|
115
|
+
true
|
|
116
|
+
);
|
|
117
|
+
foundParent = 0 < parentComponents.Length;
|
|
118
|
+
|
|
119
|
+
Array correctTypedArray = Array.CreateInstance(
|
|
120
|
+
parentComponentType,
|
|
121
|
+
parentComponents.Length
|
|
122
|
+
);
|
|
123
|
+
Array.Copy(parentComponents, correctTypedArray, parentComponents.Length);
|
|
124
|
+
field.SetValue(component, correctTypedArray);
|
|
125
|
+
}
|
|
126
|
+
else if (
|
|
127
|
+
fieldType.IsGenericType
|
|
128
|
+
&& fieldType.GetGenericTypeDefinition() == typeof(List<>)
|
|
129
|
+
)
|
|
130
|
+
{
|
|
131
|
+
parentComponentType = fieldType.GenericTypeArguments[0];
|
|
132
|
+
Type constructedListType = typeof(List<>).MakeGenericType(
|
|
133
|
+
parentComponentType
|
|
134
|
+
);
|
|
135
|
+
IList instance = (IList)Activator.CreateInstance(constructedListType);
|
|
136
|
+
|
|
137
|
+
foundParent = false;
|
|
138
|
+
foreach (
|
|
139
|
+
Component parentComponent in component.GetComponentsInParent(
|
|
140
|
+
parentComponentType,
|
|
141
|
+
true
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
{
|
|
145
|
+
instance.Add(parentComponent);
|
|
146
|
+
foundParent = true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
field.SetValue(component, instance);
|
|
150
|
+
}
|
|
151
|
+
else
|
|
72
152
|
{
|
|
73
|
-
|
|
153
|
+
Component childComponent = component.GetComponentInParent(
|
|
154
|
+
parentComponentType,
|
|
155
|
+
true
|
|
156
|
+
);
|
|
157
|
+
foundParent = childComponent != null;
|
|
158
|
+
if (foundParent)
|
|
159
|
+
{
|
|
160
|
+
field.SetValue(component, childComponent);
|
|
161
|
+
}
|
|
74
162
|
}
|
|
75
163
|
}
|
|
76
164
|
|
|
77
165
|
if (!foundParent)
|
|
78
166
|
{
|
|
79
|
-
if (
|
|
80
|
-
ParentComponentAttribute
|
|
167
|
+
if (
|
|
168
|
+
field.GetCustomAttributes(typeof(ParentComponentAttribute), false)[0]
|
|
169
|
+
is ParentComponentAttribute { optional: false } _
|
|
170
|
+
)
|
|
81
171
|
{
|
|
82
172
|
component.LogError($"Unable to find parent component of type {fieldType}");
|
|
83
173
|
}
|
|
@@ -85,4 +175,4 @@
|
|
|
85
175
|
}
|
|
86
176
|
}
|
|
87
177
|
}
|
|
88
|
-
}
|
|
178
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Attributes
|
|
2
2
|
{
|
|
3
|
-
using Extension;
|
|
4
|
-
using JetBrains.Annotations;
|
|
5
3
|
using System;
|
|
6
4
|
using System.Collections;
|
|
7
5
|
using System.Collections.Generic;
|
|
8
6
|
using System.Linq;
|
|
9
7
|
using System.Reflection;
|
|
8
|
+
using Extension;
|
|
9
|
+
using JetBrains.Annotations;
|
|
10
10
|
using UnityEngine;
|
|
11
11
|
|
|
12
12
|
[AttributeUsage(AttributeTargets.Field)]
|
|
@@ -28,11 +28,15 @@
|
|
|
28
28
|
type =>
|
|
29
29
|
{
|
|
30
30
|
FieldInfo[] fields = type.GetFields(
|
|
31
|
-
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
31
|
+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
32
|
+
);
|
|
32
33
|
return fields
|
|
33
|
-
.Where(
|
|
34
|
+
.Where(field =>
|
|
35
|
+
Attribute.IsDefined(field, typeof(SiblingComponentAttribute))
|
|
36
|
+
)
|
|
34
37
|
.ToArray();
|
|
35
|
-
}
|
|
38
|
+
}
|
|
39
|
+
);
|
|
36
40
|
|
|
37
41
|
foreach (FieldInfo field in fields)
|
|
38
42
|
{
|
|
@@ -46,18 +50,26 @@
|
|
|
46
50
|
Component[] siblingComponents = component.GetComponents(siblingComponentType);
|
|
47
51
|
foundSibling = 0 < siblingComponents.Length;
|
|
48
52
|
|
|
49
|
-
Array correctTypedArray = Array.CreateInstance(
|
|
53
|
+
Array correctTypedArray = Array.CreateInstance(
|
|
54
|
+
siblingComponentType,
|
|
55
|
+
siblingComponents.Length
|
|
56
|
+
);
|
|
50
57
|
Array.Copy(siblingComponents, correctTypedArray, siblingComponents.Length);
|
|
51
58
|
field.SetValue(component, correctTypedArray);
|
|
52
59
|
}
|
|
53
|
-
else if (
|
|
60
|
+
else if (
|
|
61
|
+
fieldType.IsGenericType
|
|
62
|
+
&& fieldType.GetGenericTypeDefinition() == typeof(List<>)
|
|
63
|
+
)
|
|
54
64
|
{
|
|
55
65
|
siblingComponentType = fieldType.GenericTypeArguments[0];
|
|
56
66
|
Type constructedListType = typeof(List<>).MakeGenericType(siblingComponentType);
|
|
57
67
|
IList instance = (IList)Activator.CreateInstance(constructedListType);
|
|
58
68
|
|
|
59
69
|
foundSibling = false;
|
|
60
|
-
foreach (
|
|
70
|
+
foreach (
|
|
71
|
+
Component siblingComponent in component.GetComponents(siblingComponentType)
|
|
72
|
+
)
|
|
61
73
|
{
|
|
62
74
|
instance.Add(siblingComponent);
|
|
63
75
|
foundSibling = true;
|
|
@@ -67,7 +79,12 @@
|
|
|
67
79
|
}
|
|
68
80
|
else
|
|
69
81
|
{
|
|
70
|
-
if (
|
|
82
|
+
if (
|
|
83
|
+
component.TryGetComponent(
|
|
84
|
+
siblingComponentType,
|
|
85
|
+
out Component siblingComponent
|
|
86
|
+
)
|
|
87
|
+
)
|
|
71
88
|
{
|
|
72
89
|
foundSibling = true;
|
|
73
90
|
field.SetValue(component, siblingComponent);
|
|
@@ -80,8 +97,10 @@
|
|
|
80
97
|
|
|
81
98
|
if (!foundSibling)
|
|
82
99
|
{
|
|
83
|
-
if (
|
|
84
|
-
SiblingComponentAttribute
|
|
100
|
+
if (
|
|
101
|
+
field.GetCustomAttributes(typeof(SiblingComponentAttribute), false)[0]
|
|
102
|
+
is SiblingComponentAttribute { optional: false } _
|
|
103
|
+
)
|
|
85
104
|
{
|
|
86
105
|
component.LogError($"Unable to find sibling component of type {fieldType}");
|
|
87
106
|
}
|
|
@@ -89,4 +108,4 @@
|
|
|
89
108
|
}
|
|
90
109
|
}
|
|
91
110
|
}
|
|
92
|
-
}
|
|
111
|
+
}
|
|
@@ -92,9 +92,9 @@ namespace UnityHelpers.Core.Extension
|
|
|
92
92
|
|
|
93
93
|
object ValueFormat(object value)
|
|
94
94
|
{
|
|
95
|
-
if (value is Object obj
|
|
95
|
+
if (value is Object obj)
|
|
96
96
|
{
|
|
97
|
-
return obj.name;
|
|
97
|
+
return obj != null ? obj.name : null;
|
|
98
98
|
}
|
|
99
99
|
return value?.ToString();
|
|
100
100
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
namespace UnityHelpers.Tests.Attributes
|
|
2
|
+
{
|
|
3
|
+
using System.Collections;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using Components;
|
|
6
|
+
using Core.Attributes;
|
|
7
|
+
using UnityEngine;
|
|
8
|
+
using UnityEngine.Assertions;
|
|
9
|
+
using UnityEngine.TestTools;
|
|
10
|
+
|
|
11
|
+
public sealed class ChildComponentTests
|
|
12
|
+
{
|
|
13
|
+
[UnityTest]
|
|
14
|
+
public IEnumerator Nominal()
|
|
15
|
+
{
|
|
16
|
+
GameObject parent = new("Parent-ChildComponentTest", typeof(SpriteRenderer));
|
|
17
|
+
GameObject baseGameObject = new(
|
|
18
|
+
"Base-ChildComponentTest",
|
|
19
|
+
typeof(SpriteRenderer),
|
|
20
|
+
typeof(ExpectChildSpriteRenderers)
|
|
21
|
+
);
|
|
22
|
+
baseGameObject.transform.SetParent(parent.transform);
|
|
23
|
+
GameObject childLevel1 = new("ChildLevel1", typeof(SpriteRenderer));
|
|
24
|
+
childLevel1.transform.SetParent(baseGameObject.transform);
|
|
25
|
+
GameObject childLevel2 = new("ChildLevel2", typeof(SpriteRenderer));
|
|
26
|
+
childLevel2.transform.SetParent(childLevel1.transform);
|
|
27
|
+
GameObject childLevel2Point1 = new("ChildLevel2.1", typeof(SpriteRenderer));
|
|
28
|
+
childLevel2Point1.transform.SetParent(childLevel1.transform);
|
|
29
|
+
|
|
30
|
+
ExpectChildSpriteRenderers expect =
|
|
31
|
+
baseGameObject.GetComponent<ExpectChildSpriteRenderers>();
|
|
32
|
+
expect.AssignChildComponents();
|
|
33
|
+
|
|
34
|
+
Assert.AreEqual(4, expect.exclusiveChildrenArray.Length);
|
|
35
|
+
Assert.AreEqual(4, expect.exclusiveChildrenList.Count);
|
|
36
|
+
Assert.IsTrue(
|
|
37
|
+
expect.exclusiveChildrenList.Contains(baseGameObject.GetComponent<SpriteRenderer>())
|
|
38
|
+
);
|
|
39
|
+
Assert.IsTrue(
|
|
40
|
+
expect.exclusiveChildrenList.Contains(childLevel1.GetComponent<SpriteRenderer>())
|
|
41
|
+
);
|
|
42
|
+
Assert.IsTrue(
|
|
43
|
+
expect.exclusiveChildrenList.Contains(childLevel2.GetComponent<SpriteRenderer>())
|
|
44
|
+
);
|
|
45
|
+
Assert.IsTrue(
|
|
46
|
+
expect.exclusiveChildrenList.Contains(
|
|
47
|
+
childLevel2Point1.GetComponent<SpriteRenderer>()
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
Assert.IsTrue(
|
|
51
|
+
expect.exclusiveChildrenList.ToHashSet().SetEquals(expect.exclusiveChildrenArray)
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
Assert.AreEqual(3, expect.inclusiveChildrenArray.Length);
|
|
55
|
+
Assert.AreEqual(3, expect.inclusiveChildrenList.Count);
|
|
56
|
+
|
|
57
|
+
Assert.IsTrue(
|
|
58
|
+
expect.inclusiveChildrenList.Contains(childLevel1.GetComponent<SpriteRenderer>())
|
|
59
|
+
);
|
|
60
|
+
Assert.IsTrue(
|
|
61
|
+
expect.inclusiveChildrenList.Contains(childLevel2.GetComponent<SpriteRenderer>())
|
|
62
|
+
);
|
|
63
|
+
Assert.IsTrue(
|
|
64
|
+
expect.inclusiveChildrenList.Contains(
|
|
65
|
+
childLevel2Point1.GetComponent<SpriteRenderer>()
|
|
66
|
+
)
|
|
67
|
+
);
|
|
68
|
+
Assert.IsTrue(
|
|
69
|
+
expect.inclusiveChildrenList.ToHashSet().SetEquals(expect.inclusiveChildrenArray)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
Assert.IsTrue(expect.exclusiveChild != null);
|
|
73
|
+
Assert.AreEqual(expect.GetComponent<SpriteRenderer>(), expect.exclusiveChild);
|
|
74
|
+
|
|
75
|
+
Assert.IsTrue(expect.inclusiveChild != null);
|
|
76
|
+
Assert.AreEqual(childLevel1.GetComponent<SpriteRenderer>(), expect.inclusiveChild);
|
|
77
|
+
|
|
78
|
+
yield break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
namespace UnityHelpers.Tests.Attributes.Components
|
|
2
|
+
{
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using Core.Attributes;
|
|
5
|
+
using UnityEngine;
|
|
6
|
+
|
|
7
|
+
[DisallowMultipleComponent]
|
|
8
|
+
public sealed class ExpectChildSpriteRenderers : MonoBehaviour
|
|
9
|
+
{
|
|
10
|
+
[ChildComponent(onlyDescendents = true)]
|
|
11
|
+
public List<SpriteRenderer> inclusiveChildrenList;
|
|
12
|
+
|
|
13
|
+
[ChildComponent(onlyDescendents = false)]
|
|
14
|
+
public List<SpriteRenderer> exclusiveChildrenList;
|
|
15
|
+
|
|
16
|
+
[ChildComponent(onlyDescendents = true)]
|
|
17
|
+
public SpriteRenderer[] inclusiveChildrenArray;
|
|
18
|
+
|
|
19
|
+
[ChildComponent(onlyDescendents = false)]
|
|
20
|
+
public SpriteRenderer[] exclusiveChildrenArray;
|
|
21
|
+
|
|
22
|
+
[ChildComponent(onlyDescendents = true)]
|
|
23
|
+
public SpriteRenderer inclusiveChild;
|
|
24
|
+
|
|
25
|
+
[ChildComponent(onlyDescendents = false)]
|
|
26
|
+
public SpriteRenderer exclusiveChild;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
namespace UnityHelpers.Tests.Attributes.Components
|
|
2
|
+
{
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using Core.Attributes;
|
|
5
|
+
using UnityEngine;
|
|
6
|
+
|
|
7
|
+
[DisallowMultipleComponent]
|
|
8
|
+
public sealed class ExpectParentSpriteRenderers : MonoBehaviour
|
|
9
|
+
{
|
|
10
|
+
[ParentComponent(onlyAncestors = true)]
|
|
11
|
+
public List<SpriteRenderer> inclusiveParentList;
|
|
12
|
+
|
|
13
|
+
[ParentComponent(onlyAncestors = false)]
|
|
14
|
+
public List<SpriteRenderer> exclusiveParentList;
|
|
15
|
+
|
|
16
|
+
[ParentComponent(onlyAncestors = true)]
|
|
17
|
+
public SpriteRenderer[] inclusiveParentArray;
|
|
18
|
+
|
|
19
|
+
[ParentComponent(onlyAncestors = false)]
|
|
20
|
+
public SpriteRenderer[] exclusiveParentArray;
|
|
21
|
+
|
|
22
|
+
[ParentComponent(onlyAncestors = true)]
|
|
23
|
+
public SpriteRenderer inclusiveParent;
|
|
24
|
+
|
|
25
|
+
[ParentComponent(onlyAncestors = false)]
|
|
26
|
+
public SpriteRenderer exclusiveParent;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
namespace UnityHelpers.Tests.Attributes
|
|
2
|
+
{
|
|
3
|
+
using System.Collections;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using Components;
|
|
6
|
+
using Core.Attributes;
|
|
7
|
+
using UnityEngine;
|
|
8
|
+
using UnityEngine.Assertions;
|
|
9
|
+
using UnityEngine.TestTools;
|
|
10
|
+
|
|
11
|
+
public sealed class ParentComponentTests
|
|
12
|
+
{
|
|
13
|
+
[UnityTest]
|
|
14
|
+
public IEnumerator Nominal()
|
|
15
|
+
{
|
|
16
|
+
GameObject root = new("PartComponentTest - Root", typeof(SpriteRenderer));
|
|
17
|
+
GameObject parentLevel1 = new("ParentLevel1", typeof(SpriteRenderer));
|
|
18
|
+
parentLevel1.transform.SetParent(root.transform);
|
|
19
|
+
GameObject parentLevel2 = new("ParentLevel2", typeof(SpriteRenderer));
|
|
20
|
+
parentLevel2.transform.SetParent(parentLevel1.transform);
|
|
21
|
+
GameObject parentLevel3 = new(
|
|
22
|
+
"ParentLevel3",
|
|
23
|
+
typeof(SpriteRenderer),
|
|
24
|
+
typeof(ExpectParentSpriteRenderers)
|
|
25
|
+
);
|
|
26
|
+
parentLevel3.transform.SetParent(parentLevel2.transform);
|
|
27
|
+
|
|
28
|
+
ExpectParentSpriteRenderers expect =
|
|
29
|
+
parentLevel3.GetComponent<ExpectParentSpriteRenderers>();
|
|
30
|
+
expect.AssignParentComponents();
|
|
31
|
+
|
|
32
|
+
Assert.AreEqual(4, expect.exclusiveParentList.Count);
|
|
33
|
+
Assert.IsTrue(
|
|
34
|
+
expect.exclusiveParentList.Contains(parentLevel3.GetComponent<SpriteRenderer>())
|
|
35
|
+
);
|
|
36
|
+
Assert.IsTrue(
|
|
37
|
+
expect.exclusiveParentList.Contains(parentLevel2.GetComponent<SpriteRenderer>())
|
|
38
|
+
);
|
|
39
|
+
Assert.IsTrue(
|
|
40
|
+
expect.exclusiveParentList.Contains(parentLevel1.GetComponent<SpriteRenderer>())
|
|
41
|
+
);
|
|
42
|
+
Assert.IsTrue(expect.exclusiveParentList.Contains(root.GetComponent<SpriteRenderer>()));
|
|
43
|
+
Assert.IsTrue(
|
|
44
|
+
expect.exclusiveParentList.ToHashSet().SetEquals(expect.exclusiveParentArray)
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
Assert.AreEqual(3, expect.inclusiveParentList.Count);
|
|
48
|
+
Assert.IsTrue(
|
|
49
|
+
expect.inclusiveParentList.Contains(parentLevel2.GetComponent<SpriteRenderer>())
|
|
50
|
+
);
|
|
51
|
+
Assert.IsTrue(
|
|
52
|
+
expect.inclusiveParentList.Contains(parentLevel1.GetComponent<SpriteRenderer>())
|
|
53
|
+
);
|
|
54
|
+
Assert.IsTrue(expect.inclusiveParentList.Contains(root.GetComponent<SpriteRenderer>()));
|
|
55
|
+
Assert.IsTrue(
|
|
56
|
+
expect.inclusiveParentList.ToHashSet().SetEquals(expect.inclusiveParentArray)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
Assert.IsTrue(expect.exclusiveParent != null);
|
|
60
|
+
Assert.AreEqual(expect.GetComponent<SpriteRenderer>(), expect.exclusiveParent);
|
|
61
|
+
|
|
62
|
+
Assert.IsTrue(expect.inclusiveParent != null);
|
|
63
|
+
Assert.AreEqual(parentLevel2.GetComponent<SpriteRenderer>(), expect.inclusiveParent);
|
|
64
|
+
|
|
65
|
+
yield break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|