// MIT License - Copyright (c) 2023 wallstop
// Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE
namespace WallstopStudios.UnityHelpers.Core.Attributes
{
using System;
using UnityEngine;
///
/// Extension methods for automatically assigning components based on relational attributes.
///
public static class RelationalComponentExtensions
{
///
/// Assigns all relational components (parent, sibling, and child components) to fields marked with the corresponding attributes.
///
/// The component on which to perform the assignment.
///
/// This is a convenience method that calls ,
/// , and
/// in sequence.
///
/// Fields must be marked with , , or
/// for automatic assignment.
///
/// Call from Awake() or OnEnable() so dependent code has references ready.
///
/// To avoid any first-use overhead from generating reflection helpers lazily, you can explicitly
/// pre-initialize all relational component reflection caches using
/// .
/// Consider calling it during a loading/bootstrap phase.
/// Null handling: If the component is null, this will cause a .
/// Thread-safety: Not thread-safe; Unity component access must occur on the main thread.
/// Performance: O(n*m) where n is the number of attributed fields and m is the search space for each component.
///
///
///
///
///
///
///
public static void AssignRelationalComponents(this Component component)
{
if (component == null)
{
return;
}
Type componentType = component.GetType();
ParentComponentExtensions.AssignParentComponents(
component,
ParentComponentExtensions.GetOrCreateFields(componentType)
);
SiblingComponentExtensions.AssignSiblingComponents(
component,
SiblingComponentExtensions.GetOrCreateFields(componentType)
);
ChildComponentExtensions.AssignChildComponents(
component,
ChildComponentExtensions.GetOrCreateFields(componentType)
);
}
}
}