305x260 png 70 images used for texture animation, Below code explains
var textures : Texture[ ];
var index : int = Time.time / changeInterval;
index = index % textures.length;
texture = textures[index];
renderer.material.mainTexture = textures[index];
var shader2 = Shader.Find(“Transparent/Diffuse”);
renderer.material.shader = shader2;
But it takes lot of cpu usage. app failed because of that.
Any thing wrong i am doing here. Please help me.
There are a lot of places for improving performance here.
First, unless you only have a single object that has a texture animation (and really not even then), you generally don’t want to alter the material in any way at runtime. Doing so will cause Unity to create a new material, meaning the object cannot be batched with other objects, even if it shares all the same settings with those objects. This means more draw calls, and that eats a ton of performance.
Second, you never want to use any kind of “Find” method at runtime other than in initialization at the very start of your game. Find* methods are very slow.
Third, 70 305x260 images are going to eat a TON of memory and I wouldn’t be surprised if this is what’s crashing your game. This can be significantly reduced by using PVRTC compression, but even then, that’s a LOT. See if there are any ways you can reduce the size of your textures, and then if you can get it small enough, you can combine them onto a single texture atlas, and use various methods to animate it from there by changing the UVs at a given rate.
If you can get things down to a texture atlas (or even a few of them), SpriteManager (see the wiki) might be of some help as it can help you handle the animating of it in a much more performance-friendly manner.
Also, you may also want to check out SpriteManager 2 (SM2) which, depending on what’s on your 70 images, might be able to further help you get your content condensed onto a single atlas without requiring lots of work. You can see some videos on it here: http://www.anbsoft.com/middleware/sm2
I created the textureAtlas using i3d tool. I dont have an clear idea, how to use this textureAtlas for extract each frame and create animations in unity.
It won’t help you build an atlas and some other things, but it will handle the changing of the UVs from one sprite to the next on an atlas.
An atlas is a single texture (which is a max size of 1024x1024 on iPhone) that contains several images on it. If you cannot reduce your images down to at least fit on a few atlases, you are going to keep running into problems with memory. See if you can shrink your images down and fit them all together onto a single texture, or on a few textures. Then you can use something like SM1 to animate by switching the UVs to point to different images on the atlas.
I done with UVanimation, when i look at the animation, the images are blurred.
var index : int = Time.time * framesPerSecond;
// repeat when exhausting all frames
index = index % (uvAnimationTileX *uvAnimationTileY);
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, 1.0 - size.y - vIndex * size.y);
That looks good from what I can tell without actually running it.
Blurriness can be a result of PVRTC compression, but I’m not sure if that’s what you’re referring to.
Also, be aware that if you alter a material’s properties, including uv offset and scale, Unity treats that material as a new one. So it won’t batch with other objects that use the same material. Though if you’re only doing this on a single object anyway, that won’t matter.
Sm2 is a great tools! Great Job Brady. But for this case is this will help? Like: Murali mentioned that image size is 305x260 which mean, 1024x1024 atlas will able to contain max 9 sub-texture. He have to change the texture of a material after 9 frame. So,
But it will help a lot that’s for sure. For one of my project I need to use 450 images size of 320x480. I used Cocoa API. For my case I didn’t need any 3D stuff. What I did is:
Load all images in memory as NSData.
Create UIImage at run time from NSData.
Frame rate was max 10. May be you can use a trick in Unity as well.
Store all images in Resources directory.
Load each image at run time.
Use it in the animation.
I don’t know your purpose exactly but you can use this kind of tricks.
Right, it definitely may not help in this particular case. My thinking was that there are cases where you may not actually need frames of that size if you had a way to reduce unneeded alpha pixels that you may otherwise have in there to keep the frames uniform, etc. SM2 would automatically strip that out. But if you actually have to have all 305x260 for each frame, you’d have to switch atlases constantly.
So are you saying with the Cocoa approach you got a max FPS of 10?
Yup, using native API and UIView canvas for drawing, I did the 450 image key frame animation with FPS 8-10. In case of threading, it sometimes goes to 15 but not constant. iPhone 3G was my device.