- Home /
Translate and Update (from java to c# (4.6UI))
Hi Everyone,
I have found this java script code that basically plays a loop of images, my issue is that it hasn't been updated to support the new UI elements, I have limited knowledge of java script so I have attempted to do this in c#. Bellow is the original script and the second is my poor attempt at implementing it. Any help would be apreachetd. (my code doest work, it was just used to sudo code and block out how I thought I could achieve this)
var frames : Texture[];
var framesPerSecond = 10;
function Update() {
var index : int = (Time.time * framesPerSecond) % frames.Length;
renderer.material.mainTexture = frames[index];
}
my bad code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Animation : MonoBehaviour {
public Image wheretoputit;
public Image[] images;
int fps = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float index = (Time.time * fps) % images.Length;
wheretoputit.mainTexture = images [index];
}
}
Answer by HarshadK · May 15, 2015 at 11:00 AM
Instead of
wheretoputit.mainTexture = images [index];
use:
wheretoputit.sprite= images [index];
Thanks for the speedy respone. I have also ran into this issue on the line above :
Assets/Animation.cs(18,46): error CS0266: Cannot implicitly convert type
float' to
int'. An explicit conversion exists (are you missing a cast?)
Use int type for index rather than float as:
int index = (int)((Time.time * fps) % images.Length);
I do have one last question although it may be another question, I was using an "eventAlphaThreshold" to be able to click through the transparent part of the button earlier, but now this isn't working with the sprite. How can I make sure I click on the sprite but not the transparent area?
From the page UI.Image.eventAlphaThreshold it states a method:
In order for lower values to work, the sprite used by the Image must have readable pixels. This can be achived by enabling Read/Write enabled in the advanced Texture Import Settings for the sprite and disabling atlassing for the sprite.
Try that.
That is enabled, I don't think there is a sprite.eventAlphaThreshold thought