Search Unity

Question How to save Sprite create by script to Png?

Discussion in 'General Graphics' started by HZG1995, May 11, 2023.

  1. HZG1995

    HZG1995

    Joined:
    Mar 15, 2022
    Posts:
    5
    I have one png it has some sprite . Then I create other sprite by script when Unity is running at line 1018,and I want to export the new temp sprite to png at line 1021,but it will report an error upload_2023-5-11_14-12-22.png upload_2023-5-11_14-10-20.png upload_2023-5-11_14-8-7.png
     

    Attached Files:

  2. HZG1995

    HZG1995

    Joined:
    Mar 15, 2022
    Posts:
    5
    I create sprite by name array x,y array in the png upload_2023-5-11_14-14-48.png
    Too many arrays cause unity to not work properly,will cause Unity to not respond
    So I want to save them locally
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    It says it can‘t handle compressed textures, thus you need to change it to uncompressed.

    Btw the use of a non-monospace font for code hurts my eyes, and line numbers in the 4-digits and magic numbers (eg 2045, 72) hurt my brain, and both together hurt so much I have to mention it. ;)

    Consider the case where you need to change the 2045 to 2044. What are you going to do? It‘s just self defeating if you don‘t declare a const field like ‚MaxArraySize‘ and use that.
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    I think use of non-monospace for programming was recommended by either Knuth or Stroustrup. Probably Stroustrup. It is definitely valid and also teaches programmers not to use spaces for alignment very quickly.

    The concern for magic number is perfectly valid though.
     
  5. HZG1995

    HZG1995

    Joined:
    Mar 15, 2022
    Posts:
    5
    upload_2023-5-11_14-51-56.png
    it's not compressed
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    Couldn't find that as a quote but dug around a little because I'm aghast at the thought of using variable sized fonts for programming. Found this discussion and the strongest "pro" argument is ... wow ... based solely on making fewer typos.

    Ouch. That hurts even more. For the simple reason that if you make a typo, it's either a syntax error and you cannot go on without fixing it, or it just doesn't matter because the system still works with typos like "PersistantStorage". I see those incorrect uses of the english language even more often than typos. And if you do spot them, there's always Refactor=>Rename. ;)
     
  7. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    Try logging the sprite.texture.format and related properties. Perhaps it's actually a texture atlas which is compressed? The "Default" setting at the bottom of the Inspector seems to indicate compression but I'm not sure if that's being used since it doesn't have the "Override" checkbox set. But then the Inspector shows the properties of the font, not the texture properties.

    Try clicking the Override checkbox and set to an uncompressed format.
     
  8. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    I've been programming this way for a couple of years. It is very comfortable when you use camelCase, and when you use tabs for idents. It also goes along with literary programming ideas.

    Basically, there's zero reason to stick with monospace in the first place, because we aren't using terminals anymore. Proportional fonts make certain things like braces stick out and take less space at the same time. Identifiers are very compact, and It also feels like you read the code much faster. After a couple of months monospace font looks like somebody is trying to awkwardly dictate you a book, one syllable at a time.

    You can check this discussion and check out screenshots of the top answer:
    https://softwareengineering.stackexchange.com/questions/5473/does-anyone-prefer-proportional-fonts

    Eventually, however, you might stop caring and use whatever.

    Also while I can't find a quote, quite a lot of people used proportional fonts after reading Stroustrup's book. It uses proportional fonts for all examples.
     
    Last edited: May 11, 2023
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    To answer OPs question.

    EncodeToPng only works with uncompressed image. So to save image with it, you'll need to either extract it using GetPixels ( https://docs.unity3d.com/ScriptReference/Texture2D.GetPixels.html ), or blit it onto another ARGB texture first.

    If my memory serves me right, you should not NEED to create a png image in order to create a sprite asset. Basically, I'd expect to be able to create it directly using AssetDatabase, then alter its property. The resulting asset, however, will be impossible to modify using external program, because it'll be saved as *.asset or something like that.

    Also, if your asset is imported with compression, by saving it again as png, you'll be slowly losing data due to lossy compression.