Hello Guest

Author Topic: Load/Change texture on Sprite.  (Read 17291 times)

spacejaguar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
Load/Change texture on Sprite.
« on: January 20, 2013, 06:00:35 pm »
I am new to programing and having trouble changing the texture on a 2k 2d sprite.  :-\

I am using the    "SwitchCollectionAndSprite (tk2dSpriteCollectionData newCollection, int newSpriteId)" as seen on the tk2BaseSprite class reference but keep getting all sort of errors.
I am using Javascript.

I wantto create a small javacript script and atach it to the object. Then the script reads a variable that informs wich Sprite image should be used.

Can someone please provide an example on how to do this? Would it be possible to call the sprite texture based on the sprite name instead of the sprite ID?

Thanks in advance.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #1 on: January 22, 2013, 11:45:46 am »
Make sure you have run this:
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,33.0.html
This will make sure you can access the tk2d classes from JS.

In order to get a reference to a sprite collection, you can do this:
Code: [Select]
var spriteCollectionData : tk2dSpriteCollectionData; // drag a sprite collection data object into this
Assuming you have a reference to the tk2dSprite class in a variable called sprite, you can do this:
Code: [Select]
sprite.SwitchCollectionAndSprite( spriteCollectionData, spriteCollectionData.GetSpriteIdByName("spritename") );

That is pretty much all you need.

fattie

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 106
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #2 on: January 22, 2013, 02:30:45 pm »
To be clear, if you have a collection called say "Test".

In your Project panel, there will be an object called "Test".  There will be a folder called "Test Data".  inside that folder will be a number of Atlas, and also, another object called "Test".  It is the object inside the folder named Test, which you drag to the variable slot in the Inspector.

I have always wondered if there is a way to get these by NOT using the Inspector-dragging.

Something like, spriteCollectionData = .. Find in the Project/Assets .. ("Test");

Thanks if anyone knows this.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #3 on: January 22, 2013, 10:32:16 pm »
There kinda is, using the new "Loadable asset" system. It is unsupported at the moment, but it does work - the platform specific stuff uses it.

If you tick it in the sprite collection editor, that makes the asset loadable using the tk2d system. Once ticked, you can iterate through allResourceEntries - currently there isn't any type info stored, but the only objets stored in here are tk2dSpriteCollectionData objects for now so you can make that assumption.

As mentioned before, it isnt supported - but it is likely to become a core feature in a future release.

fattie

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 106
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #4 on: January 31, 2013, 12:52:10 pm »
I don't know how to get at allResourceEntries ...

(isn't that just in the Editor?)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #5 on: January 31, 2013, 11:10:22 pm »
It is private in runtime, but you can write a function in that class to access it. But, if all you want is to load sprite collections dynamically, you don't need to do this. I'll reply to your email soon with more details about that, just held up with some stuff right now. It does work.

fattie

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 106
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #6 on: February 01, 2013, 09:12:44 am »
One particular problem I was facing - imagine large (near full screen) signs which appear from time to time (eg, "new level" for example). In terms of memory use it is critical to load these only when needed, and get rid of them afterwards.

Here is an exact formula to achieve this using the "single shot" CreateFromTexture method. This explains exactly how to fully "get rid of" such an image.

Starting with the name of your PNG, say imageFileName = "sign18" if the image is sign18.png. The normal Unity command Resources.Load is used to get the PNG from disk and begin using it.

Note that CreateFromTexture creates a gameObject containing the sprite. (handyTempGameObject). Note that the code below then gets the underlying tk2dSprite (handyTempTKS) using GetComponent.

Note carefully that CreateFromTexture also creates in your scene (at root level) the actual sprite COLLECTION needed for the "one-off" sprite. You will see it in the scene at root level and you can get to it via handyTempTKS.collection.gameObject. You must remember to later also Destroy that COLLECTION as well as the sprite game object.

In the example code, I simply move the COLLECTION under the game object (ie, using .parent). This means that later, the COLLECTION will all be trashed when you simply call Destroy on the sprite game object, i.e. you won't forget to separately Destroy it.

The first time you run the code in editor, you will get a warning "you must move _ _ _ shader to a resources folder." Bring up that shader, show in finder. Make a Resources/ folder there beside it (it may exist already). COPY the shader in to that Resources folder.

   var handyTempGameObject:GameObject;
   var handyTempTKS:tk2dSprite;
   var tempTexture:Texture2D;
   tempTexture = Resources.Load( imageFileName, Texture2D );

   // now create the temporary sprite
   // the code shows how to set the size, anchor, screen-size and scale
   // note that you can use SpriteCollectionSize.ForTk2dCamera()
   // rather than SpriteCollectionSize.Explicit() if relevant to you.
   // PS remember to pop the shader in to a Resources/ folder
   // you will get a clear alert telling you which shader to move, if you forget.

   var tSize:Rect = new Rect(0, 0, tempTexture.width, tempTexture.height);
   var tAnch:Vector2 = new Vector2(tempTexture.width/2, tempTexture.height/2);

   handyTempGameObject =
      tk2dSprite.CreateFromTexture( tempTexture,
         tk2dRuntime.SpriteCollectionSize.Explicit( 1, 1536 ),
         tSize, tAnch );
   handyTempTKS = handyTempGameObject.GetComponent(tk2dSprite);
   handyTempTKS.scale = Vector3(2,2,1);

   // critical: keep track of the dynamic COLLECTION, to release it later:
   handyTempTKS.collection.transform.parent = handyTempGameObject.transform;
   
   // have an empty object called something like "holder" in the correct
   // position you want to use the large dynamic sign.
   // typically you would have eg. unity animations (perhaps a bounce, etc)
   // and anything else (scripts etc) you need on that holder.
   // put the dynamic sign in that holder
   handyTempGameObject.transform.parent = signHolder;
   handyTempGameObject.transform.localPosition = Vector3(0,0,0);


   // now use your sprite here, as needs be.
   // do whatever you want with the holder, eg,
   // slide it across the screen, blink it, whatever.
   // use your sprite here, as needs be
   // use your sprite here, as needs be


   // finally to fully unload everything from memory is this simple
   // these two lines of code:
   DestroyImmediate( handyTempGameObject );
   Resources.UnloadAsset( tempTexture );
   // since we put the dynamic collection "under" the sprite game object,
   // it is released properly.  alternately, leave it at the root level but
   // it is essential to remember to call DestroyImmediate on it also


Thanks as always to TK "ultimate gold-plated user support" !
« Last Edit: February 04, 2013, 08:46:43 am by fattie »

spacejaguar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #7 on: February 19, 2013, 09:52:58 pm »
Hello Fattie.

I followed your instructions and created an empty object named "signHolder".

I atached the script with the code to it. And animated the empty object.

However i keep getting the error:
Assets/Scripts/PNGtoSprite.js(37,43): BCE0005: Unknown identifier: 'signHolder'.

Shouldn't that empty object (signHolder) be the parent object of the created sprite? I don't understand why it is not being identified...


spacejaguar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #8 on: February 19, 2013, 11:39:27 pm »
Ok i think i fixed it...

I added:

var signHolder: GameObject;

and changed

handyTempGameObject.transform.parent = signHolder;

to

handyTempGameObject.transform.parent = signHolder.transform;

and then linked the signHolder variable to the parent object in the editor.

so now when i run it i get another error  ;D

NullReferenceException: Object reference not set to an instance of an object
PNGtoSprite.Start () (at Assets/Scripts/PNGtoSprite.js:20)





fattie

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 106
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #9 on: February 20, 2013, 06:02:01 am »
Hi Newbie, to be clear those are code fragments.  They work perfectly and are copied out of different files from a working project, but t is not one slab of text you can paste in to one javascript file.

General javascript help is beyond the scope of this forum I guess, but whatever your variable "PNGtoSprite" is, basically you have not set it (use one of the "Find" commands or drag something to it in the editor). Hope it helps!

kreso22

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #10 on: August 01, 2014, 06:52:56 am »
I was wondering is it possible just to change collection if I know it's name (string)?
I need to change collection and chose another sprite from this new collection.
SetSprite wants CollectionData which I don't know how to read (except dragging & dropping in inspector) but I want to avoid inspector if possible. Is it possible?

Thank you,
Kreso

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #11 on: August 01, 2014, 09:02:58 am »
The only way to do this is to use Resources.Load
http://docs.unity3d.com/ScriptReference/Resources.Load.html

You will need the sprite collection data object (and nothing else) in a Resources folder for this to work.

kreso22

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Load/Change texture on Sprite.
« Reply #12 on: August 01, 2014, 08:36:50 pm »
I was hoping that it could be done via 'string' method argument, since in inspector I can chose sprite collection by name, from dropdown (again I assumed it's stored somewhere where I could potentially use it).
OK, I understand. Perhaps in  a future version. Thank you for your prompt reply!