Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class animationControlScript : MonoBehaviour {
- private int frameAmount;
- private int animAmount;
- private float animCurrentFrame;
- private float frameReader = 0f;
- private int lastPlayedId = 0;
- public void animate (GameObject GO, int frameSizePixels, int animId, int realNumberOfFrames, int frameRate) {
- //get number of frames
- frameAmount = (GO.renderer.material.mainTexture.width / frameSizePixels);
- //get number of animations
- animAmount = (GO.renderer.material.mainTexture.height / frameSizePixels);
- //reset frame reader if playing a new animation
- if (lastPlayedId != animId) {
- frameReader = 0f;
- }
- //texture tiling - scale texture to fit number of animations
- GO.renderer.material.mainTextureScale = new Vector2(1.0f / frameAmount, 1.0f / animAmount);
- //frame reading
- GO.renderer.material.mainTextureOffset = new Vector2(frameReader, (1.0f * (animAmount - animId) / animAmount));
- //move frame reader to next frame by the frame rate selected
- if (Time.fixedTime - animCurrentFrame >= (frameRate * 1.0f) / 30.0f ) {
- animCurrentFrame = Time.fixedTime;
- frameReader += 1.0f/frameAmount;
- //if the frame reader is past the existing frame number, return to frame 1
- if (frameReader >= realNumberOfFrames * 1.0f / frameAmount * 1.0f) {
- frameReader = 0f;
- }
- }
- lastPlayedId = animId;
- }//animate end
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement