XDracam / unity-corelibrary

Collection of classes and extension methods that make life with Unity3D more comfortable
MIT License
15 stars 3 forks source link

IfAbsentCompute<T>(ref T field, Action<T> getter) #15

Closed XDracam closed 5 years ago

XDracam commented 5 years ago

We want a method IfAbsentCompute<T>(ref T field, Action<T> getter) (or similar) which takes a reference to a field and if that field is equal to it's default (or unity-null) then the getter will be called and the field will be set to that result.

The main use case for this is in Start or Awake methods of cloned or .Instantiated game objects, for which many fields would already be present, and then unnecessesarily recreated.