0

I have a gallery scene and I want to load PNG's from Persistence path .

The thing is that I want them as thumbnails, theirs no need for me to load the full size file .

How can I defined the scale of the sprite? that the relevant line for the creating of the sprite:

  Sprite sp1 = Sprite.Create(texture1, new Rect(0, 0, texture1.width, texture1.height), new Vector2(0.5f, 0.5f), 100, 0, SpriteMeshType.FullRect);

and that the texture creating code:

 Texture2D takeScreenShotImage(string filePath)
{
    Texture2D texture = null;
    byte[] fileBytes;
    if (File.Exists(filePath))
    {
        fileBytes = File.ReadAllBytes(filePath);
        texture = new Texture2D(1, 1, TextureFormat.ETC2_RGB, false);

        texture.LoadImage(fileBytes);
    }
    return texture;
}

1 Answers1

0

The proper place to make that change is on the Texture2D after loading the Texture. If you really want to load them as thumbnails and want the size to be smaller to save memory then resize it with the Texture2D.Resize function after loading the Texture. The height and width of 60 should be fine. You can then create Sprite from it with Sprite.Create.

Texture2D takeScreenShotImage(string filePath)
{
    Texture2D texture = null;
    byte[] fileBytes;
    if (File.Exists(filePath))
    {
        fileBytes = File.ReadAllBytes(filePath);
        texture = new Texture2D(1, 1, TextureFormat.ETC2_RGB, false);

        texture.LoadImage(fileBytes);
    }

    //RE-SIZE THE Texture2D
    texture.Resize(60, 60);
    texture.Apply();

    return texture;
}

Note that TextureFormat.ETC2_RGB is compressed. To resize it you may have to create a copy of it. See #1 solution from my other post on how to do this.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I added "texture.Resize(texture.width/2, texture.height/2);" , and i'm getting an "empty" file , I can see in the inspector that the resize worked but the file all alpha. What can be the problem ? – Dror Davidovich Aug 07 '18 at 07:29
  • Not sure but try to provide the texture format and mipmap. Use `texture.Resize(60, 60, TextureFormat.ETC2_RGB, false);` – Programmer Aug 07 '18 at 12:25
  • I tried , but no luck. Any Resize that I'm doing resolve with an alpha image. – Dror Davidovich Aug 07 '18 at 12:37
  • Enable mipmap both where you're creating the texture and resizing it – Programmer Aug 07 '18 at 12:43
  • when I add texture format I'm getting "Can't resize to a compressed texture format". When enabling mipmap I get "failed to create 2D texture id=568 width=1 height=1 mips=3 dxgifmt=28 [D3D error was 80070057]" edit: after changing the texture format tp ARGB32 , the error's are gone but I still having an all alpha image. – Dror Davidovich Aug 07 '18 at 12:47
  • The texture you have seems to be compressed. I suggest you duplicate or make a copy of the texture then try to resize it that one. See #1 solution from my [other](https://stackoverflow.com/a/44734346/3785314) post on how to do this – Programmer Aug 07 '18 at 12:54
  • I out of the office, I will try it firs thing tomorrow morning. Making a duplicate of the textures won't take a lot of memory and preformance? – Dror Davidovich Aug 07 '18 at 19:17
  • `ETC2_RGB` = compressed format. By making a copy of it, you will remove the compression on the new texture. You can destroy the new texture after that with the `Destroy` function – Programmer Aug 08 '18 at 00:07
  • Ok , I tried to make a cope and it kind of works . The texture is resized and I can see it , the only problem is that the resizing don't take the whole texture , it's change only the file size . So after changing it to "source.Resize(source.width / 2, source.height / 2);" i can see only the 50% of the with and height of the original. – Dror Davidovich Aug 08 '18 at 07:55
  • I forgot to say that you have to call `texture.Apply();` after resizing it. Please do this and the issue should be solved. Edited my answer to reflect that. – Programmer Aug 08 '18 at 12:28