Converting from UnityScript to C# (c sharp) August 30, 2009
Here's a situation most Unity3d developers run into: you have scripts in both C# and unity JavaScript, and you can't get them to work together even after you figure out the bizarre compilation order because you want bi-directional communication. What to do? Unfortunately, it seems as though the only thing you can do is to convert the UnityScript (JavaScript) to C#. Unfortunately, there is no completely automated way to do this.
Running the following search/replace in vim (what else?) in the following order gets you about 85% of the way there:
Other things that the above find/replaces won't catch are: foreach/for(a in b) dynamicly typed variables, variables whos types are implicit.. ie: var a = SomeFunction();, and it won't currently ensure that you have an 'f' following your floats -- 2.0f instead of 2.0.
As I mentioned at the Unity3d forums:
%s/var \(.*\) : \(.*\) = \(.*\);/\2 \1 = \3;/g
%s/var \(.*\) : \(.*\);/\2 \1;/g
%s/function \(.*\)() : \(.*\)/public \2 \1()/g
%s/function \(.*\)/public void \1
%s/boolean/bool
more of a PITA is getting around c#'s inability to set variables on member properties ( ie: foo.color.a becomes tmpclr = foo.color; tmpclr.a= 0.2f; foo.color = tmpclr); I decided to create class-specific private member variables to avoid dynamic variable allocation (so we dont have as much garbage to collect.)Finally, the last thing to worry about is how Unity3d implements coroutines in C#.. but I'll save that for another blog post.
EDIT (09/18/09): More resources can be found at the excellent 8bit Feed.