Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UI verticalNormalizedPosition not going to bottom of the scroll view

Discussion in 'UGUI & TextMesh Pro' started by Deleted User, Jan 25, 2016.

  1. Deleted User

    Deleted User

    Guest

    I am working with the photon network to make a simple game. I would like this game to implement a simple chat system, however, I have run into a snag.

    The problem comes from the fact that I am unable to make the scroll view, which I am using to display the chat, scroll all the way to the bass, or the last message sent.

    Bellow is the code I am using.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4.  
    5. public class ChatBarManager : MonoBehaviour {
    6.  
    7.     [SerializeField] private RectTransform m_Content;
    8.     [SerializeField] private GameObject m_MessageObject;
    9.     [SerializeField] private float m_TextObjectHeight = 30.0f;
    10.  
    11.     private List<GameObject> m_Messages = new List<GameObject>();
    12.     private float m_ContentHeight = 300;
    13.  
    14.     void Start()
    15.     {
    16.         m_Content.localPosition = new Vector3(0, 0, 0);
    17.     }
    18.  
    19.     public void AddMessage(string message)
    20.     {
    21.         GameObject newMessage = (GameObject)Instantiate(m_MessageObject);
    22.         newMessage.transform.SetParent(m_Content, false);
    23.         newMessage.GetComponent<Text>().text = message;
    24.         ((RectTransform)newMessage.transform).anchoredPosition = new Vector2(0, -(m_TextObjectHeight * m_Messages.Count));
    25.         m_Messages.Add(newMessage);
    26.  
    27.         if (m_Messages.Count * m_TextObjectHeight > 200)
    28.         {
    29.             m_Content.GetComponentInParent<ScrollRect>().verticalNormalizedPosition = 0;
    30.         }
    31.  
    32.             if (m_Messages.Count * m_TextObjectHeight > m_ContentHeight)
    33.         {
    34.             m_Content.sizeDelta = new Vector2(0, m_ContentHeight + m_TextObjectHeight);
    35.             m_ContentHeight = m_ContentHeight + m_TextObjectHeight;
    36.         }
    37.     }
    38.    
    39. }
    While this code does indeed make the scroll view move down it only moves far enough to show the second last message send.

    I have made so that the code works with the scroll view created when you first create a scroll view
    Create > UI >Scroll View

    The m_MessageObject is just a normal text object I saved as a prefab.

    Any help would be appreciated.
     
  2. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    You'll probably have to wait a frame or do a LayoutRebuilder.ForceLayoutRebuildImmediate(m_Content) before setting of the scroll rect position will work.
     
  3. Deleted User

    Deleted User

    Guest

    Thank you very much. I used an IEnumerator to wait for a frame and then ran the verticalNormalizedPosition = 0.
    This returned the desired result.

    Again thank you.
     
    Keepps65 likes this.