Search Unity

How can I do this with script? (Message scrolls upwards)

Discussion in 'Scripting' started by RockNRogueTR, Aug 31, 2019.

  1. RockNRogueTR

    RockNRogueTR

    Joined:
    May 2, 2019
    Posts:
    2
    As shown in the example, messages move upwards as new messages arrive. I couldn't find a sample code that I could do this way. Is it possible to help?

    Zarya 1
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I'd assume you need some list of messages, a way to animate / move them upwards over time and a way to detect and display a new message. Then when you detect a new message, take all existing messages and move them up over time. After there is enough space, insert the new message at the bottom. If there is some limit to the size of the window, maybe fade out the topmost message when it gets too close to the top. Rinse and repeat.

    This is a very general answer to a very general question. As always there are a billion ways of implementing anything, so it may depend on what you are using it for, where you are using it, or what your other requirements are. But generally, the above would be the idea behind it.
     
    RockNRogueTR likes this.
  3. RockNRogueTR

    RockNRogueTR

    Joined:
    May 2, 2019
    Posts:
    2

    Thanks,
    This is a game, text based game. ll use Unity, C#
    I thought about it, but I could not find a solution to this situation; How the Gamer 'll scroll back down all messages, if i'll use animation. I think, it's simple, record animation but out of control, Am i right?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    If it's a text based game i dont think you actually need animations. You could simply move up all old messages over time and add in the new one when there is enough space. Depending on whether you need to keep the messages, you could either disable or delete any messages that get moved out of view. Also depends on whether you need to be able to scroll back up (which obviously only works if you save the messages).
    As for the insertion, i'd create a List or Queue "pendingMessages", then as long as its size is greater than 0, i would move the text objects up at some predefined speed over Time.deltaTime. As soon as there is enough new space at the bottom to insert a new element, take the top element from pendingMessages and put it there.
    This way, as long as there are some pending messages (if it's at all possible for more than 1 message to arrive while another one gets displayed), all previously displayed messages get "scrolled up", creating new space under them. After inserting the last pending message, the size of pendingMessage is 0, so the upwards motion stops until any new messages need to be displayed.

    Hope this is applicable to your situation.
     
    RockNRogueTR likes this.
  5. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Disregard everything else pretty much. This will work:


    All you have to do is setup values in the inspector so it only shows one message per row and whenever you want to show a new message just spawn it as a separate object.
     
    RockNRogueTR likes this.