Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

(FIXED) Scrollable InputField: Cannot seem to update the InputField after setting its Text

Discussion in 'UGUI & TextMesh Pro' started by asperatology, Feb 27, 2016.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    TL;DR:
    My main gripe with the Scrollable InputField is it doesn't update immediately when the output messages set to the InputField. I even tried explicitly to rebuild the InputField, but it's not working.

    I expected the Scrollable InputField to immediately display all of the viewable text messages in the InputField, and the user can scroll up and down with the scrollbar to the end in an intuitive way, without having to drag/select the text and move the mouse downwards to refresh the InputField.

    ________________________________________________________________________________________________

    I am making a read-only InputField where the player can copy any strings inside to another place, like an output log file. I need to do this because for WebGL builds, you cannot write anything or a text file to the local computer. (Please prove me wrong. I really really appreciate this.)

    This is the current setup for the Scrollable InputField:



    Panel has Scroll Rect. In the Scroll Rect, the "Content" is pointing towards the Text game object, and the "Viewport" is pointing towards the InputField game object. The Scrollbar is a default bottom to top vertical scrollbar.

    InputField has Mask added.

    Text has Content Size Fitter, with "Vertical Fit" set to Preferred Size, and "Horizontal Fit" set to Unconstrained. It also has a Layout Element with "Flexible Height" set to 4096. Nothing else is set.

    This is what happens when I set the InputField's Text component with dummy output log messages. Note that the dummy output log messages are printed 4 times in total (because I am lazy). You have to actually drag/select the text inside InputField, and drag to the bottom of the InputField to have all of the text to show up:



    That action of dragging/selecting the text to update the InputField is completely buggy and wrong.

    And here's what the log output messages look like. I have separated the output log messages into sections, so you know that I'm just outputting dummy messages 4 times:

    Code (CSharp):
    1. public static void PrintLog() {
    2.     GameMetricLogger log = GameMetricLogger.instance;
    3.     if (GameMetricLogger.instance.stringBuilder == null) {
    4.         Debug.LogError("Print(): Game metrics logger cannot output anything. Please double check.");
    5.         log.outputField.text = "No game metrics report generated.";
    6.     }
    7.     else {
    8.         StringBuilder sB = log.stringBuilder;
    9.         sB.Length = 0;
    10.         //---------------------------------------------------------------------------------------------------------
    11.         //---------------------------------------------------------------------------------------------------------
    12.         sB.AppendLine("Game Metrics Report");
    13.         sB.AppendLine("(Please copy and paste this report somewhere else.)");
    14.         sB.AppendLine("Total Game Time Since Report Is Generated: " + log.totalGameTimeSinceEpoch.ToString("0.000") + " seconds");
    15.         sB.AppendLine();
    16.         sB.AppendLine("Player Name: " + log.playerName);
    17.         sB.AppendLine("Level Difficulty: " + GetLevelDifficulty());
    18.         sB.AppendLine("Unit Attribute Equation Used: " + log.difficultyEquations);
    19.         sB.AppendLine();
    20.         sB.AppendLine("Total Time Played: " + log.totalGameTime.ToString("0.000") + " seconds");
    21.         sB.AppendLine("Total Death: " + log.numberOfDeaths);
    22.         sB.AppendLine("Total Kills: " + log.numberOfKills);
    23.         sB.AppendLine("Total Attacks: " + log.numberOfAttacks);
    24.         sB.AppendLine("Total Splits: " + log.numberOfSplits);
    25.         sB.AppendLine("Total Merges: " + log.numberOfMerges);
    26.         sB.AppendLine();
    27.         sB.AppendLine("Total Time Accumulated When Attacking: " + log.totalAttackTime.ToString("0.000") + " seconds");
    28.         sB.AppendLine("Total Time Accumulated Under Attack: " + log.totalBattleEngagementTime.ToString("0.000") + " seconds");
    29.         sB.AppendLine();
    30.         //---------------------------------------------------------------------------------------------------------
    31.         //---------------------------------------------------------------------------------------------------------
    32.         sB.AppendLine("Game Metrics Report");
    33.         sB.AppendLine("(Please copy and paste this report somewhere else.)");
    34.         sB.AppendLine("Total Game Time Since Report Is Generated: " + log.totalGameTimeSinceEpoch.ToString("0.000") + " seconds");
    35.         sB.AppendLine();
    36.         sB.AppendLine("Player Name: " + log.playerName);
    37.         sB.AppendLine("Level Difficulty: " + GetLevelDifficulty());
    38.         sB.AppendLine("Unit Attribute Equation Used: " + log.difficultyEquations);
    39.         sB.AppendLine();
    40.         sB.AppendLine("Total Time Played: " + log.totalGameTime.ToString("0.000") + " seconds");
    41.         sB.AppendLine("Total Death: " + log.numberOfDeaths);
    42.         sB.AppendLine("Total Kills: " + log.numberOfKills);
    43.         sB.AppendLine("Total Attacks: " + log.numberOfAttacks);
    44.         sB.AppendLine("Total Splits: " + log.numberOfSplits);
    45.         sB.AppendLine("Total Merges: " + log.numberOfMerges);
    46.         sB.AppendLine();
    47.         sB.AppendLine("Total Time Accumulated When Attacking: " + log.totalAttackTime.ToString("0.000") + " seconds");
    48.         sB.AppendLine("Total Time Accumulated Under Attack: " + log.totalBattleEngagementTime.ToString("0.000") + " seconds");
    49.         sB.AppendLine();
    50.         //---------------------------------------------------------------------------------------------------------
    51.         //---------------------------------------------------------------------------------------------------------
    52.         sB.AppendLine("Game Metrics Report");
    53.         sB.AppendLine("(Please copy and paste this report somewhere else.)");
    54.         sB.AppendLine("Total Game Time Since Report Is Generated: " + log.totalGameTimeSinceEpoch.ToString("0.000") + " seconds");
    55.         sB.AppendLine();
    56.         sB.AppendLine("Player Name: " + log.playerName);
    57.         sB.AppendLine("Level Difficulty: " + GetLevelDifficulty());
    58.         sB.AppendLine("Unit Attribute Equation Used: " + log.difficultyEquations);
    59.         sB.AppendLine();
    60.         sB.AppendLine("Total Time Played: " + log.totalGameTime.ToString("0.000") + " seconds");
    61.         sB.AppendLine("Total Death: " + log.numberOfDeaths);
    62.         sB.AppendLine("Total Kills: " + log.numberOfKills);
    63.         sB.AppendLine("Total Attacks: " + log.numberOfAttacks);
    64.         sB.AppendLine("Total Splits: " + log.numberOfSplits);
    65.         sB.AppendLine("Total Merges: " + log.numberOfMerges);
    66.         sB.AppendLine();
    67.         sB.AppendLine("Total Time Accumulated When Attacking: " + log.totalAttackTime.ToString("0.000") + " seconds");
    68.         sB.AppendLine("Total Time Accumulated Under Attack: " + log.totalBattleEngagementTime.ToString("0.000") + " seconds");
    69.         sB.AppendLine();
    70.         //---------------------------------------------------------------------------------------------------------
    71.         //---------------------------------------------------------------------------------------------------------
    72.         sB.AppendLine("Game Metrics Report");
    73.         sB.AppendLine("(Please copy and paste this report somewhere else.)");
    74.         sB.AppendLine("Total Game Time Since Report Is Generated: " + log.totalGameTimeSinceEpoch.ToString("0.000") + " seconds");
    75.         sB.AppendLine();
    76.         sB.AppendLine("Player Name: " + log.playerName);
    77.         sB.AppendLine("Level Difficulty: " + GetLevelDifficulty());
    78.         sB.AppendLine("Unit Attribute Equation Used: " + log.difficultyEquations);
    79.         sB.AppendLine();
    80.         sB.AppendLine("Total Time Played: " + log.totalGameTime.ToString("0.000") + " seconds");
    81.         sB.AppendLine("Total Death: " + log.numberOfDeaths);
    82.         sB.AppendLine("Total Kills: " + log.numberOfKills);
    83.         sB.AppendLine("Total Attacks: " + log.numberOfAttacks);
    84.         sB.AppendLine("Total Splits: " + log.numberOfSplits);
    85.         sB.AppendLine("Total Merges: " + log.numberOfMerges);
    86.         sB.AppendLine();
    87.         sB.AppendLine("Total Time Accumulated When Attacking: " + log.totalAttackTime.ToString("0.000") + " seconds");
    88.         sB.AppendLine("Total Time Accumulated Under Attack: " + log.totalBattleEngagementTime.ToString("0.000") + " seconds");
    89.         sB.AppendLine();
    90.         //---------------------------------------------------------------------------------------------------------
    91.         //---------------------------------------------------------------------------------------------------------
    92.  
    93.         log.outputField.text = sB.ToString();
    94.         log.outputField.Rebuild(CanvasUpdate.MaxUpdateValue);
    95.     }
    96. }
    How can I fix this? Thanks in advance.
     
    Last edited: Feb 27, 2016
  2. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Thanks to @MisterSyntax, this is what I added to fix the InputField:
    Code (CSharp):
    1. public void Update() {
    2.     this.outputField.GetComponentInChildren<Text>().text = this.outputField.text.ToString();
    3. }
    Because the Text component in the InputField is not updating, or rather, it requires a lot more updates than just 1 update tick, it was not able to stretch its text boundaries vertically. By putting the code above in the Update() function, it refreshes the boundaries, and thus stretch the text boundaries vertically.

    And then everything else comes into play nicely.

    Thanks again.