Search Unity

Setting ScrollRect vNormPos in 'Start()'

Discussion in 'UGUI & TextMesh Pro' started by authorthe, Oct 28, 2014.

  1. authorthe

    authorthe

    Joined:
    Oct 28, 2014
    Posts:
    5
    Hi All

    Thought I'd just post this here before logging a bug, as requested. Maybe you can tell me where I'm going wrong here!

    I'm ultimately trying to create a fairly simple implementation of a datagrid (i.e. 'Table' of 'Row' prefabs, bound to some 'List<>' collection) that can contain any number of 'rows' at runtime and would therefore work quite nicely inside a ScrollRect. I started this project a while ago and am returning to it after several beta versions (now using b21).

    After struggling for a bit to create a test data generator, row prefabs and working out the best anchoring and layout component combinations, I hit a bit of a snag with automatically scrolling the ScrollRect to the 'top' of the newly-generated datagrid in the 'Start()' method I was using to do the population - it simply ignored any attempt to set verticalNormalizedPosition = 1f. For some reason, I've convinced myself that I managed to do exactly this in an earlier beta (17, maybe?).

    As a test and to rule-out any potential problem with my more dynamic example, I grabbed a large chunk of 'Lorem Ipsum' text and put it in a Text object, set to overflow vertically with a ContentSizeFitter. I then added a ScrollRect to the parent object (an Image in this case), set to only scroll vertically. The Text object expanded above and below the ScrollRect's viewport by some considerable way and remained vertically 'centered' in design view, as expected. I then added a script (which I tried attaching to various objects like the parent, the Text and even the Camera) which set verticalNormalizedPosition = 1f. This didn't seem to work in any combination.

    I then added two buttons - one to call verticalNormalizedPosition = 1f and one to call verticalNormalizedPosition = 0. Once the game was running, clicking either of these buttons did exactly what they were supposed to do - scrolled to the top and bottom respectively.

    I've experimented with various other events, but most of them happen repeatedly and would involve a boolean check each time to only happen the once - I really only want to do this once at startup after I've populated a box (or maybe if objects are added/removed on-the-fly, but I can deal with that later).

    Another odd side-effect is that querying verticalNormalizedPosition after pressing 'Play' always results in returning 0 (whether I tried to set it in 'Start()' or not), but, as soon as the ScrollRect is moved in any way, the true current position of 0.5 ('centered') is reported and subsequently updated correctly as it is scrolled.

    I have the test Game which I can package up and provide if anyone needs it, but I imagine most of you would be able to recreate this setup in about 60 seconds.

    Any ideas? TIA