- Home /
Can someone translate this to Javascript?
I'm making a leaderbord (http://forum.unity3d.com/threads/134939-Free-easy-to-implement-LeaderBoard)
I know Javascript really good but I can't translate this part:
string[] rows = this.highScores.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries); foreach (string r in rows) { string[] values = r.Split(new char[] {'|'}, System.StringSplitOptions.RemoveEmptyEntries); GUILayout.BeginHorizontal(); GUILayout.Label(values[0], width200); GUILayout.Label(values[1], width200); GUILayout.EndHorizontal(); }
Thanks!
for any help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting.
I found these links useful for converting between C# and JS :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
Thanks! btw I forced myself to make the leaderboard in C# now xd
Answer by dukearena · Nov 27, 2012 at 11:01 AM
C# syntax and Javascript syntax are different in some points, especially in variables declaretion.. This is the translation:
var rows : string[] = highScores.Split('n', System.StringSplitOptions.RemoveEmptyEntries);
for(var i = 0; i<rows.length; i++) {
var r : string = rows[i];
var values : string[] = r.Split('|', System.StringSplitOptions.RemoveEmptyEntries);
GUILayout.BeginHorizontal();
GUILayout.Label(values[0], width200);
GUILayout.Label(values[1], width200);
GUILayout.EndHorizontal();
}
I haven't tried it in Unity but it should work :D
I explain the differences:
1 - Javascript doesn't have a foreach.. There are many methods for build it but it's more simple to use the simple for statement.
2 - Javascript have a long declaration.. It needs "var varname : type", C# only "type varname"
Please, let me know if I've written some errors :D
Wow thank you so much! :D I'll try it out when I get home :)
Your answer
Follow this Question
Related Questions
Translate Unity-scripts to uScript 1 Answer
java to C# conversion 1 Answer
Camera Effect + Translating US to C# 3 Answers
translate C# with => to unityscript 2 Answers
Converting Variable to C# 1 Answer