Loading sequence of images for animation Using prefabs

Hi Guys,
Im trying to create the sprite animation game i have about 300+ images for one character which are distributed between 12 different animations. The image count for the animations differs from animation to animation. The min number of images for a animations is 20 frames/png. and the max image sequence for an animation is 69 frames. The Images resolutions are different ranging from 512x512 to 256x256 (Note the width height remains same for the same animation)

Since there are too may images and we cannot load all the images at the same time im trying to load them using prefabs and destroy the prefabs when the animation is completed.

The Actual scene is just having one Camera and one script attached to it called AniamtionHandeler.cs. This Script is creating an clone of the prefab object which is attached to it as public. when the button is pressed. The prefab is having a common script attached to it, which plays the animation and when the animation is completed its getting destroyed.

Now since im using the prefabs im expecting the images to load runtime as this will save loads of memory. but I think this is not happening at all. im using DetectLeaks.cs which is available on the wiki. im attaching the images of the output.

One more thing which i have noticed now is that the application is working well in 3GS and its not loading (Crash “Low Memory” in crash log) on other devices its obviously due to the improved hardware.

Can anyone help me with the problem i have tried every possible solution which i can think of.

[b]__AnimationHandler.cs__[/b]
public GameObject NormalObj;//Taking the Prefab Object : this is nothing but a GUI Texture used For showing the Aniamtion

public GameObject FireAniObj; //	Taking the Prefab Object : this is nothing but a GUI Texture used For showing the Aniamtion

Object	AnimationPrefabObj;
Object	DefaultObject;

void OnGUI () {
		
		GUILayout.Label("Actual Memory "+ SystemInfo.graphicsMemorySize);
		GUILayout.Label("All " + FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length);
        GUILayout.Label("Textures " + FindObjectsOfTypeAll(typeof(Texture)).Length);
        GUILayout.Label("AudioClips " + FindObjectsOfTypeAll(typeof(AudioClip)).Length);
        GUILayout.Label("Meshes " + FindObjectsOfTypeAll(typeof(Mesh)).Length);
        GUILayout.Label("Materials " + FindObjectsOfTypeAll(typeof(Material)).Length);
        GUILayout.Label("GameObjects " + FindObjectsOfTypeAll(typeof(GameObject)).Length);
        GUILayout.Label("Components " + FindObjectsOfTypeAll(typeof(Component)).Length);
       
Rect MyRect = new Rect( 405, 5, 70, 35);

if ( GUI.Button(MyRect, "Slam" ) ){ //	Creating the Button
			if ( MyClone == null)	{ // if the clone is already existing than dont clone it 
				Vector3 TempPos = new Vector3(0, 0, 0); //	Setting the Position
				MyClone	= Instantiate(SlamAniObj, TempPos, Quaternion.identity);//	Creating the clone of the Prafab
				DestroyImmediate (MyBasicClone);	//	Deleting the Prefab Object
			}
		}


// Update is called once per frame
	void Update () {
		if ( MyClone == null)	{
			if(MyBasicClone == null)	{
				Vector3 TempPos = new Vector3(0.5f, 0.5f, 0);	//	Setting the Position
				MyBasicClone	= Instantiate(NormalObj, TempPos, Quaternion.identity);//	Creating the clone of the Prafab
			}
		}
	}

The Above code is acting as my Main Class which controls the Creation/cloning of prefabs when no other prefab is already created/cloned.

[b]__PrefabAnimation.cs__[/b]

public	Texture[] AnimationImages;//Array of images approx 64 images for the Animation of 512x512 / 256x256 / 512x256 / 256x512

void Update () {
		if ( m_fMyTimer < Time.time ) { //	Checking the Timer for next change of image
			if ( m_iCount < AnimationImages.Length-1 ){//	Checking the length of the Image array if current count is less thean the size of the array than changing the image 
				guiTexture.texture	= AnimationImages[m_iCount];//Loading the next images in the array
				m_fMyTimer	= Time.time	+ m_fWaitTime;//	Updating the timer for next change
				m_iCount++;//Setting the Next Index for loading the new img
				
			}
			else{//	If we have completed the aniamtion than deleating the Game object from the scene (Note this is a prefab object created on the fly from another Script )
				Destroy(gameObject);//	Deleting the Prefab object
			}
		}
	}//	End of Update

Image 1: This is when the Prefabs Is cloned (You can see the compound count getting set to 34)

Image 2: This is when the Prefabs clone object is deleted (You can see the compound count getting set to 33)

Note : This are the actual reading from the device (iphone 3GS)``

232773--8338--$screen_shot_2009_12_01_at_81359_pm_946.png
232773--8339--$screen_shot_2009_12_01_at_81504_pm_999.png

I didn’t actually understand the question. Were you asking how to prevent your project crashes on older iDevices?

There are 3 things which i wanted to ask

1> When im trying to load the images run time using prefabs are the really getting loaded runtime and getting destroyed when i say Destroy??? as the Texture count is always showing 318.

2> Using prefabs is correct technique to load and destroy objects run time to save graphics memory.

3> And if the techniques is correct how to prevent crashes on the devices. Or is there any other better technique which i can use???

If you have to work with 2D-Animations check out the SpriteManager2. Maybe this would help you.
http://www.anbsoft.com/middleware/sm2/

These are actually very important questions. I think they can all be boiled down to one:

  • How can we make sure that only the textures we’re currently using are occupying memory, and when we’re done with them, free them from memory to avoid memory crashes?

Honestly, I don’t have an authoritative answer on that, but I will suggest that deleting the prefab which uses the material/texture is not going to free the texture itself. I’m not 100% sure how Unity handles this internally, and maybe it will free the texture once it is no longer being referenced, but I’m thinking there may be another step that has to be taken to explicitly unload the texture.

BTW, it sounds like SM2 might definitely be a big help to your workflow. At present, it is limited to one material per-sprite, but I plan very soon to allow multiple textures per-sprite.

I am doing something similar and have a similar problem, if there is a viable solution to this outside of the prefabs I would love to hear it.

“Bumping!”

Any resolutions to this?

If only we could do our own memory management in Unity like you do in Objective C (another reason ObjC is superior to Java etc. imho - none of this nonsense of waiting for a garbage collector to maybe sometime clear the memory for you).

Anyhow, I would recommend:

  1. you check out SM2 - it could save you vast amounts of memory by getting more frames on one png
  2. before you start you prototype/profile the devices you want to support to be sure of the limitations - it looks like you’re definitely pushing way beyond the memory limits of your target device

What does your memory usage go up to on 3GS?

When I use lots of memory with textures I clear them out between scenes/levels by going to en empty scene.

btw, I assume you are using PVRTC and no mip-maps?