Help with conversion from javascript to c#

Hi there,

I have this matrix script to set resolution, so I am able to make buttons fit on every resolution. But I have a button which is written in c# and it has to be c#, but I am having a bit of a hard time, trying to convert the matrix script from javascript to c#.

var originalWidth = 960.0;
var originalHeight = 640.0;
private var scale : Vector3;

function OnGUI() {

scale.x = Screen.width/originalWidth; 
	scale.y = Screen.height/originalHeight; 
	scale.z = 1;
	
	var svMat = GUI.matrix; 
	
	GUI.matrix = Matrix4x4.TRS(Vector3.zero,Quaternion.identity, scale);


GUI.matrix = svMat;

}

If someone would take the time to help me, it would be much appreciated.

For manual translation take a look what differs C# and JS syntactically. Sir Irk mentioned one good resource for that.

For translation of bigger projects you might want to translate the IL code produced by unity to C# and then set appropriate names for vars. The IL code is in the assembly called Assembly-CSharp.dll for C# code and Assembly-UnityScript.dll for JS, these files can be found in your project folder. Using a tool like ILSpy you can then translate that code to C#.

IL to C# or VB:

for syntax differences between UnityScript(JS) and C#:
http://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax

Look here to get started with syntax differences. Look here for built-in types in C#. 960.0 and 640.0 in this case can be type float. Doubles and floats are for numbers with that precision(There are others that are less common). Float is more common than double when scripting in unity. It’s usually not the best idea to ask people to rewrite your script for you. Instead if you have a specific problem when rewriting one, ask about that. Since this script is small and I see you tried a rewrite already, I’ll help with this one.

using UnityEngine;
//using System.Collections; not necessary for this

//When inheriting from MonoBehaviour, the class's name needs 
//to match the file's name otherwise Unity won't load it properly.
public class ClassName : MonoBehaviour {

    private float originalWidth = 960.0f;
    private float originalHeight = 640.0f;
    private Vector3 scale;
   
    private void OnGUI() {
 
        //scale's xyz variables are floats.
        //Screen.width is an integer but since we are dividing by a float
        //we will get a float as a result.
     
        scale.x = Screen.width / originalWidth; 
        scale.y = Screen.height / originalHeight; 
        scale.z = 1f; //"f" is used after a constant to make it a float

        //GUI.matrix is a Matrix4x4 from the UnityEngine Namespace.
        Matrix4x4 svMat = GUI.matrix;         
        GUI.matrix = Matrix4x4.TRS(Vector3.zero,Quaternion.identity, scale);       
        GUI.matrix = svMat;     
    }
}

To read about conversion and casting look here

C# tends to require more explicit declaration and conversion/casting than UnityScript does.

If you are looking for a quick and easy way to do this there is an asset in the Asset Store for doing just this.

This is not something I recommend for such a small script but if you insist on not doing the hard work it is an option, especially if you do a lot of tutorials and such with UnityScript. Also it is not my asset, before anyone accuses me of trying to make a profit from this.