Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing UI Image's sprite, preserve aspect ratio is checked but not working?

Discussion in 'Editor & General Support' started by NotASnack, Apr 23, 2019.

  1. NotASnack

    NotASnack

    Joined:
    Nov 13, 2018
    Posts:
    4
    Changing UI Image's sprite, preserve aspect ratio is checked but not working. Version: Unity 2019.1.0f2

    What happened

    I updated to the latest software update for Unity which is Unity 2019.1.0f2.

    Before when it was version 2018.3.12f, the UI Image's sprite changes normally. But now when I have updated it, it doesn't regain its aspect ratio and gets stretched.

    When the sprite changes on the UI image, the aspect ratio/width and height changes for the image and when you go back to the original image, then it becomes stretched or compressed.

    I thought it was because I updated my game Unity version but I reproduced it on a brand new Unity project and it is doing the exact same thing.

    How I reproduced it:

    Code in Sprite[] and Image.

    Code to change the sprite image to the next image in the sprite array.

    Have a button to cycle through the images.

    Run game.

    Press button multiple times.

    When it is cycled back to the first image again, it is now stretched or compressed and aspect ratio doesn't go back to its original size.

    This bug has only been happening when I updated to the latest version Unity 2019.1.0f2 from 2018.3.12f.

    Help would be appreciated. Thanks!
     
    Yakirbu and floboc like this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,230
    Have you filed a bug report?
     
  3. NotASnack

    NotASnack

    Joined:
    Nov 13, 2018
    Posts:
    4
    Yes I have, I was posting to see if anyone had a similar issue and if they had found a solution. Thanks.
     
  4. reev4eg

    reev4eg

    Joined:
    May 10, 2015
    Posts:
    37
    When wait a fix? UI is broken.
     
  5. NotASnack

    NotASnack

    Joined:
    Nov 13, 2018
    Posts:
    4
    Are you experiencing similar things?
     
  6. Twil75

    Twil75

    Joined:
    Aug 30, 2012
    Posts:
    22
    Same problem for me on 2019.1.0f2
    UI broken :(
    Waiting a fix
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,230
    Have you filled a bug report? What is the number?
     
  8. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    I got similar issues on *some* UI sprites when changing them by code. Changing them manually via inspector fixed them temporarly, but each time I changed them via code, they broke again. (stretch weirdly or else).

    Although, it didn't happened on every image. In my case the stretch issue happened only on the first image of an array.
    At other time, there was an other UI image with button, and the image was cut in half once clicked, like the rendering of the quad didn't completed.

    I sadly can't reproduce that on a blank projet (I tried without success), and can't submit my 12 gb's project either for bug report.

    (No special shader used. I'm working with latest LWRP on 2019.0.f1. With different types of canvas (worldspace, overlay...))
    - I didn't had that issue before on 2018.3

    I guess this issue is similar to our problem ?
    https://issuetracker.unity3d.com/issues/modifying-image-dot-sprite-of-an-existing-image-deforms-it
     
    Last edited: Apr 24, 2019
    Schneider21, reev4eg and kyferrell like this.
  9. floboc

    floboc

    Joined:
    Oct 31, 2017
    Posts:
    91
    Same issue here when updating from Unity 2018.2.12f1 (64-bit) to Unity 2019.1.0f2 (64-bit).
    I did nothing else but updating unity to the latest release.

    Moreover, the "Shadow" effect is also broken with sprite randomly being croped and render weirdly (in gray) as you can see in the following image:

    bug_001.jpg

    It was also working before updating
     
    Alverik, SnowDaySoftware and Immu like this.
  10. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    Last edited: Apr 25, 2019
  11. FamishedMammal

    FamishedMammal

    Joined:
    Nov 3, 2015
    Posts:
    113
    I am having a similar 'random streching' issue with my UI images after updating, but I seem to have found a temporary fix.
    Select your sprite in the inspector, and change Mesh Type from the default "Tight" to "Full Rect"
     
    Last edited: Apr 25, 2019
  12. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    589
    They have a bug on this : https://forum.unity.com/threads/sizedelta-broken-on-unity-2019-1-0f2.663268/
    All these problem are related to this issue.
    The funny thing , is that this bug has been reported the 3 april and they have released this version the 15 april without fixing it .... bravo !

    As always, they run too fast and release a broken version even with a bad poll result ....

    You have also this : https://forum.unity.com/threads/playerprefs-not-working-on-webgl-build.666022/#post-4457716

    And the best one : https://forum.unity.com/threads/edi...field-is-broken-with-unity-2019-1-0f2.665479/

    All these bug from Unity 2019.1.0f2

    So cool .....

    @karl_jones
     
    Last edited: Apr 25, 2019
    Immu likes this.
  13. reev4eg

    reev4eg

    Joined:
    May 10, 2015
    Posts:
    37
    karl_jones likes this.
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,230
    reev4eg likes this.
  15. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    Anyway, to sum up 2019.1f2 isn't production ready for UI. It kills existing projects :/ (including my complex UI animations, some of them are awefully broken now, like scales and positions became a mess, directly in editor, without even play mode, never happened before and it's been years ).

    So.. Can we have an ETA for a fix ? :)

    Also, please don't forget my question ^^ :

    "https://issuetracker.unity3d.com/issues/modifying-image-dot-sprite-of-an-existing-image-deforms-it : from which issue this one is now considered to be a duplicate of ? There's no way to know :/ "

    Is it a duplicate from that ? Which is himself a duplicate of that ?
    Thank you and good luck for the debug o_O
     
    Last edited: Apr 26, 2019
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,230
    Alverik, Immu and reev4eg like this.
  17. reev4eg

    reev4eg

    Joined:
    May 10, 2015
    Posts:
    37
    Immu and karl_jones like this.
  18. NotASnack

    NotASnack

    Joined:
    Nov 13, 2018
    Posts:
    4
    Updated to 2019.1.1f1 and still experiencing the same thing :(. Fingers cross for the next one, I can't continue to do any game dev anymore with Unity since a lot of my stuff relies on this as well.
     
    reev4eg likes this.
  19. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    589
    It's the same for me.
    Also this bug https://issuetracker.unity3d.com/issues/playerprefs-dont-get-saved-in-a-webgl-build stop me to move foward ....

    I will check if this bug has been fixed in 2019.1.1f1
     
    Last edited: May 7, 2019
  20. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    2019.1.1f1 had none of the UI fixes backported / given ETA yet. I hope they'll still land within this month though. It's really game breaking :/
     
    DezBoyle, reev4eg and Jar_Coding like this.
  21. reev4eg

    reev4eg

    Joined:
    May 10, 2015
    Posts:
    37
  22. nosajtevol

    nosajtevol

    Joined:
    Jun 28, 2012
    Posts:
    219
    I'm having the same issue. Was working well before I updated to latest Unity build.
     
  23. nosajtevol

    nosajtevol

    Joined:
    Jun 28, 2012
    Posts:
    219
    Actually, I found a simple solution to this. Simply set the sprite to null first, and then change the sprite. Seems to preserve the aspect of the sprite!
     
    KWaldt, Haagndaaz, Grhyll and 3 others like this.
  24. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,132
    The fix for this bug has not reached 2019.1 and 2019.2 yet.
     
  25. reev4eg

    reev4eg

    Joined:
    May 10, 2015
    Posts:
    37
    Thank you! God job!

    bug workaround:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public static class ExtensionMethods
    5. {
    6.     public static void SetSprite(this Image image, Sprite sprite)
    7.     {
    8.         if(image.sprite != sprite)
    9.         {
    10.             image.sprite = null;
    11.             if(sprite != null)
    12.             {
    13.                 image.sprite = sprite;
    14.             }
    15.         }
    16.     }
    17. }
    From:
    x.sprite = y;
    To:
    x.SetSprite(y);
     
  26. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    589
    Sorry, but can you tell us if for you this situation is normal according that this bug has been mainly reported the 3 April, so around 15 days before the official release.

    It's like the PlayerFrefs not working on WebGL, this bug has been reported the 26 March :https://issuetracker.unity3d.com/issues/playerprefs-dont-get-saved-in-a-webgl-build and the status is "Fixed in Unity 2019.2" so for July right?

    So now, imagine someone new coming for making a small 2D game with WebGL and sprite with the last Unity version.

    He can't !!! Because the playerprefs is broken with WebGL, the UI (sprite) is broken when you access to it by script and you can't edit your script if you have too much input value in it. (https://forum.unity.com/threads/edi...field-is-broken-with-unity-2019-1-0f2.665479/)

    Anyway, you have all informations here : https://forum.unity.com/threads/share-your-unity-2019-1-0f2-experience.670816/

    Thank you.
     
    Immu likes this.
  27. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    Wait..New update. Fixed in 2019.3.. Ok cool. But...
    https://issuetracker.unity3d.com/is...d-via-script-when-image-type-is-set-to-simple

    Can we still hope for a fix within the month for 2019.1 ? I don't expect to have to wait months to see a core functionality being fixed in a 'production ready' version. Especially when I updated to this version to fix previous versions which also had core functionality bugs like that.

    Thank you
     
    Last edited: May 15, 2019
  28. spaghettiSyntax

    spaghettiSyntax

    Joined:
    Aug 20, 2017
    Posts:
    1
    Worked like a charm, thanks! I just went:

    _statsImage.sprite = null;
    _statsImage.sprite = _characterStats[selected]._characterImage;

    Every time it's pulled and it fixed it. =D
     
    reev4eg likes this.
  29. JMYtech

    JMYtech

    Joined:
    Dec 13, 2018
    Posts:
    1
    @reev4eg Thanks a lot! Setting 'null' resolved my problem.
     
    reev4eg likes this.
  30. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    2019.1.4
    Still no backport of the fix for the bug reported on April 3rd.
    Very disappointed. But well. I keep hope for 2019.1.5 by the end of the week now :/
     
    Last edited: May 27, 2019
  31. Immu

    Immu

    Joined:
    Jun 18, 2013
    Posts:
    240
    From the issue tracker:

    This fix will probably also be in 2019.1.5f1 and 2019.2.0b4

    Glad to read that!
    Can't wait, thanks :)
     
  32. waitix

    waitix

    Joined:
    Sep 29, 2016
    Posts:
    8
    2019.1.5f1 fixed issue for me.
     
    Eater_Games, elhispano and Immu like this.
  33. Labecki

    Labecki

    Joined:
    Apr 14, 2015
    Posts:
    17
    I have been experiencing this problem as well. I guess I will checkout the latest update (although that usually entails an entire afternoon fixing new bugs :/ ).
     
  34. Abdul_Malik_Badshah

    Abdul_Malik_Badshah

    Joined:
    Oct 28, 2016
    Posts:
    13
    Select your sprite in the inspector, and change Mesh Type from the default "Tight" to "Full Rect"[/QUOTE]


    worked for me
     
  35. Supergeek

    Supergeek

    Joined:
    Aug 13, 2010
    Posts:
    103
    I was experiencing this bug, (Mesh Type gets converted from Full Rect -> Tight) because I was actually creating a new Sprite via script (Sprite.Create) from the carcass of a sprite created in the Editor. Tight must be the default behavior. I was able to work around the issue by specifying the Mesh Type in Sprite.Create.
     
    Last edited: Oct 10, 2019
  36. DezBoyle

    DezBoyle

    Joined:
    Feb 10, 2013
    Posts:
    5
    Setting the sprite to null and then changing it to the desired sprite seems to work fine! :)
     
    GrubO likes this.
  37. GrubO

    GrubO

    Joined:
    Oct 31, 2013
    Posts:
    1
    Hey, just wanted to let you know that this helped me!
     
  38. zenasprime

    zenasprime

    Joined:
    Mar 31, 2010
    Posts:
    165
    The above solution does not work for me. Also, my issue is a little bit different. Assigning a new sprite to the image simply does not take place.

    here's what the code looks like (it's pretty simple):

    Code (CSharp):
    1. if (MusicOn)
    2. {
    3.   musicImage.sprite = musicOnSprite;
    4. }
    5. else
    6. {
    7.   musicImage.sprite = musicOffSprite;
    8. }
    I've noticed that the scripting API for 2019.3 doesn't even include a reference to Image.sprite, which seems odd to me.

    https://docs.unity3d.com/2019.3/Documentation/ScriptReference/UIElements.Image.html

    I have a feeling that this is the incorrect API reference but for the life of me cannot find anything else in the reference for Image.
     
    Last edited: Apr 13, 2020
  39. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
  40. zenasprime

    zenasprime

    Joined:
    Mar 31, 2010
    Posts:
    165