The InitArgs class is the bridge through which initialization arguments are passed from the classes that create instances to the objects that are being created (called clients).
Note that in most cases you don’t need to use InitArgs directly; all of the various methods provided for initializing instances with arguments already handle this for you behind the scenes. But for those less common scenarios where you want to write your own code that makes use of the InitArgs you can find details about its inner workings below.
InitArgs.Set
The InitArgs.Set method can be used to store one to six arguments for a client of a specific type, which the client can subsequently receive during its initialization.
The client class must implement an IArgs<T…> interface with generic argument types matching the types of the parameters being passed in order for it to be targetable by the InitArgs.Set method.
InitArgs.Set<TClient, TArgument>(argument);
InitArgs.TryGet
The InitArgs.TryGet method can be used by a client object to retrieve arguments stored in InitArgs during its initialization phase.
if(InitArgs.TryGet(Context.Awake, this, out TArgument argument)) { Init(argument); }
Use the Context argument to specify the method context from which InitArgs is being called, for example Context.Awake when calling during the Awake event or Context.Reset when calling during the Reset event.
InitArgs.Clear
Use InitArgs.Clear to release the arguments stored for a client from memory. This can be done by the class that provided the arguments for the client, after the client has been initialized.
If the stored arguments were never retrieved by the client, then InitArgs.Clear will return true. If this happens, it’s probably appropriate to log an error or throw an exception.