Search Unity

Can't seem to save Image and Load it Back

Discussion in 'UI Toolkit' started by Bleuzion, Jan 16, 2020.

  1. Bleuzion

    Bleuzion

    Joined:
    Oct 31, 2017
    Posts:
    3
    So i've been struggling and this is my last resort. can someone plz help me find a way to save user imported image as bytes and then call it back as a byte and into a sprite. I've tried everything but the closest I've gotten is making question mark happened.

    if you wanna see my code just ask.
    Thanks
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Use these to save a texture to bytes[], based on the desired format (I recommend PNG as it's lossless):

    https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToPNG.html
    https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToJPG.html

    Use this to save it to a file:

    https://docs.unity3d.com/ScriptReference/Windows.File.WriteAllBytes.html

    Use this to load an image from a file:

    https://docs.unity3d.com/ScriptReference/Windows.File.ReadAllBytes.html

    Use this to decode the bytes back into a texture:

    https://docs.unity3d.com/ScriptReference/ImageConversion.LoadImage.html

    Use this to create your Sprite at runtime:

    https://docs.unity3d.com/ScriptReference/Sprite.Create.html


    The EncodeToPng link even has a sample of how to save an image and upload it to the web.

    If you're still not sure you can paste the code here with the Insert Code: [<>] button on the post editor options bar.
     
  3. Bleuzion

    Bleuzion

    Joined:
    Oct 31, 2017
    Posts:
    3
    This is the loading after saving
    upload_2020-1-16_15-45-42.png
    This part bellow is Saving it
    upload_2020-1-16_15-46-22.png
    I've tried other way of but the results were worst.
    hope this help you help me.

    also i already read most of the API provided by unity.
     

    Attached Files:

  4. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    ah, don't use the Load/Save RawTextureData. That's for very specific use cases and is specific to the GPU (it doesn't save as normal image data, it's litterally what the GPU format is and if you're loading user-submitted content / normal image files, it won't work at all). use the ImageConversion tools and normal image files instead.

    Also, I'd assign the created sprite to the instantiated element, not the comps prefab/prototype.
     
  5. Bleuzion

    Bleuzion

    Joined:
    Oct 31, 2017
    Posts:
    3
    I honestly don't know why its not working. its saying that it cant create a sprite from a given texture. I tried changing the sprite size but the results didn't get better.