I am able to change the sprite collection data at runtime. But the change is reflected in once I close the game and start again. So all I need is to refer the latest updated sprite collection at ru time . So I did following but it is not working, it doesnot refer to the latest sprite collection.I have marked the collection as loadable
tk2dSpriteCollectionData collectionData = animatorObject.gameObject.GetComponent<tk2dSprite> ().Collection;
SetAtlasAndMaterialAsPerClothsSelected (collectionData); // here I have updated the material and atlas as per my need
animatorObject.GetComponent<tk2dBaseSprite>().
SetSprite(tk2dSystem.LoadResourceByName<tk2dSpriteCollectionData>("Boy_Mid_Torso1_SpriteCollection"),
"boy_mid_jump_torso"
);
EDIT : I have been able to do it using following code , I had to strip away the tk2dSprite and tk2dSpriteAnimator to add it at run time : -
tk2dSpriteCollectionData scData = Resources.Load("Temp/Boy_Mid_Torso1_SpriteCollection",typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData ;
SetAtlasAndMaterialAsPerClothsSelected (scData);
part.gameObject.AddComponent<tk2dSprite>();
animatorObject = part.gameObject.AddComponent<tk2dSpriteAnimator> ();
part.gameObject.GetComponent<tk2dSprite>().SetSprite(scData,"Home_idle_torso");
EDIT 2: I just found that above way of setting the collection doesnot actually set the sprite collection. The tk2dSprite is not referencing any sprite collection when I check in inspector, but some how animation is playing. What might be wrong ?
Although it plays the animation but before playing it it throws NullReferenceException at line following function Bold Line
public void SetSprite(tk2dSpriteCollectionData newCollection, int newSpriteId) {
bool switchedCollection = false;
if (Collection != newCollection) {
collection = newCollection;
[b] collectionInst = collection.inst; [/b] // throws NullReferenceException.
_spriteId = -1; // force an update, but only when the collection has changed
switchedCollection = true;
}
spriteId = newSpriteId;
if (switchedCollection) {
UpdateMaterial();
}
}
My concern is If its not able to get the reference then how animation is playing ?
Edit 3: I just found that the sprite collection I am messing up with that is Boy_Mid_Torso1_SpriteCollection, cannot be found by the system. While setting this sprite collection for in animation editor, It keeps on Scanning the project folder forever for Rebuilding the indexes and throws error "Sprite Collection cannot be found : this is serious problem" . What went wrong ? How to fix it ?