Fixing JS script to CS

Ive got an old asset that uses a JS uv scrolling script Im trying to convert to CS. Ive done an initial conversion but I have some errors I cant figure out. Can anyone help id appreciate immensely.


type or paste code here

using UnityEngine;
using System.Collections;

public class AnimatedTextures : MonoBehaviour {
FIXME_VAR_TYPE uvAnimationTileX= 24; //Here you can place the number of columns of your sheet. 
                           //The above sheet has 24
 
FIXME_VAR_TYPE uvAnimationTileY= 1; //Here you can place the number of rows of your sheet. 
                          //The above sheet has 1
FIXME_VAR_TYPE framesPerSecond= 10.0f;
 
void  Update (){
 
	// Calculate index
	int index = Time.time * framesPerSecond;
	// repeat when exhausting all frames
	index = index % (uvAnimationTileX * uvAnimationTileY);
 
	// Size of every tile
	FIXME_VAR_TYPE size= Vector2 (1.0f / uvAnimationTileX, 1.0f / uvAnimationTileY);
 
	// split into horizontal and vertical index
	FIXME_VAR_TYPE uIndex= index % uvAnimationTileX;
	FIXME_VAR_TYPE vIndex= index / uvAnimationTileX;
 
	// build offset
	// v coordinate is the bottom of the image in opengl so we need to invert.
	FIXME_VAR_TYPE offset= Vector2 (uIndex * size.x, 1.0f - size.y - vIndex * size.y);
 
	GetComponent.<Renderer>().material.SetTextureOffset ("_MainTex", offset);
	GetComponent.<Renderer>().material.SetTextureScale ("_MainTex", size);
}

I assume all those FIXME_VAR_TYPE just require you to put in the appropriate variable type, such as int or float.

If there’s more errors, you kinda need to tell us what those are.

Im trying to follow you here. Are you saying the fixed variables have to have code added in the code (int and or float)? I converted this JS script into CS using a js to cs converter. I can post the errors im getting from script in unity if you like.

I did just say:

Assets\Tools\Realistic Water Fountain\Prefabs\AnimatedTextures.cs(32,2): error CS1513: } expected

Assets\Tools\Realistic Water Fountain\Prefabs\AnimatedTextures.cs(31,15): error CS1001: Identifier expected

Assets\Tools\Realistic Water Fountain\Prefabs\AnimatedTextures.cs(30,26): error CS1525: Invalid expression term ')'

I’m just going to fix the code because its easier than going back and forth continually:

using UnityEngine;

public class AnimatedTextures : MonoBehaviour
{
	[SerializeField]
	private int uvAnimationTileX = 24; //Here you can place the number of columns of your sheet. The above sheet has 24

	[SerializeField]
	private int uvAnimationTileY = 1; //Here you can place the number of rows of your sheet. The above sheet has 1

	[SerializeField]
	private float framesPerSecond = 10.0f;

	[SerializeField]
	private Renderer _renderer;

	private Material _material;

	private void Awake()
	{
		_material = _renderer.material;
	}

	private void OnDestroy()
	{
		Object.Destroy(_material);
	}

	void Update()
	{
		// Calculate index
		float index = Time.time * framesPerSecond;
		// repeat when exhausting all frames
		index %= (uvAnimationTileX * uvAnimationTileY);

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

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

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

		_material.SetTextureOffset("_MainTex", offset);
		_material.SetTextureScale("_MainTex", size);
	}
}

The old code had a memory leak as well by accessing the material of a renderer and not cleaning up the instanced material it makes. Also we can just avoid the repeated GetComponent<T> calls by having a serialized reference to the Renderer component we care about.

No idea if this does what’s it is meant to, of course. Haven’t tested it.

1 Like

Gonna check it out. Ill report back.

That looks to be correct. I can see the textures now moving however they are spinning in a circle rapidly and the settings on the script dont have much control to change them. What this is is a script for UV’s on mesh for a water fountain. There are about 5 meshes and 3 of them use the same script twice at different settings/frame rates. Im gonna mess with settings and see if I cant get em to work

By the way thx. Saved a bunch of time. :slightly_smiling_face:

1 Like