Hello Guest

Author Topic: Too Many Spritesheets !  (Read 10114 times)

krithik_b007

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Too Many Spritesheets !
« Reply #15 on: November 08, 2013, 02:45:08 pm »
Hello

I am trying to create sprite from Texture like  , but the size of the sprite is improper . (I guess something to do with pixel meter ?)
Also i saw many methods to create sprite , but all the methods need tk2dSpriteCollectionData  .

Can you tell me what is wrong in the below code or let me know if there is a easier method to create a simple 2d sprite from Texture2D , also i didn't get the point Use the texture from sprite component as I mentioned before, and call Create on it when updating the texture.

               
Code: [Select]
tk2dBaseSprite spriteInstance = null;

Texture2D runtimeTexture = new Texture2D (4,4);
runtimeTexture.LoadImage (imageTextAsset.bytes);

Debug.Log ("runtimeTexture.width" + runtimeTexture.width + runtimeTexture.height);

string[] names = new string[] {
"Full sprite",
};
Rect[] regions = new Rect[] {
new Rect (0, 0, runtimeTexture.width, runtimeTexture.height)
};
Vector2[] anchors = new Vector2[] {
new Vector2 (.5f, .5f),
};


tk2dSpriteCollectionSize spriteCollectionSize = tk2dSpriteCollectionSize.Explicit (runtimeTexture.width, runtimeTexture.height);

tk2dSpriteCollectionData spriteCollectionInstance = tk2dSpriteCollectionData.CreateFromTexture (runtimeTexture, spriteCollectionSize, names, regions, anchors);

spriteCollectionInstance.halfTargetHeight = 0.5f;
spriteCollectionInstance.invOrthoSize = 2.0f;

GameObject go = new GameObject ("sprite");
go.transform.localPosition = new Vector3 (0,0,0);
spriteInstance = go.AddComponent<tk2dSprite> ();
spriteInstance.SetSprite (spriteCollectionInstance, 0);


In-between i also saw a feature called dicing , it works great in saving Atlas space . Whether using dicing in the atlas increased loading time or any performance issue ?

Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Too Many Spritesheets !
« Reply #16 on: November 09, 2013, 09:54:58 am »
There is a component called Texture From Sprite - check the documentation on how to use it. You can use that to create a sprite in the scene.
If you look in the code, there will be a function called Create which will let you change the image assigned from code.
This component manages the texture itself.

Dicing won't increase load times, and if you're only dicing a background there will be no performance drop.

krithik_b007

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Too Many Spritesheets !
« Reply #17 on: November 09, 2013, 03:45:48 pm »
Ah Finally after lots of google search & scratching head got the solution .

Create the texture like this
*note you need so give the .png.bytes file through the Unity interface

Code: [Select]
Texture2D runtimeTexture = new Texture2D(1024,768,TextureFormat.RGBA32,false);
runtimeTexture.LoadImage (imageTextAsset.bytes);

If we directly create Texture using new Texture2D (32, 32); it will be blurry.

Then from the texture create sprite like

   
Code: [Select]
tk2dSpriteCollectionSize spriteCollectionSize = tk2dSpriteCollectionSize.Explicit (runtimeTexture.width, runtimeTexture.height);
spriteCollectionSize.pixelsPerMeter = 1;
spriteCollectionSize.orthoSize = 512;

GameObject myTestSprite = tk2dSprite.CreateFromTexture (runtimeTexture, spriteCollectionSize, new Rect (0, 0, runtimeTexture.width, runtimeTexture.height), new Vector2 (runtimeTexture.width / 2, runtimeTexture.height / 2));
myTestSprite.transform.localPosition = new Vector3 (0, 0, 0);


Hope this will be helpful for someone  :)

@unikronsoftware
Thanks :) & Let me know if there is any improvement in the above code.