In C# there are extension methods that can be written such as:
// Create an extension for string
public static class MyExtension
{
public static bool IsCaptialized(this string value)
{
return Char.IsUpper(value[0]);
}
}
// And then use it
void TestExtension()
{
string shouldBeCapitalized = "Capitalized";
Debug.Log(shouldBeCapitalized.IsCaptialized());
}
- Can I write extension methods in JS?
- Can I use extension methods written in C# with JS?
Progress update.
As I have been trying some C#/JS since I asked the question, it appears that JS does not recognize the extensions I wrote in C#. I placed the extensions in Standard Assets folder so that it would compile before the JS. That leaves me to think there isn't any way in JS to use extension methods and as such there is no way to write extension methods in JS, at least not for the present.
Since no other answers have been posted and I haven't been able to find any solution through my own fiddling, I draw the conclusion that it's a simple case;
Javascript for Unity3D does not support extension methods.
There are no extension methods in JS, or in Unity’s version of JS (which isn’t traditional JS).
JS won’t recognize C# extension methods because C# extension methods are not real artefacts added to the class, they are syntactic sugar. In essence they are just static methods. The C# compiler, as a feature of the language, knows how to “use” them from an object context given the right syntax. But as said - it’s just a bit of magic, they are not real. Hence they are not visible as more than static methods from a cross-language point of view.
Generally speaking, if you want a “quick and easy” language to program in, use JS. If you come from a “heavier” computing background and would like to have the full power of a strong mainstream language at your disposal, use C#. For larger projects I’d recommend C#.
Either way don’t expect to get the same from both languages, they are different.