feat: GetOrAddBehaviour with fallback type added
This commit is contained in:
parent
a3b03efd47
commit
cd30047e4a
@ -39,6 +39,19 @@ public static class BehaviourControllerExtensions
|
||||
public static T GetOrAddBehaviour<T>(this IBehaviourController behaviourController, params object?[]? args) where T : class, IBehaviour
|
||||
=> behaviourController.GetBehaviour<T>() ?? behaviourController.AddBehaviour<T>(args);
|
||||
|
||||
/// <summary>
|
||||
/// Gets an existing <see cref="IBehaviour"/> of the specified type, or adds and returns the fallback type if it doesn't exist.
|
||||
/// </summary>
|
||||
/// <typeparam name="TOriginal">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||
/// <typeparam name="TFallback">The type of <see cref="IBehaviour"/> to add. It must be assignable from <typeparamref name="TOriginal"/></typeparam>
|
||||
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to search in.</param>
|
||||
/// <param name="args">Optional arguments to pass to the constructor of the <see cref="IBehaviour"/> if a new one is added.</param>
|
||||
/// <returns>The existing or newly added <see cref="IBehaviour"/> of the specified type.</returns>
|
||||
public static TOriginal GetOrAddBehaviour<TOriginal, TFallback>(this IBehaviourController behaviourController, params object?[]? args)
|
||||
where TOriginal : class
|
||||
where TFallback : class, IBehaviour, TOriginal
|
||||
=> behaviourController.GetBehaviour<TOriginal>() ?? behaviourController.AddBehaviour<TFallback>(args);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IUniverseObject"/>'s parents recursively.
|
||||
/// </summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user