Skip to content

Extensions

Andrew Rumak edited this page Sep 15, 2023 · 7 revisions

There are lots of them 😅

I will fill this page someday...

In the meantime, you can enable Intellisense to display self-documentation of these methods by following these steps: In Unity Editor, open Edit > Preferences > External Tools and enable "Generate .csproj files for" Registry packages and Git packages, then click "Regenerate project files". The next time you open Visual Studio, Intellisense will show you comments for MyBox's extension methods.

MyAlgorithms

Source
Some sugary extensions for functional-style code

Cast, Is, As – Check, Cast of Convert source object to a different type

if (!enemy.Is<BossEnemy>()) .. instead of if (!(enemy is BossEnemy))

// Works with IConvertible types
1f.Cast<int>().InRange(someRangedInt) .. instead of ((int)1f).InRange(someRangedInt)

// As is exception-safe, instead of InvalidCastException it will return null
enemy.As<BossEnemy>()?.Pipe(b => { /* continue interacting with boss */ });

Pipe and PipeKeep – Take an object and pass it as an argument to a function

// Flow-style functions call
Func1(originalArgument).Pipe(Func2).Pipe(Func3).Pipe(Func4);
//Instead of 
Func4(Func3(Func2(Func1(originalArgument))));

// With null check - call InitializeCheckpoint(Checkpoint c) if Checkpoint is found
GetComponentInChildren<Checkpoint>()?.Pipe(InitializeCheckpoint);

// And with PipeKeep:
var initializedCheckpoint = GetComponentInChildren<Checkpoint>()?.Pipe(InitializeCheckpoint);