• 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
0
Question by BryceK · Jul 08, 2013 at 02:04 AM · c#converting

Need help converting javascript to c#

Hi guys,

I'm not a programmer so I got no clue how to really do it hehe.

Could anyone help me convert this?

 #pragma strict
 
 var row:int = 0;
 var column:int = 0;
 
 var xOffset:float; 
 var yOffset:float;
 var offset:Vector2;
 
 private var realX:float;
 private var realY:float;
 
 var mesh:MeshFilter;
 
 var uvs:Vector2[];
 
 var originalUVs:Vector2[];
 
 function Start () {
 //Get mesh to edit
 mesh = GetComponent(MeshFilter);
 originalUVs = new Vector2[mesh.mesh.uv.length];
 originalUVs = mesh.mesh.uv;
 
 //initialize new array
 uvs = new Vector2[mesh.mesh.uv.length];
 offset = Vector2(xOffset,yOffset);
 
 for(var i:int = 0; i<mesh.mesh.uv.length; i++){
 if(mesh.mesh.uv[i].x>0&&mesh.mesh.uv[i].y<1
 &&mesh.mesh.uv[i].x < 1 &&mesh.mesh.uv[i].y>0){
 mesh.mesh.uv[i].x = 1-mesh.mesh.uv[i].x;
 mesh.mesh.uv[i].y = 1-mesh.mesh.uv[i].y;
 }
 }
 
 for(var j:int = 0;j<mesh.mesh.uv.length; j++){
 uvs[j] = originalUVs[j] + offset;
 }
 
 mesh.mesh.uv = uvs;
 }
 
 function Update () {
 offset = Vector2(xOffset,yOffset);
 
 for(var i:int = 0; i<mesh.mesh.uv.length; i++){
 uvs[i] = originalUVs[i] + offset;
 }
 
 mesh.mesh.uv = uvs;
 }

Thanks in advance :)

Comment
Add comment · Show 2
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 ExpiredIndexCard · Jul 08, 2013 at 02:05 AM 0
Share

What did you need help converting? Did you even attempt to change to c#?

avatar image BryceK · Jul 08, 2013 at 02:31 AM 0
Share

Yes, For over hours but since i'm not a programmer I don't have a clue what to do...

1 Reply

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

Answer by aldonaletto · Jul 08, 2013 at 02:36 AM

There are several pitfalls in converting this code to C#: the default scope for variables is public in JS and private in C#, GetComponent has a different syntax, the array property length must be capitalized and Vector2 requires a new keyword. Besides this, a C# script must be explicitly declared as a class derived from MonoBehaviour - remember to replace YourScriptName in the class declaration below by the actual name you're saving this script (the file name must match the class name):

 using UnityEngine;
 using System.Collections;
 
 public class YourScriptName : MonoBehaviour {
 
     // variables are public by default in JS, but are private in C#:
     public int row = 0; 
     public int column = 0;
      
     public float xOffset; 
     public float yOffset;
     public Vector2 offset;
      
     float realX;
     float realY;
      
     MeshFilter mesh;
      
     Vector2[] uvs;
      
     Vector2[] originalUVs;
      
     void Start () {
         //Get mesh to edit - GetComponent is different in C#:
         mesh = GetComponent<MeshFilter>();
         // the array property "length" may start with lower case
         // in JS, but must be capitalized in C#
         originalUVs = new Vector2[mesh.mesh.uv.Length];
         originalUVs = mesh.mesh.uv;
         //initialize new array
         uvs = new Vector2[mesh.mesh.uv.Length];
         // Vector2/3/4 require a keyword "new" in C#:
         offset = new Vector2(xOffset,yOffset);
          
         for (int i = 0; i<mesh.mesh.uv.Length; i++){
             if(mesh.mesh.uv[i].x>0 && mesh.mesh.uv[i].y<1
             && mesh.mesh.uv[i].x < 1 && mesh.mesh.uv[i].y>0){
                 mesh.mesh.uv[i].x = 1-mesh.mesh.uv[i].x;
                 mesh.mesh.uv[i].y = 1-mesh.mesh.uv[i].y;
             }
         }
          
         for (int j = 0; j<mesh.mesh.uv.Length; j++){
             uvs[j] = originalUVs[j] + offset;
         }
          
         mesh.mesh.uv = uvs;
     }
      
     void Update () {
         offset = new Vector2(xOffset,yOffset);
          
         for(int i = 0; i<mesh.mesh.uv.Length; i++){
             uvs[i] = originalUVs[i] + offset;
         }
          
         mesh.mesh.uv = uvs;
     }
 }
Comment
Add comment · Show 3 · 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 BryceK · Jul 08, 2013 at 02:44 AM 0
Share

Thanks this really helps :) Now I hope I'm able to add some things to it with my 0.0 skill hehe

avatar image aldonaletto · Jul 08, 2013 at 03:34 AM 0
Share

One more hint: if a number has a decimal point, you must add an F or f to it to specify that it's a float, or the C# compiler will throw enigmatic and undecipherable error messages:

 float someFloat = 1.0f;
 float otherFloat = 0.5f;
avatar image BryceK · Jul 08, 2013 at 03:55 AM 0
Share

Thank you very much for the information, I really appreciate it :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Dress Problem 0 Answers

Line Renderer Positioning Problem 1 Answer

Rays and tags help? 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