Sprite.Create is slow

Hello,

From many pictures on the disk, I’m creating a sprite sheet at runtime with Unity. The process works fine, I finished saving the big texture on the disk and a json file.

Then I load the png, the json and I create my sprites:

Sprite.Create(loaderTexture.texture,newRect(textureAsset.x,textureAsset.y,textureAsset.width,textureAsset.height),Vector2.zero,pixelsPerUnit,0,SpriteMeshType.FullRect));

I tried:

  • from a 4096 x 4096 texture (16Mo) creating 50 sprites, 30 seconds.
  • from the same texture compressed with pngquant (3Mo) creating 50 sprites, 10 seconds.

The WWW part for loading the texture doesn’t change anything, it is really related to the Sprite.Create API. What Unity does internally? How can I optimize on my side?

1 Like

File a bug and we will look at it. Good feedback!

Thanks for the quick answer, done here: https://fogbugz.unity3d.com/default.asp?873789_ee19f1gcv0njktqu

In this case, the code was using WWW.texture every time it creates a Sprite. Each call to WWW.texture will create a new Texture2D from the texture data it contains.

To improve this, you can do the following

var t = loaderTexture.texture;
foreach (TextureAsset textureAsset in textureAssets.assets)
    mSprites.Add(textureAsset.name, Sprite.Create(t, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), Vector2.zero, pixelsPerUnit, 0, SpriteMeshType.FullRect));

Wow guys awesome, fixed!!

Thanks again :slight_smile: