Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Converting C# to JavaScript

Discussion in 'Scripting' started by Strogg2313, Aug 16, 2014.

  1. Strogg2313

    Strogg2313

    Joined:
    Jan 2, 2014
    Posts:
    9
    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;
    }
    }
    }
    }
     
  2. Karwler

    Karwler

    Joined:
    Aug 25, 2014
    Posts:
    18
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var showOptions = false;
    4. var shadowDrawDistance : float;
    5. var ResX : int;
    6. varResY : int;
    7. var Fullscreen : bool;
    8.  
    9. function Start () {
    10. showOptions = false;
    11. }
    12.  
    13. function Update () {
    14.  
    15. }
    16.  
    17. function OnGUI() {
    18. if(GUI.Button(Rect(500, 100, 300, 100), "Start Game")) {
    19. Application.LoadLevel(1);
    20. }
    21. if(GUI.Button(Rect(500, 210, 300, 100), "Quit Game")) {
    22. Application.Quit();
    23. }
    24. if(GUI.Button(Rect(500, 320, 300, 100), "Options Menu")) {
    25. showOptions = true;
    26. }
    27. if(showOptions == true) {
    28. //INCREASE QUALITY PRESET
    29. if(GUI.Button(Rect(810, 100, 300, 100), "Increase Quality")) {
    30. QualitySettings.IncreaseLevel();
    31. Debug.Log ("Increased quality");
    32.  
    33. }
    34. //DECREASE QUALITY PRESET
    35. if(GUI.Button(Rect(810, 210, 300, 100), "Decrease Quality")) {
    36. QualitySettings.DecreaseLevel();
    37. Debug.Log ("Decreased quality");
    38. }
    39. //0 X AA SETTINGS
    40. if(GUI.Button(Rect(810, 320, 65, 100), "No AA")) {
    41. QualitySettings.antiAliasing = 0;
    42. Debug.Log ("0 AA");
    43. }
    44. //2 X AA SETTINGS
    45. if(GUI.Button(Rect(879, 320, 65, 100), "2x AA")) {
    46. QualitySettings.antiAliasing = 2;
    47. Debug.Log ("2 x AA");
    48. }
    49. //4 X AA SETTINGS
    50. if(GUI.Button(Rect(954, 320, 65, 100), "4x AA")) {
    51. QualitySettings.antiAliasing = 4;
    52. Debug.Log ("4 x AA");
    53. }
    54. //8 x AA SETTINGS
    55. if(GUI.Button(Rect(1028, 320, 65, 100), "8x AA")) {
    56. QualitySettings.antiAliasing = 8;
    57. Debug.Log ("8 x AA");
    58. }
    59. //TRIPLE BUFFERING SETTINGS
    60. if(GUI.Button(Rect(810, 430, 140, 100), "Triple Buffering On")) {
    61. QualitySettings.maxQueuedFrames = 3;
    62. Debug.Log ("Triple buffering on");
    63. }
    64. if(GUI.Button(Rect(955, 430, 140, 100), "Triple Buffering Off")) {
    65. QualitySettings.maxQueuedFrames = 0;
    66. Debug.Log ("Triple buffering off");
    67. }
    68. //ANISOTROPIC FILTERING SETTINGS
    69. if(GUI.Button(Rect(190, 100, 300, 100), "Anisotropic Filtering On")) {
    70. QualitySettings.anisotropicFiltering = AnisotropicFiltering.ForceEnable;
    71. Debug.Log ("Force enable anisotropic filtering!");
    72. }
    73. if(GUI.Button(Rect(190, 210, 300, 100), "Anisotropic Filtering Off")) {
    74. QualitySettings.anisotropicFiltering = AnisotropicFiltering.Disable;
    75. Debug.Log ("Disable anisotropic filtering!");
    76. }
    77. //RESOLUTION SETTINGS
    78. //60Hz
    79. if(GUI.Button(Rect(190, 320, 300, 100), "60Hz")) {
    80. Screen.SetResolution(ResX, ResY, Fullscreen, 60);
    81. Debug.Log ("60Hz");
    82. }
    83. //120Hz
    84. if(GUI.Button(Rect(190, 430, 300, 100), "120Hz")) {
    85. Screen.SetResolution(ResX, ResY, Fullscreen, 120);
    86. Debug.Log ("120Hz");
    87. }
    88. //1080p
    89. if(GUI.Button(Rect(500, 430, 93, 100), "1080p")) {
    90. Screen.SetResolution(1920, 1080, Fullscreen);
    91. ResX = 1920;
    92. ResY = 1080;
    93. Debug.Log ("1080p");
    94. }
    95. //720p
    96. if(GUI.Button(Rect(596, 430, 93, 100), "720p")) {
    97. Screen.SetResolution(1280, 720, Fullscreen);
    98. ResX = 1280;
    99. ResY = 720;
    100. Debug.Log ("720p");
    101. }
    102. //480p
    103. if(GUI.Button(Rect(692, 430, 93, 100), "480p")) {
    104. Screen.SetResolution(640, 480, Fullscreen);
    105. ResX = 640;
    106. ResY = 480;
    107. Debug.Log ("480p");
    108. }
    109. if(GUI.Button(Rect(500, 0, 140, 100), "Vsync On")) {
    110. QualitySettings.vSyncCount = 1;
    111. }
    112. if(GUI.Button(Rect(645, 0, 140, 100), "Vsync Off")) {
    113. QualitySettings.vSyncCount = 0;
    114. }
    115. }
    116. }
    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?
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    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
     
    elmar1028 likes this.