XDracam / unity-corelibrary

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

Advanced Is<T>: An overload which returns the query result as an out parameter #27

Closed Eregerog closed 5 years ago

Eregerog commented 5 years ago

I implemented an overload for Is, which enables the following: Instead of writing

if(this.Is<Collider>(Search.InChildren)){
 var collider = this.As<Collider>(Search.InChildren);
 collider.trigger = true;
}

there is an overload for all the Is methods, that is used like the following (only possible starting c#7 and upwards - it is a little less useful before)

if(this.Is<Collider>(out var result, Search.InChildren)){
  result.trigger = true;
}

Saves a line and an unneccesarry query, without really sacrificing readability. It kinda works like if(foo is MyClass asMyClass)

To be done by XDracam: Add documentation of a use case to the markdown docu, and compile it. You can use the example given in the xml docu of my Is method