x


Conversion to C# please - Appears to be a tricky one

Hi, I was wondering if someone can please convert the following files into C#. I have tried to do this but am getting errors. Thanks

File 1:

 using UnityEngine;
 using System.Collections;
 
 public class Texture_Tile_Animation : MonoBehaviour
 {
     public int uvAnimationTileX = 4;
     public int uvAnimationTileY = 4;
     public float framesPerSecond = 10.0F;
 
     private float index;
 
     private Vector2 size;
     private Vector2 offset;
 
     private float uIndex;
     private float vIndex;
 
     void Start() 
     {
         StartCoroutine(DoTiling());
     }
 
     IEnumerator DoTiling() 
     {
         while(true) 
         {
             // Calculate index
             index = Time.time * framesPerSecond;
             // repeat when exhausting all frames
             index = index % (uvAnimationTileX * uvAnimationTileY);
         
             // Size of every tile
             size = new Vector2 ( 1.0f / uvAnimationTileX, 1.0f / uvAnimationTileY );
         
             // split into horizontal and vertical index
             uIndex = index % uvAnimationTileX;
             vIndex = index / uvAnimationTileX;
         
             // build offset
             // v coordinate is the bottom of the image in opengl so we need to invert.
             offset = new Vector2 ( uIndex * size.x, 1.0f - size.y - vIndex * size.y );
         
             renderer.material.SetTextureOffset ("_MainTex", offset);
             renderer.material.SetTextureScale ("_MainTex", size);
     
             yield return new WaitForEndOfFrame();
         }
     }
 }

Errors are:

Assets/_Scripts/_Tile_UV_Animation/Texture_Tile_Animation.cs(34,90): error CS1502: The best overloaded method match for UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments Assets/_Scripts/_Tile_UV_Animation/Texture_Tile_Animation.cs(34,90): error CS1503: Argument #1' cannot convert double' expression to type float'

//---------- Original JavaScript file:

     #pragma strict
 
 var uvAnimationTileX : int = 4;
 var uvAnimationTileY : int = 4;
 var framesPerSecond : float = 10.0;
 
 private var index : int;
 
 private var size : Vector2;
 private var offset : Vector2;
 
 private var uIndex : float;
 private var vIndex : float;
 
 function Start() {
     DoTiling();
 }
 
 function DoTiling() {
 
     while(true) {
         // Calculate index
         index = Time.time * framesPerSecond;
         // repeat when exhausting all frames
         index = index % (uvAnimationTileX * uvAnimationTileY);
         
         // Size of every tile
         size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
         
         // split into horizontal and vertical index
         uIndex = index % uvAnimationTileX;
         vIndex = index / uvAnimationTileX;
         
         // build offset
         // v coordinate is the bottom of the image in opengl so we need to invert.
         offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
         
         renderer.material.SetTextureOffset ("_MainTex", offset);
         renderer.material.SetTextureScale ("_MainTex", size);
     
         yield WaitForEndOfFrame();
     }
 }

more ▼

asked May 09, 2012 at 01:50 PM

asimov gravatar image

asimov
187 107 74 79

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

// Size of every tile --> size = new Vector2 ( 1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY );

        // split into horizontal and vertical index
        uIndex = index % uvAnimationTileX;
        vIndex = index / uvAnimationTileX;

        // build offset
        // v coordinate is the bottom of the image in opengl so we need to invert.
  -->          offset = new Vector2 ((uIndex * size.x), (1.0 - size.y - vIndex * size.y));


On those two lines, try adding "f" next to the 1.0 (1.0f) and see how that goes.

more ▼

answered May 09, 2012 at 01:56 PM

Lo0NuhtiK gravatar image

Lo0NuhtiK
6.4k 5 17 58

Thanks for the suggestion, That-1-CraZy-BastarD. I've tried this and it gives very strange results. I have a feeling that the 'yield return new WaitForEndOfFrame()' may not be working. Is there another way to test for this, as the JavaScript version works fine?

May 09, 2012 at 02:05 PM asimov

Well, the error you posted refers to the line
size = new Vector2(1.0 / uvAnimationTileX, 1.0 /uvAnimationTileY) ;
and it's because Vector2 needs two floats, and in that line you're making two errors with 1.0 not being a float and dividing it by a float. If you change those to 1.0f you wont get those errors... but, then the error will happen again on the next line that I pointed out, so that will need changed to 1.0f also. After you change that, then see if you get errors about anything else.

May 09, 2012 at 02:10 PM Lo0NuhtiK

OK, well tested the script using 'yield return new WaitForSeconds(1)' and it gives the same odd results just slower. So I guess the yield statement is working meaning that the error is elsewhere.

Just to provide more information, the working script simply moves through 6 sprites on a plane - the effect is a rotation of the sprite in the centre of the plane. The script that is in error doesn't keep the sprite in the middle, but moves all over the plane. Not sure if that helps any.

May 09, 2012 at 02:39 PM asimov

Maybe it's not running the same in your C# script now because your framesPerSecond variable is an int in your C# whereas it's a float in the UnityScript.

May 09, 2012 at 02:42 PM Lo0NuhtiK

That's true, well spotted, just changed that and updated the C# script I'm currently working with above. Gives the same results unfortunately.

May 09, 2012 at 03:07 PM asimov
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x10184
x6253

asked: May 09, 2012 at 01:50 PM

Seen: 1768 times

Last Updated: May 10, 2012 at 09:18 PM