Hello Folks, Im creating an application with Google VR SDK. I have a button that has a animated sequence of maps that simulates the animation of loading, when i look the button this animated secuence activated. The problem I have is when I remove the look of the button the texture freeze on the map from where you remove the look of the button, what I need to happen is when I remove the look of the button, the animated secuence returns to the initial texture.
using UnityEngine; // 41 Post - Created by DimasTheDriver on Apr/20/2012 . Part of the 'Unity: Animated texture from image sequence' post series. Available at: http://www.41post.com/?p=4742
using System.Collections; //Script featured at Part 2 of the post series.
public class ImageSequenceSingleTexture : MonoBehaviour
{
//A texture object that will output the animation
private Texture texture;
//With this Material object, a reference to the game object Material can be stored
private Material goMaterial;
//An integer to advance frames
private int frameCounter = 0;
//A string that holds the name of the folder which contains the image sequence
public string folderName;
//The name of the image sequence
public string imageSequenceName;
//The number of frames the animation has
public int numberOfFrames;
//The base name of the files of the sequence
private string baseName;
void Awake()
{
//Get a reference to the Material of the game object this script is attached to
this.goMaterial = this.GetComponent<Renderer>().material;
//With the folder name and the sequence name, get the full path of the images (without the numbers)
this.baseName = this.folderName + "/" + this.imageSequenceName;
}
void Start ()
{
//set the initial frame as the first texture. Load it from the first image on the folder
texture = (Texture)Resources.Load(baseName + "00000", typeof(Texture));
}
void Update ()
{
//Start the 'PlayLoop' method as a coroutine with a 0.04 delay
StartCoroutine("PlayLoop", 0.08f);
//Set the material's texture to the current value of the frameCounter variable
goMaterial.mainTexture = this.texture;
}
//The following methods return a IEnumerator so they can be yielded:
//A method to play the animation in a loop
IEnumerator PlayLoop(float delay)
{
//wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//advance one frame
frameCounter = (++frameCounter)%numberOfFrames;
//load the current frame
this.texture = (Texture)Resources.Load(baseName + frameCounter.ToString("D5"), typeof(Texture));
//Stop this coroutine
StopCoroutine("PlayLoop");
}
//A method to play the animation just once
IEnumerator Play(float delay)
{
//wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//if it isn't the last frame
if(frameCounter < numberOfFrames-1)
{
//Advance one frame
++frameCounter;
//load the current frame
this.texture = (Texture)Resources.Load(baseName + frameCounter.ToString("D5"), typeof(Texture));
}
//Stop this coroutine
StopCoroutine("Play");
}
}
How are you currently detecting that you are “not looking” at the button? Since the animation stops when you look away, you know your trigger point of when you can reset it back.
It looks like you have a variable called framecounter that is handling the animation, so you will have to reset that back to 0. Also, in your start it seems you are loading the first image, so you’ll need to set that back to the starting image.
So basically when you “look away” from the button, reset your image and the counter and I think that should do it.
Thank Brathann, i almost have the script working. The only thing i cant make yet is reset the animation secuence material to the 0 frame when i enter to the trigger area for second time (its continue from the last frame before i look out of the buton trigger area), any idea how can i do this on the script or adding Event Trigger???
Avance video
using UnityEngine; // 41 Post - Created by DimasTheDriver on Apr/20/2012 . Part of the 'Unity: Animated texture from image sequence' post series. Available at: http://www.41post.com/?p=4742
using System.Collections; //Script featured at Part 2 of the post series.
public class ImageSequenceSingleTexture : MonoBehaviour
{
//A texture object that will output the animation
private Texture texture;
//With this Material object, a reference to the game object Material can be stored
private Material goMaterial;
//An integer to advance frames
private int frameCounter = 0;
//A string that holds the name of the folder which contains the image sequence
public string folderName;
//The name of the image sequence
public string imageSequenceName;
//The number of frames the animation has
public int numberOfFrames;
//The base name of the files of the sequence
private string baseName;
public void OnPointerEnter()
{
//Get a reference to the Material of the game object this script is attached to
this.goMaterial = this.GetComponent<Renderer>().material;
//With the folder name and the sequence name, get the full path of the images (without the numbers)
this.baseName = this.folderName + "/" + this.imageSequenceName;
}
void Start ()
{
//set the initial frame as the first texture. Load it from the first image on the folder
texture = (Texture)Resources.Load(baseName + "00000", typeof(Texture));
}
void Update ()
{
//Start the 'PlayLoop' method as a coroutine with a 0.04 delay
StartCoroutine("Play", 0.08f);
//Set the material's texture to the current value of the frameCounter variable
goMaterial.mainTexture = this.texture;
}
//The following methods return a IEnumerator so they can be yielded:
//A method to play the animation in a loop
IEnumerator PlayLoop(float delay)
{
//wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//advance one frame
frameCounter = (++frameCounter)%numberOfFrames;
//load the current frame
this.texture = (Texture)Resources.Load(baseName + frameCounter.ToString("D5"), typeof(Texture));
//Stop this coroutine
StopCoroutine("Play");
}
//A method to play the animation just once
IEnumerator Play(float delay)
{
//wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//if it isn't the last frame
if(frameCounter < numberOfFrames-1)
{
//Advance one frame
++frameCounter;
//load the current frame
this.texture = (Texture)Resources.Load(baseName + frameCounter.ToString("D5"), typeof(Texture));
}
//Stop this coroutine
StopCoroutine("Play");
}
}
This line seems to suggest it loads the first image. So when your cursor exits the button, you should be able to set the texture back to that value. Then when update runs, it should target the first texture. The play coroutine should then run to update the texture creating animated effect. FrameCounter is what is used to determine what image to load for the animation, which is why it’s set back to 0.
public void OnPointerExit()
{
//Get a reference to the Material of the game object this script is attached to
this.goMaterial = this.GetComponent<Renderer>().material;
//With the folder name and the sequence name, get the full path of the images (without the numbers)
this.baseName = this.folderName + "/" + this.imageSequenceName;
frameCounter = 0;
Note that in the last line i adding frame counter = 0, it seems this line reset the animation secuence to the firt frame. After i add a event trigger on pointer exit and finally it works.
the final script
using UnityEngine; // 41 Post - Created by DimasTheDriver on Apr/20/2012 . Part of the 'Unity: Animated texture from image sequence' post series. Available at: http://www.41post.com/?p=4742
using System.Collections; //Script featured at Part 2 of the post series.
public class ImageSequenceSingleTexture : MonoBehaviour
{
//A texture object that will output the animation
private Texture texture;
//With this Material object, a reference to the game object Material can be stored
private Material goMaterial;
//An integer to advance frames
private int frameCounter = 0;
//A string that holds the name of the folder which contains the image sequence
public string folderName;
//The name of the image sequence
public string imageSequenceName;
//The number of frames the animation has
public int numberOfFrames;
//The base name of the files of the sequence
private string baseName;
public void OnPointerEnter()
{
//Get a reference to the Material of the game object this script is attached to
this.goMaterial = this.GetComponent<Renderer>().material;
//With the folder name and the sequence name, get the full path of the images (without the numbers)
this.baseName = this.folderName + "/" + this.imageSequenceName;
}
void Start ()
{
//set the initial frame as the first texture. Load it from the first image on the folder
texture = (Texture)Resources.Load(baseName + "00000", typeof(Texture));
}
public void OnPointerExit()
{
//Get a reference to the Material of the game object this script is attached to
this.goMaterial = this.GetComponent<Renderer>().material;
//With the folder name and the sequence name, get the full path of the images (without the numbers)
this.baseName = this.folderName + "/" + this.imageSequenceName;
frameCounter = 0;
}
void Update ()
{
//Start the 'PlayLoop' method as a coroutine with a 0.04 delay
StartCoroutine("Play", 0.08f);
//Set the material's texture to the current value of the frameCounter variable
goMaterial.mainTexture = this.texture;
}
//A method to play the animation just once
IEnumerator Play(float delay)
{
//wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//if it isn't the last frame
if(frameCounter < numberOfFrames-1)
{
//Advance one frame
++frameCounter;
//load the current frame
this.texture = (Texture)Resources.Load(baseName + frameCounter.ToString("D5"), typeof(Texture));
}
//Stop this coroutine
StopCoroutine("Play");
}
}