Skip to content

Commit 1ceb76c

Browse files
update
Made some minor adjustments while writing the base test for ComponentController.
1 parent 1fce513 commit 1ceb76c

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

com.unity.netcode.gameobjects/Runtime/Components/Helpers/ComponentController.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public class ComponentControllerEntry
3939
/// </summary>
4040
/// <remarks>
4141
/// This will synchronize the enabled or disabled state of the <see cref="Component"/>s with
42-
/// connected and late joining clients.
42+
/// connected and late joining clients.<br />
43+
/// This class provides the basic functionality to synchronizing components' enabled state.<br />
44+
/// It is encouraged to create custom derived versions of this class to provide any additional
45+
/// functionality required for your project specific needs.
4346
/// </remarks>
4447
public class ComponentController : NetworkBehaviour
4548
{
@@ -60,7 +63,7 @@ public class ComponentController : NetworkBehaviour
6063
/// </summary>
6164
public bool EnabledState => m_IsEnabled.Value;
6265

63-
private List<ComponentControllerEntry> m_ValidComponents = new List<ComponentControllerEntry>();
66+
internal List<ComponentControllerEntry> ValidComponents = new List<ComponentControllerEntry>();
6467
private NetworkVariable<bool> m_IsEnabled = new NetworkVariable<bool>();
6568

6669
#if UNITY_EDITOR
@@ -144,7 +147,16 @@ protected virtual void OnValidate()
144147
/// </summary>
145148
protected virtual void Awake()
146149
{
150+
ValidComponents.Clear();
151+
152+
// If no components then don't try to initialize.
153+
if (Components == null)
154+
{
155+
return;
156+
}
157+
147158
var emptyEntries = 0;
159+
148160
foreach (var entry in Components)
149161
{
150162
if (entry == null)
@@ -156,20 +168,16 @@ protected virtual void Awake()
156168
if (propertyInfo != null && propertyInfo.PropertyType == typeof(bool))
157169
{
158170
entry.PropertyInfo = propertyInfo;
159-
m_ValidComponents.Add(entry);
171+
ValidComponents.Add(entry);
160172
}
161173
else
162174
{
163-
Debug.LogWarning($"{name} does not contain a public enable property! (Ignoring)");
175+
NetworkLog.LogWarning($"{name} does not contain a public enable property! (Ignoring)");
164176
}
165177
}
166178
if (emptyEntries > 0)
167179
{
168-
Debug.LogWarning($"{name} has {emptyEntries} emtpy(null) entries in the {nameof(Components)} list!");
169-
}
170-
else
171-
{
172-
Debug.Log($"{name} has {m_ValidComponents.Count} valid {nameof(Component)} entries.");
180+
NetworkLog.LogWarning($"{name} has {emptyEntries} emtpy(null) entries in the {nameof(Components)} list!");
173181
}
174182

175183
// Apply the initial state of all components this instance is controlling.
@@ -216,7 +224,7 @@ private void OnEnabledChanged(bool previous, bool current)
216224
/// </summary>
217225
private void InitializeComponents()
218226
{
219-
foreach (var entry in m_ValidComponents)
227+
foreach (var entry in ValidComponents)
220228
{
221229
// If invert enabled is true, then use the inverted value passed in.
222230
// Otherwise, directly apply the value passed in.
@@ -231,7 +239,7 @@ private void InitializeComponents()
231239
/// <param name="enabled">the state update to apply</param>
232240
private void ApplyEnabled(bool enabled)
233241
{
234-
foreach (var entry in m_ValidComponents)
242+
foreach (var entry in ValidComponents)
235243
{
236244
// If invert enabled is true, then use the inverted value passed in.
237245
// Otherwise, directly apply the value passed in.

0 commit comments

Comments
 (0)