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

Help Converting JS to C# please

Discussion in 'Scripting' started by Nubz, Dec 5, 2014.

  1. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Found some scripts I want to use but they're all in js and my entire project is C#.
    Only one I am not sure of is this one for the crosshair.
    If anyone could help please it would be great.

    Code (JavaScript):
    1. @script ExecuteInEditMode()
    2. var crosshair : Texture2D;
    3.  
    4. function OnGUI () {
    5.     var w = crosshair.width/2;
    6.     var h = crosshair.height/2;
    7.     position = Rect((Screen.width - w)/2,(Screen.height - h )/2, w, h);
    8.  
    9.     if (!Input.GetButton ("Fire2")) {
    10.         GUI.DrawTexture(position, crosshair);
    11.     }
    12. }    
    Only one part that I am not really sure what to do with is the 2 vars in the OnGUI function.

    Code (JavaScript):
    1. function OnGUI () {
    2.     var w = crosshair.width/2;
    3.     var h = crosshair.height/2;
    Not sure what to call them in C#.
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    var should work
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    w and h are floats.

    float w = crsoshair.width / 2;
    etc

    Automatic converter if you need (It doesn't do variable types, but like I said those 2 are floats)
    http://www.m2h.nl/files/js_to_c.php
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    you mean ints
     
    ZO5KmUG6R likes this.
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Oops, my bad, I do :p
     
    User340 likes this.
  6. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Thanks a bunch for the help both of you.
    @aaro4130
    I knew about that converter already and use it when I get stumped but like you said it won't fix everything.