- Home /
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.
We are not going to just rewrite it for you.
If you are having trouble with your translation then show it to us and tell us in detail what the issues with it are.
I am sorry, if it seemed like I just wanted you to write me the script, that wasn't what I wanted. But thank you very much for all of your help, so far my script looks like this.
originalWidth = 960.0;
originalHeight = 640.0;
Vector3 scale;
void OnGUI() {
scale.x = Screen.width/originalWidth;
scale.y = Screen.height/originalHeight;
scale.z = 1;
sv$$anonymous$$at = GUI.matrix;
GUI.matrix = $$anonymous$$atrix4x4.TRS(Vector3.zero,Quaternion.identity, scale);
GUI.matrix = sv$$anonymous$$at;
}
$$anonymous$$y problem, is that I can't figure out what to put in front of the variables, because in the javascript it does not say what kind of variable they are.
Thank you so much, I appreciate your time. I have now changed the things, that I had done wrong but the problem is that, when I use the $$anonymous$$atrix sv$$anonymous$$at = GUI.matrix;
variable it gives me this error. "The type or namespace name `$$anonymous$$atrix' could not be found. Are you missing a using directive or an assembly reference?" and then when I change the $$anonymous$$atrix to $$anonymous$$atrix4x4 like the Unity documentations say, I get this error while playing the game. "Ignoring invalid matrix assinged to GUI.matrix - the matrix needs to be invertible. Did you scale by 0 on Z-axis?" I am not quiet sure, what is wrong. By the way, remember to make it an answer so I can check as the right answer when finish.
Answer by Sir-Irk · Nov 18, 2014 at 12:40 AM
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.
Answer by grahnzz · Nov 17, 2014 at 09:41 PM
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: http://ilspy.net/
for syntax differences between UnityScript(JS) and C#: http://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax
Answer by Mosy · Nov 17, 2014 at 09:24 PM
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.
https://www.assetstore.unity3d.com/en/#!/content/20232
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can someone help me translate this to c#? 1 Answer
this script in Javascript?? 0 Answers
java to C# 2 Answers
java to C# conversion 1 Answer