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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

CSharpatron - the definitive Unityscript to C# converter!

Discussion in 'Assets and Asset Store' started by trepan, Jul 31, 2014.

  1. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    I realize I’m probably somewhat behind the times with this, but I just completed work on what I’d like to think is the definitive Unityscript to C# converter… Prompted by my own need to convert a large, complex Unityscript code-base, I tried out some of the existing conversion tools and found that they didn’t take me too far. I thought I’d try a different approach…

    CSharpatron parses script files utilizing type information gained via Reflection. It's able to understand Unityscript files at a very low level, not only making it great at the 'obvious' conversions, but able to go much, much further to make really subtle fix-ups too, e.g. variable scoping adjustments and adding required type-casts. ...In most cases the output C# files will compile right away with no manual work whatsoever.

    There are many other features to help at all stages of project conversion:
    • Thorough log information for all fix-ups and errors.
    • A workspace view shows all converted files and their conversion status. You can enable or disable a C# file at will.
    • C# compile errors are captured and can be viewed from the workspace.
    • A special tool allows you to test compilation of a C# file even when the file has many dependencies that would otherwise block compilation.
    • Unityscript and C# files are switched in a way that won't break any mapped game-objects.
    CSharpatron can convert the old Penelope example project in less than 5 minutes without a single file-edit required. For my own project, it enabled me to move 107,000 lines of Unityscript to C# in just a few hours.

    There's a really thorough manual covering CSharpatron and the full process of .js to .cs conversion here.

    CSharpatron is available on the Asset Store here.

    All feedback welcome...

    Chris



    The workspace view show the status of a 'mid-conversion' project. It shows you any conversion issues, C# compile issues and external dependencies for a file...



    Open up a file to see errors (with jump to line) or type/file dependencies. A number of other tools (including a 'bonus' .Net Reflection tool) exist to help you convert your files and projects.

     
    Last edited: Jul 31, 2014
    twobob likes this.
  2. LadyAth

    LadyAth

    Joined:
    Jan 17, 2014
    Posts:
    158
    I've been staring at CSharpatron in my wishlist for weeks. I've looked at a few converters in the past and been very disappointed with the results. Going to take the plunge and buy this one (if it goes on sale in the next 6 months I am going to cry) simply because there are SO MANY tutorials that I have to ignore because they are written as .js (and not interested in spending time to learn to convert scripts manually)

    trepan: I really hope this is the answer to my script conversion woes.
     
  3. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    > I really hope this is the answer to my script conversion woes

    I hope so too. Let me know if you encounter any problems!
     
  4. LadyAth

    LadyAth

    Joined:
    Jan 17, 2014
    Posts:
    158
    Indeed it has! This is by far the best conversion tool I've tried and a very worthwhile purchase :) huzzah!!!
     
  5. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    That's good to hear :)

    Thanks for the feedback.
     
  6. Arcanor

    Arcanor

    Joined:
    Nov 4, 2009
    Posts:
    283
    I'm having show-stopper problems with the package, which I just purchased today on 50% off sale. It's hanging up my Unity Editor in an endless error loop that seems to be related to the CSharpatron editor window GUI.

    FormatException: Input string was not in the correct format
    System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
    CSharpatron.SSCSharpatronRecord+ErrorInfo..ctor (System.String str)
    CSharpatron.SSCSharpatronRecord..ctor (System.String _fn, System.IO.StreamReader reader)
    CSharpatron.SSCSharpatronWorkspace.ReadFromFile ()
    CSharpatron.SSCSharpatronWorkspace..ctor (System.String _fn)
    CSharpatron.SSCSharpatron.InitializeWorkspace ()
    CSharpatron.SSCSharpatron.OnEnable ()
    CSharpatron.SSCSharpatron.OnInspectorUpdate ()
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
    System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
    UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:241)
    UnityEditor.HostView.Invoke (System.String methodName) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:234)
    UnityEditor.HostView.OnInspectorUpdate () (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/GUI/DockArea.cs:199)

    The only way I can stop the errors from continuing to accrue is to delete the CSharpatron dll.

    I'm running Unity Pro 4.5.4f1.
     
  7. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    It seems like there's some bad text in the workspace file, most likely a captured error message of unexpected form. A quick fix will be to delete the offending error from the file record. I've sent you a PM with more information and request for a copy of the bad text so I can properly resolve the problem.

    Sorry for the inconvenience :(
     
  8. Arcanor

    Arcanor

    Joined:
    Nov 4, 2009
    Posts:
    283
    Thanks for your quick response. :) I will continue to work it out with you via email so as not to clutter this thread. I'm sure we can get this resolved. It looks like a great product!
     
  9. Arcanor

    Arcanor

    Joined:
    Nov 4, 2009
    Posts:
    283
    Quick followup for the forums...

    Apparently this was a small bug, due to Windows vs. Mac CR-LF end of line character differences. Trepan was very quick to provide me with an updated DLL and it seems to be working fine now. Excellent service!
     
  10. iamsam

    iamsam

    Joined:
    Dec 22, 2013
    Posts:
    233
    Hi Trepan,

    I am getting similar error each time I modify a new converted file. I will PM you my details and would appreciate the update.

    Also is it possible to make a video demonstrating the conversion process. I tried to convert the standard assets js scripts and although the errors were corrected I could not get the finalize button to show up.

    Thanks,
    Sam
     
  11. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Hi Sam,

    Sorry about the error :(. I have learnt that although Unity shields us from most platform differences, line-feeds in compiler output are in fact different between Mac/PC!

    I've replied to your PM regarding the update and your standard assets test (need a little more info to tell if this is a bug or incomplete process).

    For anyone else seeing the Int32.Parse/GUI bug please contact my support email (see 'Support' in manual) and I'll reply with a fixed interim version. NOTE: I'm currently finishing work on a new build of CSharpatron that fixes this bug and also addresses another problem seen by another user when projects contain vars of mixed type (e.g. int[,][]). Hopefully I'll be submitting this later today so it'll be live in 3-4 days.

    As for a video demo, yes, that might be a good idea...

    Regards,

    Chris
     
  12. iamsam

    iamsam

    Joined:
    Dec 22, 2013
    Posts:
    233
    Hi Chris,

    Thanks a lot for the outstanding support.

    I just wanted to let the readers know that the errors were my fault as I did not see the finalize button which appears in blue when the csharp files compiles 100%. I successfully converted the character controller scripts in the standard assets folder to csharp equivalent files in a matter of few minutes using this plugin. Really amazing and well designed asset. Looking forward for the video for first time users like me to quickly dive in and use it to its full potential.

    Good luck with future updates, this is a must have plugin for Unity.
    Sam.
     
  13. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Well I'm glad about that! Thanks so much for the positive feedback.

    v1.1 will be submitted to the Asset Store very soon now...
     
  14. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Just a quick update to let you know that I created a demo/tutorial video for CSharpatron. You can see the video here.

    I also submitted v1.1 to the Asset Store last night, so hopefully it will be live in a few days. As soon as it is, I'll post full details of the update (there's a lot of stuff, both bug fixes and new features).

    Many thanks to those of you that have purchased CSharpatron - I really appreciate your support and feedback.

    Chris
     
  15. iamsam

    iamsam

    Joined:
    Dec 22, 2013
    Posts:
    233
    Thanks Chris for the outstanding support, the video was exactly what I was looking for... really helps in using this asset to its full potential. Patiently waiting for v1.1 :)

    Sam
     
  16. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    The new version of CSharpatron was just approved. Sorry it took a little longer than anticipated (the first submission failed to send and I had to resubmit). On the plus side there are now a couple more improvements...

    Here's the change-log for v1.11:
    • Fix for error case when reading workspace file when running under Windows (causing 'Int32.Parse' crash cycle).
    • Improved support for multi-var arrays declared in Unityscript (e.g. int[,])
    • Support for mixed format multi-dimensional arrays declared in a C# assembly (e.g. int[,][])
    • Fix for a freeze bug that could occur if source contained an abbreviated float constant of the form ‘1f’.
    • Reworked handling for auto fix-up of assignment to value type return. New method works in all cases barring multi var assignment.
    • Prevented an unhelpful type-cast to ‘IEnumerable' that could be added in some circumstances.
    • Fixed a case where a var declared as a Unityscript Array could fail to yield an error.
    • Fixed possible confusing error message pending assignment of a Unityscript Array to an otherwise untyped var.
    • Fixed potential parsing bug when processing initialized arrays (could have caused bad type-cast bracketing).
    • Added a debug ‘line-step’ feature to help one user to track down a 'lock-up’ bug.
    • Added support for Javascript ‘Number’ type (converts to C# 'double’)
    • UnityEngine.Object.Instantiate return type now implicitly set to match first param (see details in manual)
    • @CustomEditor is now respected and used to infer the type of the editor class var ‘target’. This works wonders for conversion error counts in editor related files.
    • Extension methods (from C# assemblies) are now parsed correctly when used in Unityscript.
    • Added a ‘DumpExtensionMethods’ debug tool (under Extras).
    • Fixed bogus ‘read-only’ error when script was writing to ‘this.member’.
    • Fixed possibility for negative numeric constants to be misinterpreted.
    • Improved detection of implicitly convertible types in binary ops to fix some spurious casts.
    • ‘Rebuild stubs file’ is now blocked if there are outstanding fatal errors (to avoid generation of an incomplete file).
    • Reworked intParse and floatParse to just insert type-casts as appropriate.
    • Improved wording for a couple of common warnings.
    • Improved handling for typeof() operator. If supplied a non-class name, CSharpatron now auto-converts to var.GetType()
    • Added new ‘About’ callout in Control Center panel to make version and contact details a little more accessible.
    • Added support for anonymous methods.
    • Major optimization to conversion times, now approximately twice as fast as v1.03.
    • Fixed parsing problem in ‘for( var x:type in y)’ style loops where ‘type’ included template brackets.
    • Fixed fatal error parsing nested ‘if' statements of the form ‘if (x) if (y) {}’.
    • Added ability to ‘DestroyWorkspace’ after ‘Finalize’.
    • Better handling of attempted conversion of scripts in 'Standard Assets', 'Editor' or 'Plugins' folders (direct conversion blocked, workaround detailed in manual).
    • '@script AddComponentMenu' is now relocated prior to class body as required by C#.
    Many thanks to users who logged bug reports and supplied code snippets that helped me to identify and resolve problems. Special thanks to Tanuki Digital for granting me full source access to an entire project - great for prompting some of the new conversion improvements!

    You can see the manual, updated for the new version, here.

    Chris
     
  17. Metapoo

    Metapoo

    Joined:
    Oct 30, 2014
    Posts:
    6
    Hi Chris,

    I'm getting this error for a JS file that imports System.Security:

    js/ApiManager.js[-1]. Fatal:System.Exception: Failed to find component 'Security' of path: System.Security.Cryptography. Is this library in the 'exclusions' list?
    at CSharpatron.SSTypeDatabase.LookupContainer (System.String path, Boolean errorOnFail, CSharpatron.SSDBContainer baseContainer) [0x00000] in <filename unknown>:0
    at CSharpatron.SSTypeDatabase.LookupContainer (System.String path, CSharpatron.SSDBContainer baseContainer) [0x00000] in <filename unknown>:0
    at CSharpatron.SSSourceConverter.AddLib (System.String libName, Boolean addUsing) [0x00000] in <filename unknown>:0
    at CSharpatron.SSSourceConverter.Convert (System.String className, System.String source) [0x00000] in <filename unknown>:0
    at CSharpatron.SSCSharpatron.Convert (UnityEditor.MonoScript scriptObj) [0x00000] in <filename unknown>:0
    LAST CONVERSION LINE: 1
    UnityEngine.Debug:LogError(Object)
    CSharpatron.SSCSharpatron:EmitError(String, ErrorType, Int32)
    CSharpatron.SSCSharpatron:Convert(MonoScript)
    CSharpatron.SSCSharpatron:ConvertAll(MonoScript[])
    CSharpatron.SSCSharpatron:OnInspectorUpdate()
    UnityEditor.HostView:OnInspectorUpdate()
     
  18. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    That should be a simple thing to fix... Look in the configuration panel for 'Conversion/Filter libs list' and un-tick this option. Your file will hopefully now convert without issue.

    (What's going on here is that when CSharpatron starts up it has to generate a large database of type information and to reduce the time this takes, I deliberately filter out a number of namespaces that don't seem like things many people would be using. ...Of course this is a subjective thing and perhaps I'm wrong in the case of System.Security - sorry! ...Anyhow, the option is there to stop the filtering from occurring so that the converter has access to all available namespaces at the expense of slightly slower startup).

    Hope this resolves your problem...
     
  19. Kajo

    Kajo

    Joined:
    Apr 15, 2013
    Posts:
    12
    Hey i just purchased CSharpatron, but for me it's freezing when im trying to make the workspace for selected files. I let it process the files for over an hour but it never moved further from 16/99. There was three errors so i suppose those have something to do with it. Any help here?
     
    Last edited: Nov 21, 2014
  20. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Hi Kajo,

    Thanks for your purchase. I'm sorry to hear you're having problems.

    If you look at the progress dialog it should tell you the name of the file being processed at the point that the freeze occurs (look in the bottom right corner). We'll have to figure out what in that file is causing trouble. If you're able to send me the file I can take a look. Alternatively you can investigate further yourself by ticking the option 'Line step (for debugging)' in the control panels 'Debug' section. This will make it so that you have to click a dialog button before each attempted line of conversion - very tedious, but the best tool for diagnosing this kind of problem!

    If you look in the control panel 'About' section you'll see a direct email contact which is the best way to send me the file and/or any more information you can gain by line-stepping. Once we track down the source line that's causing the freeze it can probably be commented out as a quick work-around, and I can fix the issue and a send you a new CSharpatron .dll.

    Regards,

    Chris
     
  21. beeboks

    beeboks

    Joined:
    Aug 11, 2013
    Posts:
    11
    Hi @trepan I'm having an issue with CSharpatron showing errors on converting arrays.

    I'm getting the message:
    Problem parsing statement. Bad type usage "Unshift" for type '<NOT AVAILABLE>'. Manual fix-up required.

    How can I solve this issue?

    I'm using Unshift and Pop functions from a new Array();

    Thanks!
     
  22. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    I'm afraid Unityscript Array usage is one thing that CSharpatron (deliberately) doesn't try to automate. The issue is that Unityscript implements an Array type that isn't available in C#. Because it is a typeless array and C# almost always requires strict type choices, there's no totally equivalent array type that I could reliably convert to without having a deeper understanding of programmer intent :)

    Fortunately manual conversion, typically to a generic List or built-in array isn't usually too difficult. (Sometimes even a Stack or Queue could be more appropriate). You should see CSharpatron reporting more obvious errors where your Array instances are being declared. Switching types would typically begin there, in your Unityscript files (since all of the C# generic collections are available in Unityscript too). Once you have switched to a new type and have things compiling again, Re-Convert should no longer throw any errors. You can see a simple example of this process in my tutorial video:

    .

    The following posts contain more information:

    http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?
    http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

    I don't believe there's an exact 'Unshift' function on a generic list, but the same behaviour isn't too hard to achieve

    http://www.wenda.io/questions/4907965/c-sharp-version-of-array-unshift-from-unityscript.html

    'Pop' from a generic list is just case of removing the last element, e.g. list.RemoveAt(list.Count-1).

    I hope this helps,

    Chris
     
  23. beeboks

    beeboks

    Joined:
    Aug 11, 2013
    Posts:
    11
    @trepan thanks for the help! I got past the Array stuff and am running into a few more issues.

    I got a lot of my main files converted and when I finalized the workspace, I ran into a ton of more errors from other scripts that couldn't find the JS component anymore of my GameMaster script. I created a new workspace to convert those files over as well and now although the references are all loaded in, I'm getting a C# error in the workspace view saying "The type or namespace name "GameMaster" could not be found. Are you missing a using directive or an assembly reference?" although if I click the X-Refs, GameMaster is lit up in green. It's not allowing me to de-shadow the files as it's saying "There are still outstanding dependencies." The workspace shows 100% code conversion.

    Any idea what I could do?
     
  24. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Are you aware of Unity's script compilation order? In any case where you're converting only a subset of files from .js to C# you *really* need to consider this. You can see it explained here:

    http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

    ...Basically it means that when using a mix of languages, unless compilation order is carefully considered, you will certainly see issues where files in one language are unable to locate types defined in the other language.

    I suspect where you're at right now is a consequence of:

    - You converted your first set of files which turned GameMaster into a C# class. This was ok while you had your conversion workspace active (because CSharaptron was keeping the class file inside the Plugins folder). However when you finalized that workspace, the C# files were copied back into your regular scripts folder (where Unityscript compilation cannot see their types because they haven't been compiled yet).

    - Now you've made a new workspace to convert the problem Unityscript classes, but you're in a state where your project doesn't compile anymore. Unfortunately this breaks a core rule for working with CSharpatron: it can only work correctly with a cleanly compiling project (since it looks up critical information in your project assemblies which simply aren't present when the code is in a broken state). This is most likely why it's saying that there are still outstanding dependencies (the assemblies containing those classes are missing). If you click on a file's XRefs field you can see the classes/files it is reliant upon and which are missing - hopefully that will confirm what I'm suggesting.

    To fix things I'm afraid you'll need to unpick things a little to get back to a compiling state... I think I'd start by destroying your new workspace (it can't contribute anything useful with missing assemblies). Next move the GameMaster class back into the Plugins folder. This may well be enough to get things compiling, or that file may have its own dependencies that also need to be moved with it. ...If unpicking *those* dependencies is too problematic, then frankly it might be easiest to revert everything back to your pre-conversion state and start the whole conversion process again. If you do that - and I guess this would be a takeaway for anyone else reading this thread - I'd really recommend converting *all* of your .js files in a single workspace before finalizing it.

    I don't believe there's any major problem here, just that you've encountered an unlucky consequence of not converting all files in one go; I must admit when I developed CSharpatron I was mainly considering people wanting to completely eliminate .js files from their project (not least to get away from this ugly compilation order stuff!), but perhaps that isn't always the case?

    Hope this helps to get things back on track...

    Chris
     
  25. beeboks

    beeboks

    Joined:
    Aug 11, 2013
    Posts:
    11
    @trepan Thanks for the fast replies, Chris. Super helpful. I went ahead and started fresh, and I'm going file by file. I've run into this issue a couple times where I convert a file to C# and it adds the whole file under a block with:

    namespace CSDEP
    {
    file.cs
    }

    and I get an error that says

    error CS1061: Type 'UnityEngine.Object' does not contain a definition for 'transform' and no extension method 'transform' of type 'UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)

    How do I get around this without messing stuff up?

    Thanks!
    David
     
  26. beeboks

    beeboks

    Joined:
    Aug 11, 2013
    Posts:
    11
    @trepan actually, nevermind I think I got it. It was my mistake
     
  27. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    I'd expect an error like that to be associated with a line number. Not sure I can suggest too much without seeing the corresponding script line?

    Could you send me an example of a problem file perhaps? There's a contact email in the About section of the configuration panel.
     
  28. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Ok!

    BTW - the CSDEP namespace stuff is all part of the temporary 'file-shadowing' stuff CSharpatron does to try and separate a file from its dependencies so you can focus on getting it to compile successfully. Such a file is 'active' alongside the .js original but is hidden by virtue of sitting inside the namespace.
     
  29. beeboks

    beeboks

    Joined:
    Aug 11, 2013
    Posts:
    11
    @trepan thank you so much for all of the help. I got my whole project converted over now. Thanks for writing such an awesome tool!

    I noticed that when I finalized my project earlier, the tool errored out upon finalization and didn't copy the files over into the project because it tried to delete the work folder and the work folder had a file that I forgot to check out of source control so it copied it over into the work folder with the read only permissions still set. It might be better to reset permissions when copying files over to make sure they could be deleted upon finalization or at least put up some kind of warning.

    I tried to manually copy them over after the fact but they were conflicting with the existing js files and the project was hopelessly broken at that point anyway so I restarted the process, and when finalizing made sure that all of the permissions on the work folders allowed for writing.

    Anyway, just a suggestion. Awesome tool and thanks for your great support on it.
     
  30. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Glad you got everything converted!

    Fair point regarding the file permissions stuff - thanks for the feedback.
     
  31. KrayonZA

    KrayonZA

    Joined:
    Mar 5, 2013
    Posts:
    52
    I purchased cSharpatron its excellent i'm just having some "fun" trying to fix my arrays by converting them into lists
    Does anyone know how to convert the below arrays into a generic list?
    I'm banging my head against a wall I've been trying to get this working for 8 hours.

    Code (JavaScript):
    1.  
    2. var upgradesApplied : boolean[];
    3. var upgrades : Upgrade[];
    4. private var storeUpgrades : Upgrade[];
    5.  
    6. function Awake() {
    7.     upgrades = Array(GetComponentsInChildren(Upgrade)).ToBuiltin(Upgrade) as Upgrade[];
    8.     var tempArr = new Array();
    9.     upgradesApplied = new boolean[upgrades.length];
    10.     for(var i = 0; i < upgrades.length ; i ++) {
    11.         upgradesApplied = upgrades.applied;
    12.         if(upgrades.showInStore){
    13.             tempArr.Push(upgrades);
    14.         }
    15.     }
    16.     storeUpgrades = tempArr.ToBuiltin(Upgrade) as Upgrade[];
    17. }
    18.  
     
  32. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Thanks for purchasing CSharpatron. I think the following is what you need:

    (But beware that I haven't had a chance to test this!)

    Code (JavaScript):
    1. var upgradesApplied : boolean[];
    2. var upgrades : Upgrade[];
    3. private var storeUpgrades : Upgrade[];
    4.  
    5. function Awake() {
    6.     // Can use generic form of GetComponent() to avoid any casts. No need for this to ever be an Array.
    7.     upgrades = GetComponentsInChildren.<Upgrade>();
    8.     var tempArr = new List.<Upgrade>();                 // Create List not Array
    9.     upgradesApplied = new boolean[upgrades.length];
    10.     for(var i = 0; i < upgrades.length ; i ++) {
    11.         // Should this line...
    12.         //upgradesApplied = upgrades.applied;
    13.         // Actually be this?
    14.         upgradesApplied[ i ] = upgrades[ i ].applied;  
    15.         // Similarly the following line seems like it should be accessing a specific element, no?
    16.         if(upgrades[ i ].showInStore){
    17.             tempArr.Add(upgrades[ i ]);         // Array Push becomes List Add
    18.         }
    19.     }
    20.     storeUpgrades = tempArr.ToArray();
    21. }
     
    KrayonZA likes this.
  33. KrayonZA

    KrayonZA

    Joined:
    Mar 5, 2013
    Posts:
    52
    trepan you're a legend that works, I appreciate the help.

     
  34. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    Hi trepan! Thanks for this tool, it has been really quite helpful. I did discover a few bugs with it.


    The conversion process seemed to fail when there was a parenthesis inside a string. For example:

    var failExample : String = "Vector3(";


    Also it could not handle JavaScript files that had zero code inside them. These might be valid files in need of converting, because one might need a shell MonoBehaviour for use with a custom inspector class. So in case of an empty file CSharpatron should basically output this:

    using UnityEngine;

    public class EmptyFileName : MonoBehaviour
    {

    }


    Note that adding even just one comment inside the empty file helped getting around the above problem.


    UPDATE: Found a third bug:

    For some reason trying to convert the following code:

    #pragma strict

    function OnDestroy()
    {
    GlobalVariables.cutsceneInProgress = false;
    }



    resulted in this error:

    [-1] - Fatal: System.IndexOutOfRangeException: Array index is out of range.


    Changing the code to the following made the conversion process work:

    #pragma strict

    public class OnDestroyStopCutscene extends MonoBehaviour
    {
    function OnDestroy()
    {
    GlobalVariables.cutsceneInProgress = false;
    }
    }
     
    Last edited: Jul 13, 2015
  35. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Hi Naster, many thanks for your purchase and feedback - much appreciated!

    I'm actually pretty close to submitting v1.3 to the Asset Store. I think it'll be pretty trivial to include fixes for the first two.

    The third one will take a little more investigation. From your workaround it would seem like the converter is failing to recognize your class as a MonoBehaviour derivation where certainly it should. I'll debug it tonight.

    If you encounter anything else don't hesitate to contact me on the support email address and I can mail you the latest version.
     
  36. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    Hi, I'm just wondering how one deals with different platform defines, your help says to select the platform you are developing on but the Conversion options allows for selecting everything or a mixed collection (but not UNITY_IOS unfortunately, unless that only appears once you are on a mac?)
    Is the idea to run the conversion multiple times or should I be selecting all the platforms I have defines for in the code?
     
  37. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Hi Monark,

    I'm afraid it's necessary to select and convert for each platform individually. This is necessary because CSharpatron can only understand symbols present in the currently compiled assemblies (hence the need for your active build version and chosen platform define to match). At present you would have to manually combine the appropriate output from each version so it is a bit of a hassle unfortunately :(

    The reason UNITY_IOS is missing from the platform list is simply that the list needs to be updated - it used to be UNITY_IPHONE! I have v1.3 in progress with this fix and others - contact the support email and I'll send you an advance version if you like. ...A totally valid workaround would also be just to add UNITY_IOS as a custom define.

    Hope this helps. Thanks so much for your purchase,

    Chris
     
  38. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    I've dropped you and email about this next one too, but thought it might be useful to others.

    I'm getting fatal errors in all scripts that use xml

    Fatal: System.Exception: Failed to find component 'Xml' of path: System.Xml. Is this library in the 'exclusions' list?

    Not sure what the exclusions list is, but surely Xml can't be excluded?
     
  39. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    To give a slightly faster startup time, CSharpatron excludes some less commonly used libraries from its symbol parse. You can stop it from doing this by unticking 'Filter libs list' under the 'Conversion' options.

    You make a valid point that perhaps the Xml library shouldn't be on this list though :)
     
  40. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    Glad to know it's a simple fix! :)
     
  41. fairchild670

    fairchild670

    Joined:
    Dec 3, 2012
    Posts:
    69
    Hello there - I was curious if anyone has had any success with CSharpatron in Unity 5.2.2? I recently purchased it and started converting a ~300 js file project, but found hiccups with any file that referenced Unity's new UI Text class and Enums. Basically these are causing fatal errors:

    Code (JavaScript):
    1. private var textObjects : List.<Text> = new List.<Text>();
    2. var InventoryMenuTexts : Text[];
    3. var ItemSlots : Text[];
    Exception is:

    An example of the Enum issue is here, with the "t.ToString()":

    Code (JavaScript):
    1. for (var s : String in splitDescription) {
    2.             for (var t : TokenCIMenuItem in Enum.GetValues(TokenCIMenuItem)) {
    3.                 if (s.IndexOf(t.ToString()) >= 0) {
    Exception is:

    In any case it's a fantastic asset and can't believe converting most of the project only took a day or so. Any help would be greatly appreciated to get over that last hill!

    Larry
     
  42. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    Hi Larry,

    I've actually been sitting on a new version for a little while because I've been too busy to finish it. If you email me at the support address then I can send you a copy. It has quite a few fixes and I think that issue may be among them.

    Thanks for your purchase and very glad to hear that you're otherwise getting on well!

    Regards,

    Chris
     
  43. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,140
    Are the above issues fixed now and does it work fine in Unity 5.4? I ask because it is only published for 4.x to the store
     
  44. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    I have problems converting certain js files – Csharpatron simply hangs on them, even though they are free from warnings and errors. Is this a known problem? It makes shadowing impossible, of course.

    Also, when building the stubs file, certain definitions seem to have unresolved string arithmetic, causing compiler errors.

    CSharpatron also seems to have problems with inline arg attributes:

    @Operator("Move to an object", "Moves to a given GameObject. If null, fails. If there's no path to the target, fails.")
    public function MoveToObject (@ScriptParameter target : GameObject,
    @DefaultValue(0.0) stoppingDistance : float,
    @DefaultValue(0.6) initialSpeed : float,
    @DefaultValue(false) variableSpeed : boolean
    ) : IEnumerator {
    ...
    }

    Functional args also seem to be problematic:

    octreeHandler.OverlapSphere(pos, radius, function (go : GameObject) { ... })

    5.3.6p2, Mac.
     
    Last edited: Aug 21, 2016
  45. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,549
    It does not look like the author of this plugin visits this forum very much.

    I have a a general question about this product since you are using it already. Does this product come with C# source code or is it just an executable/plugin? If it came with C# source code I would be interested in purchasing since I could at least provide fixes to existing bugs.
     
  46. rstorm000

    rstorm000

    Joined:
    Aug 19, 2010
    Posts:
    228
    Just wanted to say this project is incredible. Cannot believe it work with such a huge project
     
  47. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    @rstorm000 Thanks so much for the praise, I really appreciate it :)

    To anyone else that messaged me above and I missed it - I'm really sorry, not sure why but I wasn't notified. Bit late now, but...

    @andyz Yes, CSharpatron works fine in 5.4. I just submit from an earlier version to ensure that 4.x users can potentially use the asset too (although not sure if there still are any these days!)

    @PeterB As you may have noticed v1.3 was recently published to the store. There are a lot of bug fixes in there, so it's worth trying again with this version. If you're still encountering problems, please contact me on the support email with as much information as you can about what you were trying to convert. There's also a new line trace option in v1.3 that should show you the last line parsed so you can get some clue of what was happening should the asset ever hang (look under Extras).

    @longroadhwy CSharpatron just ships as a DLL I'm afraid. Sorry about that, but it's quite complex so I wouldn't wish bug-hunting upon any of you :)

    Generally, it's true that I don't frequent this forum, nor spend too much time working with CSharpatron as I have a lot of other demands on my time (plus Unityscript seems to be an dead or at least dying language so this certainly isn't a high demand asset). That said, I will always respond to support emails and do my best to help. I believe CSharpatron remains the most complete tool of its kind and AFAIK - with v1.3 - it is totally up to date, at least up to Unity 5.4.2.
     
    protopop likes this.
  48. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,550
    Nimian Legends BrightRidge was written in UnityScript by a coding amateur - namely me. Its a mobile openworld adventure with hundreds of scripts and thousands of lines of code on hundreds of gameobjects, built over 4 years and 2 major unity versions (4 and 5).

    Thanks to CSharpatron, i have converted 150 scripts, with 200 to go. It even replaces the scripts in other scenes.

    It took about a day or two to understand how it works. There are a few fatal errors etc, but i have learned to do groups of files at a time, starting with scripts that have no references.

    It's a genuine life saver. Honestly, I have no idea how i would be able to do this without CSharpatron. Thank you.
     
  49. trepan

    trepan

    Joined:
    Feb 11, 2011
    Posts:
    113
    That's awesome to hear! Thanks so much for the feedback. As you say, in a large project you need to work through things a few files at a time, picking off those with fewest references first. After the zero dependency files are converted you can use the 'file shadowing' mechanism to help break through the dependencies - hopefully this is explained quite thoroughly in the manual (where my own 'case study' example may help as a reference).

    FYI there's a new build of CSharpatron currently awaiting Unity approval. It has a few bug fixes. This may end up being the final version now that Unityscript has been officially deprecated and the number of people with .js files to convert is tapering off.

    I think you'll find your compile time is many times faster once you're complete :)
     
    protopop likes this.
  50. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,550
    C# definitely compiles MUCH faster:)

    In case this helps anyone: I had some files that had Fatal errors and part of it was some extra spaces in my javascript

    var agentAnimation = GetComponent. < Animation > ();

    was filing. I removed the spaces around the <>()'s

    var agentAnimation = GetComponent.<Animation>();

    Works great. The spaces dont matter in js but can break the parser for c# conversion.

    I had like 14 failed files and was like "oh sh#$%". Don't lose hope:

    Some files were no longer used in my project. Perfect time to delete them.
    Some had the spacing issue above. Fixed.
    Some oshrt scripts only had 1 or two instances in my project. I converted those by hand or wrote new csharp files to replace them, and replaced them in my project manually.

    Now i am down to 4 fatal erros, and i am optimistic i will figure thos out too.

    When i began a few days ago i though there was no way, this is too overhwelming.

    But if you get stuck just try a few files or move onto fixing another part. I have converted about 250 files and am down to 150 left, these 150 are very interdependent so i took a break and worked on the Fatal files.

    I know all of us who used UnityScript a lot for years with big projects, this can be a difficult time for us so stay strong:) Ill post a few more notes if i learn anything else.

    Again fantastic work with Csharpatron - really beyond what i could build and so impressive.