Search Unity

Modify UI text from script at runtime in android device

Discussion in 'Scripting' started by Harokel, Oct 15, 2017.

  1. Harokel

    Harokel

    Joined:
    Nov 2, 2012
    Posts:
    51
    Hi!
    I want to modify an UI text at runtime when I press buttons.
    The problem is that it doesn't appear on the Android device although it does appear in the editor and in play mode.
    The UI Text is child of a Canvas.

    Can anybody help me?

    Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;   // For List
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.IO;                    // For parsing text file, StringReader
    6. using System.Text;
    7. using System;
    8. using UnityEngine.EventSystems;     // Required when using Event data.
    9. //
    10. public class TextImporterPrefab : MonoBehaviour
    11. {
    12.     //
    13.     public string filePath;             //Es el nombre de la ruta
    14.     public int currentLine;
    15.     public int endAtLine;
    16.     // Array donde se guardan los textos
    17.     string[] texto = new string[60];
    18.     // Para instanciar el texo
    19.     public GameObject textPrefab;
    20.     public Text myText;
    21.     //
    22.     void Start()
    23.     {
    24.         // Crear nuevo prefab de texto
    25.         GameObject nuevoTexto = Instantiate(textPrefab) as GameObject;
    26.         nuevoTexto.transform.SetParent(this.transform, false);
    27.         //
    28.         filePath = Path.Combine(Application.streamingAssetsPath, "textos.txt");
    29.         StreamReader theReader = new StreamReader(filePath, Encoding.Default);          //Encoding.Default para que aparezcan las tildes y las eñes (UTF-8)
    30.         //
    31.         if (filePath != null)
    32.         {
    33.             for (int i = 0; i < 50; i++)
    34.             {
    35.                 texto[i] = theReader.ReadLine();
    36.             }
    37.             theReader.Close();
    38.             //
    39.             for (int i = 0; i < 50; i++)
    40.             {
    41.                 //Debug.Log(names[i]);
    42.             }
    43.         }
    44.         else
    45.             Debug.Log("Nothing");
    46.         //
    47.         if (endAtLine == 0)
    48.         {
    49.             endAtLine = texto.Length - 1;
    50.         }
    51.         myText = nuevoTexto.GetComponent<Text>();
    52.     }
    53.     //
    54.     void Update()
    55.     {
    56.         myText.text = texto[currentLine];
    57.     }
    58.     //
    59.     public void Call()
    60.     {
    61.         currentLine += 1;
    62.     }
    63.     //
    64. }
    65.  
    P.D: Sorry for my bad english :-(
     
  2. olegzheleztsov

    olegzheleztsov

    Joined:
    Oct 15, 2017
    Posts:
    9
    Note that on some platforms it is not possible to directly access the StreamingAssets folder because there is no file system access in the web platforms, and because it is compressed into the .apk file on Android. On those platforms, a url will be returned, which can be used using the UnityWebRequest class.
    https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html
     
  3. Harokel

    Harokel

    Joined:
    Nov 2, 2012
    Posts:
    51
    Thank you for the information ;-)