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/TileUVAnimation/TextureTileAnimation.cs(34,90): error CS1502: The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments

Assets/Scripts/TileUVAnimation/TextureTileAnimation.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 106 72 76

(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 4 14 53

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

I found some info here that may be helpful, from the comment on http://forum.unity3d.com/threads/28766-Quick-Coroutine-amp-yield-C-question :

the C# manual page does give an example (but does not explain anything): http://unity3d.com/support/documentation/ScriptReference/index.WritingScriptsin_Csharp.html In fact, the page even forgets to mention that C# coroutines don't work unless you explicitly use StartCoroutine() to start them.

also : http://www.altdevblogaday.com/2011/07/07/unity3d-coroutines-in-detail/

May 09, 2012 at 02:14 PM alucardj

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

That's true, well spotted. I have changed this and unfortunately it gives the same result. I have also updated the C# script above to include this.

Any other ideas would be great. Could this be a problem with the logic/syntax used when converting it from JavaScript to C# ?

May 09, 2012 at 05:22 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:

x9881
x6171

asked: May 09, 2012 at 01:50 PM

Seen: 1722 times

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