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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scroll ScrollRect to the first entry if all entrys loaded from Webserver

Discussion in 'UGUI & TextMesh Pro' started by crypt0, Jan 20, 2016.

  1. crypt0

    crypt0

    Joined:
    May 25, 2015
    Posts:
    55
    Hi guys,

    I am loading a txt file from my webserver with update informations. In my UI I have a ScrollRect. In my script I load the file, split it to an string array and in an foreach loop I instantiate for every entry a new Text UI element and set the content Rect of my ScrollRect as parent.

    The problem is, that the ScrollRect beginns at the bottom. I have tested to set it to the top with

    Code (CSharp):
    1.  scrollrect.normalizedPosition = new Vector2(0, 1);
    but nothing happens.

    Here is the complete code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. public class UpdateInformater : MonoBehaviour {
    6.     private string[] content;
    7.     public Transform textBox;
    8.     public Text infoText;
    9.     public ScrollRect scrollrect;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         string url = "http://example.com/update_informations.txt";
    14.         WWW www = new WWW(url);
    15.         StartCoroutine(WaitForRequest(www));
    16.        
    17.     }
    18.     IEnumerator WaitForRequest(WWW www)
    19.     {
    20.         yield return www;
    21.         // check for errors
    22.         if (www.error == null)
    23.         {
    24.             content = www.text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
    25.  
    26.             foreach (string cont in content) {
    27.                 Debug.Log(cont);
    28.                 Text updateInfo = Instantiate(infoText, new Vector2(0,0), Quaternion.identity) as Text;
    29.                 if (cont != "#paragraph")
    30.                 {
    31.                     updateInfo.text = cont;
    32.                 }
    33.                 else {
    34.                     updateInfo.text = "- - - - - - - - - - - \n - - - - - - - - - - - - ";
    35.                 }
    36.                
    37.                 updateInfo.transform.SetParent(textBox, false);
    38.              
    39.             }
    40.             scrollrect.normalizedPosition = new Vector2(0, 1);
    41.  
    42.         }
    43.         else {
    44.             Debug.Log("WWW Error: " + www.error);
    45.         }
    46.      
    47.     }
    48. }
    49.  
    Any ideas, how to solve the problem?

    Cheers
     
  2. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    You probably have to wait a frame before setting the position or do a LayoutRebuilder.ForceRebuildLayoutImmediate on the content RectTransform before you can set the position.
     
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    The position you want to change is the AnchoredPosition, you should do this in a LateUpdate call.

    Check the various ScrollRect implementations and extensions in the UI Extensions project (link in sig) for more detail.
    What you want is the localposition of the first child index really, applied to the Context GO of the ScrollRect