Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Online UnityScript/Javascript to C# converter

Discussion in 'Scripting' started by MikeHergaarden, Apr 16, 2010.

  1. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Edit:
    A greatly improved version of this script is now available on the unity asset store under extensions.

    This version now also automatically recognizes some types of untyped JS vars(float, int, string) hereby saving a lot more work.
    Multiple scripts can be converted in a single click, all inside the editor.
    Source code included.

    Original post:

    As we're in the process of converting our projects to C#, I made a script to help converting and save loads of time.

    It's not 100% perfect (that's impossible). But it should take less than a minute to convert a regular JS script to C# now. This saves us 10-20 minutes per script (we've got some big scripts)

    This resource is now also listed with the rest of our resources at: http://m2h.nl/unity

    Enjoy!
     
    Last edited: Dec 14, 2010
  2. AgainstTime

    AgainstTime

    Joined:
    Apr 14, 2010
    Posts:
    84
    Awesome!
    Good job Leepo!
    It'd be great if you could release source code.
     
  3. KarelA

    KarelA

    Joined:
    Dec 30, 2008
    Posts:
    422
    wow thanks. This is just what i needed :)
     
  4. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    I don't use Unityscript/Javascript myself but I can see how great an addition to the community this is. Very well done, it'll put a big smile on a lot of faces I'm sure.

    Best Regards,
    Matt.
     
  5. _mikey

    _mikey

    Joined:
    Nov 6, 2008
    Posts:
    12
    thanks for sharing this! saves a lot of tedious work.

    :)
     
  6. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,551
    This is very useful, I suggest you release the source code to the unity community so people can refine the source code more.

    My suggestions:
    In Javascript, member variables are public if public/private is not specified, in C#, member variables are private if public/private is not specified. So I suggest that member variables with no specification of whether they are public or private have the word "public" added to them by default.

    The same goes for member functions.


    I don't like it that the conversion changes my formatting of the function definitions, it puts the opening bracket on the same line instead of a separate line like my old code, plus it adds spaces where I don't want it to. I suggest the conversion sticks to whatever current formatting is being used.
     
  7. FreakFish

    FreakFish

    Joined:
    Jun 4, 2010
    Posts:
    119
    brilliant, translation programs are always useful!
     
  8. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,551
    I found an error:

    my Javascript goes like this:
    Code (csharp):
    1.  
    2. var serverIP : String = "127.0.0.1";
    3.  
    and it mistakes the value for a float and did this:
    Code (csharp):
    1.  
    2. string serverIP = "127.0f.0.1f";
    3.  
     
  9. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
  10. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Neodrop; for sure!
     
  11. KennyW

    KennyW

    Joined:
    Aug 20, 2010
    Posts:
    128
    Awesome! Thanks.
     
  12. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    A greatly improved version of this script is now available on the asset store under extensions.
    This version now also automatically recognizes some types of untyped JS vars(float, int, string) hereby saving a lot more work.
    Multiple scripts can be converted in a single click, all inside the editor.
    Source code included.
     
  13. marci

    marci

    Joined:
    Dec 13, 2010
    Posts:
    3
    As new to unity, may I ask why you would prefer C#?
    - Obviously it means a great deal, since you're using time to convert your existing projects..
     
  14. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    C# is more strict in its enforcement of type-safety. Allows for some more advanced syntax usages. Avoids some quirks and obscure compiling bugs that exist in Unity's JavaScript compiler. Integration with Visual Studio.
     
  15. TheClaus

    TheClaus

    Joined:
    Nov 26, 2009
    Posts:
    4
    I am looking on the asset store under extensions and do not see your converter on there. Has it been removed? Using Unity 3.1

    Thanks

    TheClaus
     
  16. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    I have not removed it, when they added another package of mine the converter somehow disappeared (and the new package was not added either)
    I'm waiting for the asset stores team on this. I hope this'll be resolved very soon.
     
  17. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Wait, are you saying your new and improved network tutorial is up!? Please let it be so! :)
     
  18. ProjectOne

    ProjectOne

    Joined:
    Aug 9, 2010
    Posts:
    442
    ops yes I was looking for your converter on the Asset Store and couldn't find it. Now I know why :)
     
  19. Rom-

    Rom-

    Joined:
    Nov 26, 2008
    Posts:
    90
    Any word from them yet? I'm very interested in using this. Or perhaps is the updated script available on your site?

    Thanks!
     
  20. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    The JS->C# converter and the C#->JS converter are both on the asset store now. Only my "Obfuscator" package seems to be bugged, I hope they fix that asap.
     
  21. IDFour

    IDFour

    Joined:
    Jan 10, 2011
    Posts:
    1
    Have they ever fix this?
     
  22. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
  23. dgutierrezpalma

    dgutierrezpalma

    Joined:
    Dec 22, 2009
    Posts:
    404
    Sorry for reviving an old post, but I have one question about the product:
    Is it capable of convert a JS script that uses the Array type (that isn't available in C#)?
     
  24. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Yes, there is one 'hack' that forces a conversion from array to arraylist I believe.
    HOWEVER, you should not buy the plugin based on features like these, as you don't want the tool to make decisions like this for you.
    You need to be aware of what you use...either List<bool> bool[] or ArrayList etc.

    You still need to have know how of C# when using the converter.
     
  25. dgutierrezpalma

    dgutierrezpalma

    Joined:
    Dec 22, 2009
    Posts:
    404
    Well, I want to convert some Javascript code into C# to integrate it better with the rest of my project and avoid compilation order issues, but it's a lot of work if I have to convert it manually to C#... I have also tried to build a Windows executable and decompiling the *.dll with ILSpy (as I already have the Javascript code, I think it is completely legal compiling and decompiling it to get a C# conversion) but unfortunately ILSpy returns many references to Unity internal classes and methods, so it's completely useless for me because it would need a lot of work to remove from the source code those references.

    The code relies extensively on the use or non-typed variables (although most of them contains basic types or arrays of basic types) and the use of the Array class. If your "Javascript to C# Converter" is suitable for this task I will purchase it without any doubt, but before making the purchase I need to know if it will convert nicely this kind of code or if I would have to do most of the work manually.
     
  26. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    I have used it to convert two big projects from JS to C# completely, and I can honestly say it saved me several hours of work per project. There is limited/basic support for detecting the type of a var to help out there (float/string/int I believe..). Furthermore as the sourcecode is provided you could add support for your own custom arrays etc.

    By default it wil probably not detect the types of your arrays.
     
  27. dgutierrezpalma

    dgutierrezpalma

    Joined:
    Dec 22, 2009
    Posts:
    404
    You talked in your previous post about a hack that forces the conversion from Array to ArrayList... I suppose that would be good enough for the first quick conversion (although it will probably need further adjustments), is that hack included in the package in the Asset Store or it is a "non-supported" hack somebody posted in the forums?
     
  28. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    It's not in the tool. But is still in the simpler and free web version: http://www.m2h.nl/files/js_to_c.php

    EDIT: My mistake, it only points out the error. Mass replacing Array with ArrayList should work though. Using List<yourType> is even better.
     
  29. dgutierrezpalma

    dgutierrezpalma

    Joined:
    Dec 22, 2009
    Posts:
    404
    I tried the web version but it asked me to define manually more than 350 type definitions in the first script I tried (most of them were basic types or arrays [] of basic types) and didn't translate correctly the name of a function (because it included "Int" in the name). That is the reason why I was asking about the paid package, to see if it would help me to automatize a little more the conversion.
     
    Last edited: Jun 27, 2011
  30. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    This is great. Thanks
     
  31. taar1

    taar1

    Joined:
    Jan 12, 2012
    Posts:
    26
    I'm wondering if you also have an online C# to JS converter somewhere? I really just have to convert a few lines of code (which I don't know how to convert properly, that's why I need a converter. Otherwise I would have done this manually) and for me it's a bit an overkill to get this from the Assets Store. Thank you :)
     
  32. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Sorry, I don't have a web (PHP) version of [C# to JS], only the assetstore version.
     
  33. Ramsar

    Ramsar

    Joined:
    Jul 22, 2012
    Posts:
    34
    Upon loading the C# to JS unity package I do not get the 'Tools' option in the menubar, in which I should find the converter option. Any idea how to solve this?

    I'm running Unity 3.5.3 on Win7 64
     
  34. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Do you have any open compile errors in your console? If so, Unity won't compile editor scripts so that the Tools menu wont appear.
     
  35. Ramsar

    Ramsar

    Joined:
    Jul 22, 2012
    Posts:
    34
    That appeared to be the exact problem. I had trouble converting a JS file to C#, as the latter gave an error. So I imported the package to try to convert it with your tool. But since my bug-ridden C# script was still present => no 'Tools'.

    Anyways, thanks for the tip (maybe an idea to include it in the readme file?).
     
    Last edited: Aug 19, 2012
  36. fatihmert

    fatihmert

    Joined:
    Aug 13, 2012
    Posts:
    3
    Nice work, maybe our needed Javascript to Boo script language. Is this hardly? I needed Javascript to Boo script in fact.
     
  37. WalterEspinar

    WalterEspinar

    Joined:
    Aug 23, 2011
    Posts:
    83
    no more free?
     
  38. MarkrosoftGames

    MarkrosoftGames

    Joined:
    Jan 5, 2012
    Posts:
    442
    this doesn't support boo -> c# does it?
     
  39. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Correct, Boo is not supported.
     
  40. ihsan_unity347

    ihsan_unity347

    Joined:
    Feb 14, 2018
    Posts:
    1
    #pragma strict

    function Start () {

    }

    function Update () {

    }
    function OnTriggerEnter(other:Collider)
    {
    GetComponent.<AudioSource>().enabled=true;
    }
    function OnTriggerExit(other :Collider)
    {
    GetComponent.<AudioSource>().enabled=false;
    }
     
  41. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This thread is quite old. Your code looks pretty simple. I would suggest that you just learn a little bit about C# (in Unity) and you will easily be able to write your own code in C# and/or fix up that script. :)
    * remove the 'pragma' line and change each "function" to "void" , delete the colon after "other", but keep a space between them. I think that's it.
    Please post your own thread if you are still stuck after that.
     
  42. n12901

    n12901

    Joined:
    Sep 29, 2017
    Posts:
    7

    This might be a better solution.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MyClass : MonoBehaviour {
    6.  
    7. void Start () {
    8.  
    9. }
    10.  
    11. void Update () {
    12.  
    13. }
    14. void OnTriggerEnter(Collider other)
    15. {
    16. GetComponent<AudioSource>().enabled = true;
    17. }
    18. void OnTriggerExit(Collider other)
    19. {
    20. GetComponent<AudioSource>().enabled = false;
    21. }
    22.  
    23. }
    P.S. Replace MyClass with the name of your script.