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

Scroll view with newest items on the top instead of the bottom?

Discussion in 'Scripting' started by jcfrizz, Oct 17, 2017.

  1. jcfrizz

    jcfrizz

    Joined:
    Jul 22, 2017
    Posts:
    11
    Newbie here, with probably a newbie question.

    So, I followed through and completed the Scroll view tutorial here:
    https://unity3d.com/learn/tutorials/topics/user-interface-ui/scroll-view
    I now have a good scroll view with a list of buttons.

    My problem is though, when I add buttons to the new list, it adds the buttons to the end of the list. My goal is for the buttons to be added at the top of the list, so that the newest object added to my scroll view is placed at the top of the list.

    Please help if you can. I can't find anything online about this. Thanks.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Are you using some layout? Vertical/grid?

    With vertical, I'm pretty sure you can use: https://docs.unity3d.com/ScriptReference/Transform.SetAsFirstSibling.html
    (or lastsibling, if I got that mixed up)

    I tried your scenario with 1 column grid view, and accomplished adding new buttons to the top of the content with the settings: Start Corner: lower left, Alignment upper left. (constraint = 1).
    But I don't know if there's more to what you're doing than what you described. :)
     
  3. jcfrizz

    jcfrizz

    Joined:
    Jul 22, 2017
    Posts:
    11
    Sorry, could you possibly show me visually what it is you did? I'm not sure what you're describing yourself doing.

    And yeah, so basically there are two scroll view lists, each with buttons as the items in the list. when I click on a button in one list, it transfers over to the other scroll view.

    My problem is that right now when it is added to the other list, it is added to the END of the list. I want the newest to be added to the top.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The first suggestion/option I gave is just a line of code when you add the button.

    The other option is the grid layout component. All I did was describe a few of the settings you see on it, that I used to put the buttons at the top when added.
    Not sure how else to explain it..Let me know if something didn't make sense.
     
  5. ghostmode

    ghostmode

    Joined:
    Sep 26, 2016
    Posts:
    14
    What methos describes is how you'd generally handle this, but I don't think the tutorial code is set up that way.

    Try using 'Insert' instead of 'Add' in the AddItem method, that will add the item to the front of the list.
    Code (CSharp):
    1. void AddItem(Item itemToAdd, ShopScrollList shopList)
    2. {
    3.     shopList.itemList.Insert(0, itemToAdd);
    4. }
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool - if that works, I'm glad.

    Ah, I see..when I clicked the link you posted, I only skimmed through the video and didn't even see the code or realize it was a 9-part series lol. So, that's my bad -- sorry :)
    At first, I thought you had just watched a basic video and expanded the concept all on your own, and the ideas I suggested were very "general/vague" (not specific to the video at all).

    Hopefully that new code works well for you.
     
  7. karsnen

    karsnen

    Joined:
    Feb 9, 2014
    Posts:
    65