VContainer
VContainer copied to clipboard
feature: Add protected FindParent method
trafficstars
This will allow implementors the opportunity to override runtime Find behavior. This is particularly useful for projects that implement multiple potential parent scopes and need a way to disambiguate them.
For example, some child scopes might want to specifically find ancestors up their transform tree:
public class ParentedLifetimeScope : LifetimeScope {
protected override LifetimeScope FindParent(Type type) {
LifetimeScope[] allScopes = FindObjectsByType(type, FindObjectsSortMode.None).Cast<LifetimeScope>().ToArray();
return allScopes.First(x => IsAncestor(x.gameObject));
}
public bool IsAncestor(GameObject target) {
var parent = this.transform.parent;
while (parent != null)
{
if (parent == target.transform)
{
return true;
}
parent = parent.parent;
}
return false;
}
}
All unit tests pass with this change and because the implementation is a virtual method that defaults to the previous functionality, current behavior is unchanged.