Converting C# to JavaScript

Hey hows it goin everybody!
I need your help,i need a script that is in C# translated to the JavaScript.
I got no skills at the C#.
So without further adue,here it is:

using UnityEngine;
using System.Collections;

public class ADVANCEDMAINMENU : MonoBehaviour {

private bool showOptions = false;
public float shadowDrawDistance;
public int ResX;
public int ResY;
public bool Fullscreen;
// Use this for initialization
void Start () {
showOptions = false;
}

// Update is called once per frame
void Update () {

}

void OnGUI() {
if(GUI.Button(new Rect(500, 100, 300, 100), “Start Game”)) {
Application.LoadLevel(1);
}
if(GUI.Button(new Rect(500, 210, 300, 100), “Quit Game”)) {
Application.Quit();
}
if(GUI.Button(new Rect(500, 320, 300, 100), “Options Menu”)) {
showOptions = true;
}
if(showOptions == true) {
//INCREASE QUALITY PRESET
if(GUI.Button(new Rect(810, 100, 300, 100), “Increase Quality”)) {
QualitySettings.IncreaseLevel();
Debug.Log (“Increased quality”);

}
//DECREASE QUALITY PRESET
if(GUI.Button(new Rect(810, 210, 300, 100), “Decrease Quality”)) {
QualitySettings.DecreaseLevel();
Debug.Log (“Decreased quality”);
}
//0 X AA SETTINGS
if(GUI.Button(new Rect(810, 320, 65, 100), “No AA”)) {
QualitySettings.antiAliasing = 0;
Debug.Log (“0 AA”);
}
//2 X AA SETTINGS
if(GUI.Button(new Rect(879, 320, 65, 100), “2x AA”)) {
QualitySettings.antiAliasing = 2;
Debug.Log (“2 x AA”);
}
//4 X AA SETTINGS
if(GUI.Button(new Rect(954, 320, 65, 100), “4x AA”)) {
QualitySettings.antiAliasing = 4;
Debug.Log (“4 x AA”);
}
//8 x AA SETTINGS
if(GUI.Button(new Rect(1028, 320, 65, 100), “8x AA”)) {
QualitySettings.antiAliasing = 8;
Debug.Log (“8 x AA”);
}
//TRIPLE BUFFERING SETTINGS
if(GUI.Button(new Rect(810, 430, 140, 100), “Triple Buffering On”)) {
QualitySettings.maxQueuedFrames = 3;
Debug.Log (“Triple buffering on”);
}
if(GUI.Button(new Rect(955, 430, 140, 100), “Triple Buffering Off”)) {
QualitySettings.maxQueuedFrames = 0;
Debug.Log (“Triple buffering off”);
}
//ANISOTROPIC FILTERING SETTINGS
if(GUI.Button(new Rect(190, 100, 300, 100), “Anisotropic Filtering On”)) {
QualitySettings.anisotropicFiltering = AnisotropicFiltering.ForceEnable;
Debug.Log (“Force enable anisotropic filtering!”);
}
if(GUI.Button(new Rect(190, 210, 300, 100), “Anisotropic Filtering Off”)) {
QualitySettings.anisotropicFiltering = AnisotropicFiltering.Disable;
Debug.Log (“Disable anisotropic filtering!”);
}
//RESOLUTION SETTINGS
//60Hz
if(GUI.Button(new Rect(190, 320, 300, 100), “60Hz”)) {
Screen.SetResolution(ResX, ResY, Fullscreen, 60);
Debug.Log (“60Hz”);
}
//120Hz
if(GUI.Button(new Rect(190, 430, 300, 100), “120Hz”)) {
Screen.SetResolution(ResX, ResY, Fullscreen, 120);
Debug.Log (“120Hz”);
}
//1080p
if(GUI.Button(new Rect(500, 430, 93, 100), “1080p”)) {
Screen.SetResolution(1920, 1080, Fullscreen);
ResX = 1920;
ResY = 1080;
Debug.Log (“1080p”);
}
//720p
if(GUI.Button(new Rect(596, 430, 93, 100), “720p”)) {
Screen.SetResolution(1280, 720, Fullscreen);
ResX = 1280;
ResY = 720;
Debug.Log (“720p”);
}
//480p
if(GUI.Button(new Rect(692, 430, 93, 100), “480p”)) {
Screen.SetResolution(640, 480, Fullscreen);
ResX = 640;
ResY = 480;
Debug.Log (“480p”);
}
if(GUI.Button(new Rect(500, 0, 140, 100), “Vsync On”)) {
QualitySettings.vSyncCount = 1;
}
if(GUI.Button(new Rect(645, 0, 140, 100), “Vsync Off”)) {
QualitySettings.vSyncCount = 0;
}
}
}
}

#pragma strict

private var showOptions = false;
var shadowDrawDistance : float;
var ResX : int;
varResY : int;
var Fullscreen : bool;

function Start () {
showOptions = false;
}

function Update () {

}

function OnGUI() {
if(GUI.Button(Rect(500, 100, 300, 100), "Start Game")) {
Application.LoadLevel(1);
}
if(GUI.Button(Rect(500, 210, 300, 100), "Quit Game")) {
Application.Quit();
}
if(GUI.Button(Rect(500, 320, 300, 100), "Options Menu")) {
showOptions = true;
}
if(showOptions == true) {
//INCREASE QUALITY PRESET
if(GUI.Button(Rect(810, 100, 300, 100), "Increase Quality")) {
QualitySettings.IncreaseLevel();
Debug.Log ("Increased quality");

}
//DECREASE QUALITY PRESET
if(GUI.Button(Rect(810, 210, 300, 100), "Decrease Quality")) {
QualitySettings.DecreaseLevel();
Debug.Log ("Decreased quality");
}
//0 X AA SETTINGS
if(GUI.Button(Rect(810, 320, 65, 100), "No AA")) {
QualitySettings.antiAliasing = 0;
Debug.Log ("0 AA");
}
//2 X AA SETTINGS
if(GUI.Button(Rect(879, 320, 65, 100), "2x AA")) {
QualitySettings.antiAliasing = 2;
Debug.Log ("2 x AA");
}
//4 X AA SETTINGS
if(GUI.Button(Rect(954, 320, 65, 100), "4x AA")) {
QualitySettings.antiAliasing = 4;
Debug.Log ("4 x AA");
}
//8 x AA SETTINGS
if(GUI.Button(Rect(1028, 320, 65, 100), "8x AA")) {
QualitySettings.antiAliasing = 8;
Debug.Log ("8 x AA");
}
//TRIPLE BUFFERING SETTINGS
if(GUI.Button(Rect(810, 430, 140, 100), "Triple Buffering On")) {
QualitySettings.maxQueuedFrames = 3;
Debug.Log ("Triple buffering on");
}
if(GUI.Button(Rect(955, 430, 140, 100), "Triple Buffering Off")) {
QualitySettings.maxQueuedFrames = 0;
Debug.Log ("Triple buffering off");
}
//ANISOTROPIC FILTERING SETTINGS
if(GUI.Button(Rect(190, 100, 300, 100), "Anisotropic Filtering On")) {
QualitySettings.anisotropicFiltering = AnisotropicFiltering.ForceEnable;
Debug.Log ("Force enable anisotropic filtering!");
}
if(GUI.Button(Rect(190, 210, 300, 100), "Anisotropic Filtering Off")) {
QualitySettings.anisotropicFiltering = AnisotropicFiltering.Disable;
Debug.Log ("Disable anisotropic filtering!");
}
//RESOLUTION SETTINGS
//60Hz
if(GUI.Button(Rect(190, 320, 300, 100), "60Hz")) {
Screen.SetResolution(ResX, ResY, Fullscreen, 60);
Debug.Log ("60Hz");
}
//120Hz
if(GUI.Button(Rect(190, 430, 300, 100), "120Hz")) {
Screen.SetResolution(ResX, ResY, Fullscreen, 120);
Debug.Log ("120Hz");
}
//1080p
if(GUI.Button(Rect(500, 430, 93, 100), "1080p")) {
Screen.SetResolution(1920, 1080, Fullscreen);
ResX = 1920;
ResY = 1080;
Debug.Log ("1080p");
}
//720p
if(GUI.Button(Rect(596, 430, 93, 100), "720p")) {
Screen.SetResolution(1280, 720, Fullscreen);
ResX = 1280;
ResY = 720;
Debug.Log ("720p");
}
//480p
if(GUI.Button(Rect(692, 430, 93, 100), "480p")) {
Screen.SetResolution(640, 480, Fullscreen);
ResX = 640;
ResY = 480;
Debug.Log ("480p");
}
if(GUI.Button(Rect(500, 0, 140, 100), "Vsync On")) {
QualitySettings.vSyncCount = 1;
}
if(GUI.Button(Rect(645, 0, 140, 100), "Vsync Off")) {
QualitySettings.vSyncCount = 0;
}
}
}

That should work.
If not: check for spelling misstakes and if it still doesn’t work then delete the #pragma strict at the beginning.
By the way… Why the hell do you have an update function if it’s empty?

There are small differences between JavaScript and C#.

  1. When writing function. In C# it’s written as “void”. In Javascript it’s written as “function”
  2. When declaring a variable (e.g eight = 8) - C# puts it this way - int eight = 8. Javascript is = var eight : int = 8.

There are many more, but these are main differences between them.

Hope this will help you converting C# to Javascript by yourself in the future.

No. C# does not have any function keyword at all. Instead it uses the type (which IMO is a poor design decision which constantly leads to misunderstandings like this, but there you go). If a function returns void then you write void. If it returns int then you write int. If it returns GameObject then you write GameObject. Etc. You can optionally write the return type for Unityscript functions too, but in most cases it’s unnecessary since the compiler will infer it for you. (The exception being things like recursion where the type can’t be inferred, in which case you must declare the type explicitly or the compiler will complain at you.)

True as far as it goes, but if you write “var eight = 8” then it’s valid C# (in local functions only) and valid Unityscript. In both cases the type is inferred by the compiler. In neither case is it dynamic typing since the type is static and can’t be changed later.

@Strogg2313 : use code tags when posting code.

–Eric

1 Like