Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Grid Layout Group and dynamic cell size

Discussion in 'UGUI & TextMesh Pro' started by PolygonFormation, Oct 2, 2014.

  1. PolygonFormation

    PolygonFormation

    Joined:
    Apr 1, 2009
    Posts:
    68
    Hello there,

    I'm running 4.6 b20 and can't figure out (except in an ugly update) how to dynamically resize my GridLayout cell size in code, based on its panel container width (a relative canvas width), like so:

    Code (CSharp):
    1.  
    2. inventoryGrid.cellSize = Vector2.one * Mathf.RoundToInt(inventoryPanel.rect.width/2 - 16);
    That works in a Start(), but I want to catch like a "resize event" to trigger this code, any idea ?

    Thank you very much,

    smax
     
  2. ChoMPi

    ChoMPi

    Joined:
    Jul 11, 2013
    Posts:
    112
  3. PolygonFormation

    PolygonFormation

    Joined:
    Apr 1, 2009
    Posts:
    68
    Great worked , Thank you !
     
  4. djnewty

    djnewty

    Joined:
    Apr 7, 2015
    Posts:
    40
    leads to page not found now :/ .,.......
     
    ina likes this.
  5. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    62
  6. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    62
    OK, so the problem is that the first one must be overridden is a custom UIBehaviour subclass:

    Code (CSharp):
    1. /// <summary>
    2. ///   <para>This callback is called if an associated RectTransform has its dimensions changed.</para>
    3. /// </summary>
    4. protected virtual void OnRectTransformDimensionsChange()
    5. {
    6. }
    I just want to plug to an existing rect transform, without any custom component.

    The second one, Transform.hasChanged, doesn't know about RectTransform. So if the center has not moved but it was resized, it may not react at all.


    Maybe I will check any window size changed by storing the screen dimensions and comparing it to last frame, as in https://forum.unity.com/threads/window-resize-event.40253/

    But at the bottom of said thread, one suggests to use OnRectTransformDimensionsChange with a custom UIBehaviour (probably covering the whole screen, but invisible)! So I could just as well make a custom UIBehaviour and put it directly on the object I want to monitor...
     
unityunity