Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug AspectRatioFitter new restriction in 2020.2

Discussion in 'UGUI & TextMesh Pro' started by eizenhorn, Dec 15, 2020.

  1. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Hi! What the point of this restriction now? I can't see any reason for that and we used that more than year without issues. We used ARF in 2020.1 without problem on ScreenSpace - Overlay parent canvas (and have nested canvases for reducing canvas rebuild cost), but now it completely breaks our UI because fitter disabled and our UI now has wrong sizes

    @Stephan_B I know you're most responsible for TMP, but I can see only you answer the questions in this forum. (in addition @phil-Unity)

    upload_2020-12-15_19-43-44.png

    upload_2020-12-15_19-43-51.png

    As temp fix (because we need deliver build for investors today) we copied
    com.unity.ugui
    into the project and commented out that restriction.
     
    Last edited: Dec 15, 2020
    cxode, Cheesus666 and IggyZuk like this.
  2. IggyZuk

    IggyZuk

    Joined:
    Jan 17, 2015
    Posts:
    43
    Just some quick findings:

    The issue appears to be present only when you instantiate a prefab dynamically. The aspect ratio fitter will update only when you resize the screen so that it affects the ARF in some way.

    You could hack it by calling its private methods with SendMessage.

    Code (CSharp):
    1. var go = Instantiate(prefab, target, false);
    2. var fitter = go.GetComponent<AspectRatioFitter>();
    3. fitter.SendMessage("SetDirty");
    You could also create a new class:

    Code (CSharp):
    1. public class SuperAspectRatioFitter : AspectRatioFitter
    2. {
    3.     protected override void Start()
    4.     {
    5.         base.Start();
    6.         SetDirty();
    7.     }
    8. }
    Apart from that looks like a bug.
     
    zezba9000 likes this.
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    It's not relative to resize etc. (which in your case can be another thing, possible bug, which on resize spawned UI from prefab get wrong renderMode property value).
    About what I posted is - if your root canvas render mode not WorldSpace internal check in editor will get false from IsComponentValidOnObject and will show a warning in the editor and will prevent ASF from doing anything at runtime.
    upload_2020-12-15_23-49-43.png
     
  4. bran_r

    bran_r

    Joined:
    Oct 15, 2019
    Posts:
    2
    I thought this was a bug! I really dont like this restriction, as it forces me to not upgrade to 2020.2 or to change the logic of my ui... is there a simpler workaround?
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Workaround already mentioned in my OP.
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
  7. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
    I do not believe it.

    Several of my backgrounds no longer appeared in the final build, I finally found the cause. I hope a fix will come out soon!
     
  8. NiLiu

    NiLiu

    Joined:
    Sep 7, 2019
    Posts:
    1
    I find another bug...
    In 2020.2,they add a new variable:
    Code (CSharp):
    1. //Does the gameobject has a parent for reference to enable FitToParent/EnvelopeParent modes.
    2.         private bool m_DoesParentExist = false;
    This variable is false in default,and will update in
    OnEnable()
    after
    SetDirty()
    .SetDirty() will adjust recttransform.But if your AspectMode is FitInParent or EnvelopeParent,it will check if m_DoesParentExist is true.So finally,if you use AspectMode.FitInParent or AspectMode.EnvelopeParent,and build a release package,you will find image disappear or in a wrong position...(Maybe because of [ExecuteAlways] attribute,it performs perfect in editor play)
    My english is not good,hope you can understand:)
     
    IggyZuk likes this.
  9. asdbgd

    asdbgd

    Joined:
    Jul 12, 2018
    Posts:
    1
    I managed to get it to work by copying AspectRatioFitterEditor.cs and AspectRatioFitter.cs from 2020.1 to 2020.2
    found in
    C:\Program Files\Unity\Hub\Editor\2020.1.12f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\UI\Core\Layout,
    and
    C:\Program Files\Unity\Hub\Editor\2020.1.12f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Editor\UI
     
  10. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Any comments from Unity? I see you guys answer other threads but ignoring this one :(@Stephan_B @phil-Unity sorry but ping you again.
     
    Last edited: Dec 21, 2020
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
  12. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    I just switched to 2020.2 and found this christmas gift from the unity team. I'm going to try to copy the files as suggested, but this is getting stupid. Unfortunately this seems like the intended behaviour, so I doubt they pay any attention to a bug report (I'd still vote for it, though).
    Obviously they don't want us to do it this way, but they should come up with a solution and put it in the warnning instead of just ruining everyone's work. I don't want to be manually changing this every time I update the engine, so instead they should fix whatever problem may happen behind the scenes, to just allow it.
    I deeply regret how everytime, stupid things like visual scripting that are just to attract noobs get lots of resources while this key elements that are fundamental but where never finished like the GUI get so little atention.
     
    Last edited: Dec 22, 2020
  13. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    @Joachim_Ante deeply sorry for ping you, but we are completely ignored by the Unity UI staff and I don't know whom to ask about that. Release notes contain no information about the reason for that and have only 2 fixes in irrelevant bugs.
     
  14. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
  15. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you submit a bug report with project and steps to reproduce this issue (if you have not already done so)? Although I focus on text related issues, I'll take a look and discuss with Phil once he is back from the Holidays.
     
  16. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Thanks! Yeah, I know you TMP responsible, I mentioned that in start post :) @Stephan_B
    Submitted:
    Case 1302464
    In addition example of how it works in 2020.1 as expected and if you'll scale that to widescreen monitors red square will be in the correct position (with all other UI parts in our project) and wouldn't stretch. Also, it will scale properly for all screens (Imagine this red square like RTS game minimap).
    upload_2020-12-29_17-39-13.png
    upload_2020-12-29_17-43-16.png

    And now in 2020.2 with this new Aspect Ration Fitter restriction mentioned (and even pointed in code) in start post (and you can see the warning in ARF ):
    upload_2020-12-29_17-32-57.png
    upload_2020-12-29_17-50-57.png
     
  17. alakoring

    alakoring

    Joined:
    Mar 11, 2018
    Posts:
    27
    Pretty sure I ran into this one too, my workaround was to call
    Code (CSharp):
    1. var fitter = this.background.GetComponent<AspectRatioFitter>();
    2. fitter.enabled = false;
    3. fitter.enabled = true;
     
  18. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    As I already pointed a couple of times - it wouldn't work with canvases other than WorldSpace. Read carefully :)
    In 2020.2 they added new rule -
    IsComponentValidOnObject
    method. Your enable true\false will trigger
    OnEnable
    which will call
    SetDirty
    method which in turn will run this
    upload_2020-12-30_14-37-35.png
    And in
    IsComponentValidOnObject
    in 2020.2 you can see this condition which will return you
    false

    upload_2020-12-30_14-38-2.png
    and condition above will be valid and it will just
    return
    you from
    UpdateRect
    without any work.
    In 2020.1 you wouldn't see this condition and nested canvases with parent ScreenSpace - Overlay canvas will work as expected with correct resizing.
    upload_2020-12-30_14-37-16.png
     

    Attached Files:

  19. alakoring

    alakoring

    Joined:
    Mar 11, 2018
    Posts:
    27
    Interesting… my canvas is screen space and my workaround seems to work for my 2D-only game. Also, there are no editor warnings. I haven’t dug into the Unity source but this may well be two bugs, like @NiLiu suggested. In any case, thanks for filing a bug.
     
  20. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    You didn't see it because you not on 2020.2 I suppose?
     
  21. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    Can you post a link to the bug report so we can vote it?
     
  22. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    It's not processed yet (thus haven't public ticket on issuetrackers)
     
  23. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    @eizenhorn Not ignoring this at all, but was off for 2 weeks and busy leading up to it so didnt see this. I'll take a look at the bug when i get a chance.
     
  24. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    No offence, we, of course, don't know devs vacation schedule, it was not in blame tone (sorry if it feels like this) but more like a scream of soul, just in case there are other devs whom I can bother.

    Tbh you don't even need bug report for this, just create a scene with canvas in Screen Space and add nested canvas with Aspect Ratio Fitter and you'll see a warning in the editor, but anyway, all related code already mentioned by me in this thread. It's just new method
    IsComponentValidOnObject
    in UpdateRect which makes ARF useless for any canvas except WorldSpace
     
  25. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Yup no offence taken just figured id reply and let you know i've seen it.

    The bug helps as i have a ton of things on the go and without them things might get forgotten, Thats the main reason i always ask for a bug.
     
    eizenhorn likes this.
  26. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    423
    Experiencing the exact same issue! Can someone link to the issuetracker? It is really frustrating since my game works differently in the editor (in play mode) and in build and it is related to the issue described.

    EDIT: nevermind, I've found it: https://issuetracker.unity3d.com/issues/aspectratiofitter-new-restriction-in-2020-dot-2

    EDIT 2: It is really weird, because in my case something affects also the screen space canvases. Fit in Parent and Envelope aspect ratio fitters doesn't work in build (despite that they seems to be working just fine in the editor)
     
  27. vvtthristov

    vvtthristov

    Joined:
    Jul 28, 2020
    Posts:
    2
    There is another issue which I believe is not related to the one you linked (and eizenhorn created?)
    I believe found another bug (created (Case 1305265)) where fit in parent (and potentially envelope in parent) where while the ARF is create it doesn't set the sizes of the element because it believes there is no parent:
    Code (CSharp):
    1.         protected override void OnEnable()
    2.         {
    3.             base.OnEnable();
    4.             SetDirty();
    5.             m_DoesParentExist = rectTransform.parent ? true : false;
    6.         }
    which fails the check in SetDirty -> UpdateRect:
    Code (CSharp):
    1.                 case AspectMode.FitInParent:
    2.                 case AspectMode.EnvelopeParent:
    3.                 {
    4.                     if (!DoesParentExists())
    5.                         break;
    For the first issue with the warning (Case 1302464) I believe it only needs to have canvas on the same GameObject where the AspectRatioFitter is placed. I really don't see the reason behind this limitation too, because I can easily have a ARF inside the canvas as another GO or the Canvas inside the ARF and there is no warning and the limitation will be bypassed.
    I haven't yet checked how many ARF in the same GO as the canvas I have in the scenes but i am not yet there due to the other issue which is harder to workaround globally.
     
  28. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
    Was this fixed in Unity 2020.2.2? Downloading it now.

    As a workaround, I'm using an older version of the AspectRatioFitter source code.
     
  29. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    It has not been yet, working on it :)
     
    eizenhorn likes this.
  30. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    The "SuperAspectRatioFitter" hack worked for me.
    Had the same issue in player builds for Windows with Unity3D 2020.2.2
     
  31. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Fix is done will get into the branch asap.

    The fix that broke it was done for 2 reasons
    1) IsAspectModeValid makes sure that if we are trying to EnvelopeParent, or FitInParent and we dont have a parent we ignore it.
    2) if we are on a canvas that is a fixed size (such as all non world space root canvas's are) we dont process them either as what happens is the ARF would update the canvas, and then the canvas would readjust its size to match the screen. This would lead to a infinite update of the size and terrible performance.

    The issue was with reason 2 and the fact we didnt allow non root canvas's from having the ARF. Are non root Canvas's dont update to a fixed size (you can control their RT) there is no reason to not allow a ARF to control it.
     
  32. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Thanks!
     
  33. bcapdevila

    bcapdevila

    Joined:
    Oct 26, 2016
    Posts:
    18
    Same issue here. This is preventing us to upgrade to Unity 2020.2.3f1 for now. Even though, disabling/enabling the fitter does work as a workaround, I'd prefer waiting for the fix than adding the workaround in every place we use the fitter (several game objects).

    @phil-Unity are you planning to deliver this fix in a new version of the Unity-UI UPM package? When?

    Thanks
     
  34. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    304
    Glad to see I'm not the only one affected by this bug, and that it's being fixed. It couldn't be made available soon enough.
     
  35. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    UI isn't shipped via UPM. but will be pushed out with the next Unity release on the appropriate versions. If you want i can give you the fix and you can override the package locally until the proper version is out. Let me know.
     
    iamarugin likes this.
  36. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
    Please post the fix here! It will help others!
     
  37. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Temp fix already mentioned in my initial post of this thread :)
     
    wagenheimer likes this.
  38. bcapdevila

    bcapdevila

    Joined:
    Oct 26, 2016
    Posts:
    18
    Yeah, I'd appreciate if you also post the fix @phil-Unity so that we can compare with @eizenhorn's one (thanks by the way to raise the problem and propose the fix)
     
  39. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
    As stated in the early posts, here is the solution.
    Its this simple. Only couple lines of code to solve it for me while we wait for the fix to be released.
    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class AspectRatioFitterHacked : AspectRatioFitter
    4. {
    5.     protected override void Start()
    6.     {
    7.         base.Start();
    8.         SetDirty();
    9.     }
    10. }
    upload_2021-2-11_1-23-17.png
     
  40. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    879
    Can't wait for the temporary fix.
     
  41. wagenheimer

    wagenheimer

    Joined:
    Jun 1, 2018
    Posts:
    322
    2020.2.4 is out!

    UI: Fixed an issue where if a aspectRatioFitter has a parent assigned afterwards the cached parent wouldn't update properly. (1291977)

    Did that solve all of our problems? Lets test.
     
    IggyZuk likes this.
  42. IggyZuk

    IggyZuk

    Joined:
    Jan 17, 2015
    Posts:
    43
    It's working!
    2020.2.4
     
    wagenheimer and zezba9000 like this.
  43. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    It's not. Initial issue not fixed in 2020.2.4 (but it fixes other issue mantioned in this thread I think)
    upload_2021-2-14_21-41-16.png

    It's still absolutely the same
    upload_2021-2-14_21-43-18.png
    upload_2021-2-14_21-43-32.png
    upload_2021-2-14_21-43-40.png

    Initial issue fix in review
    upload_2021-2-14_21-47-26.png
     
    Last edited: Feb 14, 2021
    wagenheimer and IggyZuk like this.
  44. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Backport in progress
    upload_2021-2-15_20-33-37.png
    upload_2021-2-15_20-33-52.png
     
  45. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,681
    Finally backported to 2020.2.7f1!

    upload_2021-3-9_17-24-51.png
     
  46. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    178
    @phil-Unity Can you please use UPM, or at least update Changelog
     
  47. Alejandro-Martinez-Chacin

    Alejandro-Martinez-Chacin

    Joined:
    Oct 15, 2013
    Posts:
    147
    How do I get this fix? it says that it was backported as far as 2020.2.
    I'm on 2021.1.12f1, see attached screenshot.
    Screen Shot 2021-06-30 at 11.31.39 AM.png
     
  48. LokoSolo

    LokoSolo

    Joined:
    Aug 3, 2018
    Posts:
    4
    Can you please also fix this on 2020.3 LTS?
     
  49. Eviral

    Eviral

    Joined:
    Mar 20, 2013
    Posts:
    35
    Hi,

    I'm using 2021.3.3f1 and this message is still here :(
    "You cannot use this Aspect Mode because this Component is attached to a Canvas with a fixed width and height"

    002.png

    @phil-Unity, in my case (Canvas with RenderMode=ScreenSpaceCamera and CanvasScaler with UIScaleMode=ScaleWithScreenSize) how can i use Aspect Ratio Fitter as shown on the screen capture please ?

    The AspectRatioFitterHacked.cs tweak doesn't work at all :(

    What solution could you suggest me please ?
    The only working solution i found is to create an empty gameobject under the canvas gameobject and to move the AspectRatioFitter on this child gameobject (so ugly solution !!!)

    Please help, i'm stuck on it and i can't upgrade to Unity latests versions because of that :(


    May be this change was made to fix a performance issue with AspectRatioFitter but i' have never notice performances issues before so for me this fix is bringing more problems than solved problems :(


    Thanks a lot

    Fred
     
  50. Alejandro-Martinez-Chacin

    Alejandro-Martinez-Chacin

    Joined:
    Oct 15, 2013
    Posts:
    147
    @Eviral
    Hey Fred, try the following, that's your main Canvas, nest another canvas under it with most things 'inherited' and attach the AspectRatioFitter there, see if that works?

    In my experience it sometimes requires a lot of tweaking and shuffling (i.e. the Graphics Raycaster has to be put in there instead) but when needing to force aspect ratio (for some, I got away with only fixing some screens and not needing this for all) I went that way and it worked.