2D Toolkit Forum

2D Toolkit => Support => Topic started by: krithik_b007 on November 06, 2013, 11:07:08 am

Title: Too Many Spritesheets !
Post by: krithik_b007 on November 06, 2013, 11:07:08 am
Hello,

We are facing some problems due to use of more textures . We have some 25 different textures (there are some 10 different stages in the game each has its own background) , and again each texture is created 3 times (iPad Retina , iPad , iPhone) . So that makes around 75 textures !!!
I saw that 2048 texture takes 16 MB & 4096 * 4096 takes 64 MB ! . So when compiling the project unity creates a file called resources.assets and its size becomes more than 1 GB !.
and compiling the project for iOS takes looong time more than 10 mins ! .

I am from cocos2d background , there we use lot of textures & everything works fine .
since we are working on 2d game on unity , there are lot of 2d images , so how we need to proceed  ? . Is there any workaround ?

&

One more question is whether we need to have original images which we used to create sprite sheets  in the resources folder of unity ? or can we delete it ?

I have also attached the Sprite Collection Settings & texture settings screenshot for the reference.

Thank you
Title: Re: Too Many Spritesheets !
Post by: unikronsoftware on November 06, 2013, 12:10:49 pm
The original images makes no difference to the build, or resources size. They aren't included in the build.

You can try compressing the images using 16bit / compressed - that will reduce the file sizes. Unity does a terrible job at compressing textures when on disk - well for starters they aren't compressed in iOS build. There is a hack we can do to reduce the file sizes at the cost of load times, which will probably be in tk2d 2.3 or 2.4 - but again this is just a hack around a fundamental issue in Unity.

You can also put stuff into asset bundles, which will reduce sizes as the asset bundles can be compressed as far as I remember.
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 on November 06, 2013, 01:15:43 pm
Thanks for the quick reply .
So when you have plans of releasing 2.3 which will have this feature .

I Guess one more option is to use 2d Planes to create background images , which will save lot of space in texture atlas ?

Thanks .
Title: Re: Too Many Spritesheets !
Post by: unikronsoftware on November 06, 2013, 01:32:46 pm
Using planes will save atlas space, but the texture will still occupy a lot of space. It is the texture that is occupying the space.
Edit: Unsure if this feature will be in 2.3 or 2.4 - and I don't have any dates for either yet.
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 on November 06, 2013, 01:56:25 pm
So if we just have a image of 2048*1536 and we will use this image in Unity 3d's  2d Plane ,still unity will take 32 MB memory space in resources.assets ?
Title: Re: Too Many Spritesheets !
Post by: unikronsoftware on November 06, 2013, 02:19:40 pm
2048*2048 will take 16MB, 2048*1536 will take about 14MB. Not a huge difference. You can get better results by using .png.bytes files, and then Resources.Loading them. You can save a ton of memory that way, but its a pain as you will have to do it manually.

If you have huge backgrounds you don't need to put them into a spriite collection, just use sprite from texture or something similar.
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 on November 06, 2013, 04:22:05 pm
Can you please explain about .png.bytes files, and then Resources.Loading them.
How can we do that ?

Krithik


Title: Re: Too Many Spritesheets !
Post by: unikronsoftware on November 06, 2013, 04:31:08 pm
http://answers.unity3d.com/questions/490255/how-do-i-store-textures-as-png-in-the-final-build.html
Shaderop's solution is what you need to do. Once you get the Texture2D you can feed that directly into the SpriteFromTexture component.
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 on November 06, 2013, 05:12:25 pm
Thanks unikronsoftware
I will look into it  :)
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 on November 07, 2013, 03:00:33 pm
Hello

I tries various methods today , and found that .png.bytes file is the best one  :) , If we do this we can save  20 MB for 2048*2048 in RGBA 32 Bit ! Thats huge memory size if we consider its just one iPad Retina Background .

Do you think by doing this , will it effect the loading time of the game or the scene ?

Also which is the best way to create tk2dsprite from the Texture2D ?

Currently i am using code like

Code: [Select]
tk2dBaseSprite spriteInstance = null;

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

string[] names = new string[] {
"Extracted region",
"Another region",
"Full sprite",
};
Rect[] regions = new Rect[] {
new Rect (79, 243, 215, 200),
new Rect (256, 0, 64, 64),
new Rect (0, 0, runtimeTexture.width, runtimeTexture.height)
};
Vector2[] anchors = new Vector2[] {
new Vector2 (regions [0].width / 2, regions [0].height / 2),
new Vector2 (0, regions [1].height),
new Vector2 (0, regions [1].height)

};



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

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

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


Is there any easy way to load the Texture2D directly to tk2dsprite ?

Thanks
Title: Re: Too Many Spritesheets !
Post by: unikronsoftware on November 07, 2013, 03:24:18 pm
Due to a high number of support requests from this IP address, I'm going to have to ask you (and the others on your static IP address) to register your copies of 2D Toolkit.

Instructions here:
http://2dtoolkit.com/forum/index.php?index.php/topic,34.0.html
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 on November 07, 2013, 03:51:53 pm
Okay i will do that tomorrow .
Is there any restrictions in support request or anything like that ?

Thanks .
Title: Re: Too Many Spritesheets !
Post by: unikronsoftware on November 07, 2013, 03:56:36 pm
No the only rule is just be sensible. We get a significant volume of support requests and registered users get priority.
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 on November 07, 2013, 04:21:51 pm
Ok Sure . I have filled the form.

Thanks .
Title: Re: Too Many Spritesheets !
Post by: unikronsoftware on November 07, 2013, 06:36:48 pm
Thanks.

Quote
Also which is the best way to create tk2dsprite from the Texture2D ?
Use the texture from sprite component as I mentioned before, and call Create on it when updating the texture.
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 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
Title: Re: Too Many Spritesheets !
Post by: unikronsoftware 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.
Title: Re: Too Many Spritesheets !
Post by: krithik_b007 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.