Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by deeredman1991 · Jul 10, 2011 at 05:13 PM · animationconvert

Need help converting js to C#

hey guys I'm trying to convert a js script to a c# script because my movement system is in c# and it would be alot simpler if this function were in c# then to rescript my entire movement and combat system in js so I was wondering if someone could help me out...I've already done a good piece of it...I just need probably the last few lines (I'm having a hard time understanding exactly what Vectors are...that could have something to do with it)

ok this is the original js code

 //vars for the whole sheet
 var colCount    : int =  3;
 var rowCount    : int =  4;
 
 //vars for animation
 var rowNumber   : int =  0; //Zero Indexed
 var colNumber   : int =  0; //Zero Indexed
 var totalCells  : int =  3;
 var fps  : int = 10;
 var offset  : Vector2;  //Maybe this should be a private var
 
 //Update
 function Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);  }
 
 //SetSpriteAnimation
 function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int){
 
     // Calculate index
     var index : int = Time.time * fps;
     // Repeat when exhausting all cells
     index = index % totalCells;
    
     // Size of every cell
     var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
    
     // split into horizontal and vertical index
     var uIndex = index % colCount;
     var vIndex = index / colCount;
  
     // build offset
     // v coordinate is the bottom of the image in opengl so we need to invert.
     offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
    
     renderer.material.SetTextureOffset ("_MainTex", offset);
     renderer.material.SetTextureScale  ("_MainTex", size);
 }

and this is the C# code

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAnimation : MonoBehaviour 
 {
     //vars
     int colcount = 3;
     int rowcount = 4;
     int rownum = 0;
     int colnum = 0;
     int totalcells = 3;
     int fps = 10;
     Vector2 offset;
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         spriteanimation(colcount,rowcount,rownum,colnum,totalcells,fps);
     }
     
 IEnumerator spriteanimation(int colcount,int rowcount,int rownum,int colnum,int totalcells,int fps)
     {
         float index = Time.time * fps;
         index = index % totalcells;
         
         int size = Vector2(1.0 / colcount, 1.0 / rowcount);
         
         float uindex = index % colcount;
         float vindex = index / colcount;
         
         offset = Vector2((uindex + colnum) * size.x, (1.0 - size.y) - (vindex+rownum) * size.y);
         
         renderer.material.SetTextureOffset ("_MainTex", offset);
         renderer.material.SetTextureScale ("_MainTex", size);
     }
 }
Comment
Add comment · Show 3
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 Bunny83 · Jul 10, 2011 at 06:07 PM 1
Share

Next time format your code. Just select the code and press the "101 010" button. I've done it for you.

Oh, and don't forget to up-vote / accept an answer if it's helpful.

avatar image deeredman1991 · Jul 10, 2011 at 06:52 PM 0
Share

thanks bunny I'll remember this for the future about the up voting...but what do you mean format a code and press 101 010?

avatar image deeredman1991 · Jul 10, 2011 at 06:54 PM 0
Share

oh nvm...figured it out...thanks :)

1 Reply

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

Answer by GuyTidhar · Jul 10, 2011 at 05:39 PM

Here you go:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAnimation : MonoBehaviour 
 {
    //vars 
     int colcount = 3; 
     int rowcount = 4; 
     int rownum = 0; 
     int colnum = 0; 
     int totalcells = 3; 
     int fps = 10; 
     Vector2 offset; 
 
     void Update () 
     {
         spriteanimation(colcount,rowcount,rownum,colnum,totalcells,fps);
     }
 
     private void spriteanimation(int colCount,int rowCount,int rowNumber,int colNumber,int totalCells,int fps) 
     { 
         // Calculate index
         int index = (int)(Time.time * (float)fps);
         // Repeat when exhausting all cells
         index = index % totalCells;
         
         // Size of every cell
         Vector2 size = new Vector2 (1.0f / colCount, 1.0f / rowCount);
         
         // split into horizontal and vertical index
         int uIndex = index % colCount;
         int vIndex = index / colCount;
         
         // build offset
         // v coordinate is the bottom of the image in opengl so we need to invert.
         offset = new Vector2 ((uIndex+colNumber) * size.x, (1.0f - size.y) - (vIndex+rowNumber) * size.y);
         
         renderer.material.SetTextureOffset ("_MainTex", offset);
         renderer.material.SetTextureScale  ("_MainTex", size);
     }    
 }
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 deeredman1991 · Jul 10, 2011 at 05:43 PM 0
Share

awesome thanks :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Can I make animations snap to a frame? 1 Answer

Could you help me convert this script to C#? 1 Answer

cannot implicitly convert type void to bool 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