// Build the uvs
var uvs : Vector2[] = new Vector2[GetComponent(MeshFilter).mesh.vertices.Length];
uvs = GetComponent(MeshFilter).mesh.uv; // Take the existing UVs
for (var i = 0 ; i < uvs.Length; i++) uvs[i] = Vector2 (uvs[i].x + xOffset, uvs[i].y + yOffset); // Offset all the UV's
// Apply the modded UV's
GetComponent(MeshFilter).mesh.uv = uvs;
// Blow this shit away
Destroy(this);
Works a treat until I remembered to put #pragma stuff at the beginning. It’s throwing ‘mesh’ is not a member of ‘UnityEngine.Component’.
// Build the uvs
var uvs : Vector2[] = new Vector2[(GetComponent(MeshFilter) as MeshFilter).mesh.vertices.Length];
uvs = (GetComponent(MeshFilter) as MeshFilter).mesh.uv; // Take the existing UVs
for (var i = 0 ; i < uvs.Length; i++) uvs[i] = Vector2 (uvs[i].x + xOffset, uvs[i].y + yOffset); // Offset all the UV's
// Apply the modded UV's
(GetComponent(MeshFilter) as MeshFilter).mesh.uv = uvs;
// Blow this shiz away
Destroy(this);
So here’s my ‘Offset the Mesh UV’s to avoid making instanced maps to change textures’ script. It feels wasteful, but its only called once on load, so… I don’t have a big problem with it.
Heh; I use this method a lot while using sprite animations / mesh deforms (even every-frame) now that I’m doin iPhone stuff and have to push that draw call count down
Actually, that’s precisely why I bothered to learn it… Need a single draw call gui soon, and I don’t feel like dealing with something that’s bloated and not my own (SpriteManager)
Could you elaborate more on this? I’m trying to do Zombieville type animation and I’m wondering if you’d extend this basic code to animate texture atlases…I’m looking to do something custom rather than the buy the SpriteManager.
Hello guys, I was just looking for a way to animate an explosion for my game.
I figure the best way was to use a texture atlas with all my explosion frames and animate the UV of a plane.
Well I’m using a variation of JTBentley’s scripts and it works pretty good when I have 1 explosion at the screen.
When I try to get 2 or more explosions at the same time, only the first plays. All others freeze at the first frame.
The code for animation is the following:
function Update(){
/// SOME CODE FOR DEFINING xIndex and yIndex
/// THIS IS WORKING AS EXPECTED
var _uvs : Vector2[] = new Vector2[uvs.length];
Debug.Log(Time.time + " :: " + xIndex + " :: " + yIndex);
for (var i = 0 ; i < _uvs.Length; i++){
_uvs[i] = Vector2 (uvs[i].x + xIndex, uvs[i].y + yIndex);
}
explosionMesh.uv = _uvs;
}
Through Debug.Log output I see that all explosions set xIndex and yIndex as expected, BUT the texture doesn’t change for more than 1 explosion.
Any clues?
[EDIT] :
the variable ‘uvs’ is set on Start:
function Start(){
uvs = new Vector2[explosionMesh.vertices.Length];
uvs = explosionMesh.uv;
}
[EDIT 2]:
Ok guys I found the problem. I was noobing and using GameObject.Find to get the Explosion Game Object I wanted to animate… Now I’m using transform.Find and the hierarchy in my GO’s is being respected.
I made this a while back and it’s now updated for Unity 3 and iPad: GUISpriteUI. It has the same idea as SpriteManger (single mesh, more around UV offsets and verts) but a much cleaner API and lots of built in controls. It’s also pretty easy to add your own custom controls.