- Home /
Explain this script :) plz
var uvAnimationTileX = 24; //Here you can place the number of columns of your sheet.
//The above sheet has 24
var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet.
//The above sheet has 1
var framesPerSecond = 10.0;
function Update () {
// Calculate index
var index : int = Time.time * framesPerSecond;
// repeat when exhausting all frames
index = index % (uvAnimationTileX * uvAnimationTileY);
// Size of every tile
var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
// split into horizontal and vertical index
var uIndex = index % uvAnimationTileX;
var vIndex = index / uvAnimationTileX;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
var offset = Vector2 (uIndex * size.x, 2.0 - size.y - vIndex * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
Right can someone explain what few lines in this code mean? what does
renderer.material.SetTextureOffset ("_MainTex", offset); do ? I am new to unity when it comes to programming 2d sprites i found this on Unity Wiki by the way :)
the second is:
renderer.material.SetTextureOffset ("_MainTex", offset);
and also what does "_MainTex" do in this code?
and last:
What is this line of code doing ?????7
var offset = Vector2 (uIndex size.x, 2.0 - size.y - vIndex size.y);
so if someone can explain that to me i would be very great full thanks :)
Answer by efge · Feb 28, 2011 at 08:12 AM
This script cycles through the lines and columns of a sprite texture to get a flipbook animation effect. It then supplies the new u and v texture coordinates to the renderer.
_MainTex is the name of a texture in a shader, usually the main texture of the material.
Take a look at he reference:
Material.mainTexture
Material.SetTexture
Your answer
Follow this Question
Related Questions
Using Texture Assets in Code 1 Answer
A node in a childnode? 1 Answer
how do i make an object always face the player? 5 Answers
Image is crushed --- Sprite 2 Answers
Sprite animate on start ? 1 Answer