Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by L1vD3v · Nov 17, 2014 at 08:37 PM · c#javascriptconvertconversion

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · Nov 17, 2014 at 08:40 PM 1
Share

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.

avatar image meat5000 ♦ · Nov 17, 2014 at 08:48 PM 1
Share

Any errors? Debug output?

avatar image Sir-Irk · Nov 17, 2014 at 08:58 PM 1
Share

@L1vD3v Please look here

avatar image L1vD3v · Nov 17, 2014 at 10:09 PM 1
Share

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.

avatar image L1vD3v · Nov 18, 2014 at 12:11 AM 1
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image L1vD3v · Nov 18, 2014 at 02:45 PM 0
Share

Thank you very much, saved my day!

avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges