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

Read Text files on mobile

Discussion in 'Scripting' started by Leandre5, Mar 9, 2021.

  1. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Hello,

    I would like to make a game on a mobile phone that has a large database held in a text file.

    On PC it works fine but once build and sent on my phone my texts are no longer displayed. I know it's because of the path which is different, but how to solve this problem.

    I did some research and came across this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using System.IO;
    6. using System.Linq;
    7. public class DetermineDuel : MonoBehaviour
    8. {
    9.     #region  Variables
    10.     public TMP_Text DuelTitle;
    11.  
    12.     /// Tous les PATH
    13.     string DuelTitlepath = "Assets/DataText/Duel/DuelsTitle.txt";
    14.     string DuelRulepath = "Assets/DataText/Duel/DuelsRules.txt";
    15.  
    16.     ////// Panel Rules ///////////
    17.     [SerializeField]
    18.     private GameObject PanelRules;
    19.     [SerializeField]
    20.     private TMP_Text RuleText;
    21.  
    22.     #endregion
    23.  
    24.     void Start()
    25.     {
    26.         // prend les fichiers txt dans des tableaux
    27.         string[] Titlelines = System.IO.File.ReadAllLines ( Titlepath);
    28.         string[] Ruleslines = System.IO.File.ReadAllLines ( DuelRulepath);
    29.  
    30.         // aléatoirement definit le duel ( en fonction du dossier ) et la règle qui va avec
    31.         int rand = Random.Range(0,Titlelines.Length);
    32.         DuelTitle.text = Titlelines[rand];
    33.         RuleText.text = Ruleslines[rand];
    34.     }
    35.  
    36.     // active le panel des regles
    37.     public void ShowRules()
    38.     {
    39.         PanelRules.SetActive(true);
    40.     }
    41.  
    42.     // desactive le panel des règles
    43.     public void HideRules()
    44.     {
    45.         PanelRules.SetActive(false);
    46.     }
    47. }
    but I don't understand how it works (I've tried to use it anyway), if I just add it to my path in this way,

    Is it enough to find my text file once in the game on mobile?
    Because my Text file is exported to it well so that I download my game, the text file is part of the files.

    I thank the person who will come to my rescue.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    When Unity builds your game, your directory structure is destroyed and all of your assets are packed into some nice efficient data files. That means any "raw files" that you wanted to include in your build won't make it through the process.

    You have two options.

    1. Instead of using string paths and reading the file yourself, create TextAsset fields and drag your text files in in the inspector:
    Code (CSharp):
    1. [SerializeField]
    2. private TextAsset DuelTitleAsset;
    3.  
    4. // later on...
    5. string DuelTitleContents = DuelTitleAsset.text;
    2. Put the file into the StreamingAssets folder, and additionally, you must use UnityWebRequest to access the file, rather than System.IO.File. This is all documented here:
    Unity - Manual: Streaming Assets
     
    Leandre5 likes this.
  3. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    In the case I am using TextAsset, am I able to read the text line by line?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Leandre5 likes this.
  5. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Thanks a lot
     
  6. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
  7. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using System;
    6. using Random=UnityEngine.Random;
    7.  
    8.  
    9. public class DetermineDuel : MonoBehaviour
    10. {
    11.     #region  Variables
    12.     public TMP_Text DuelTitles;
    13.  
    14.     //// Transform .txt en asset
    15.     [SerializeField]
    16.     private TextAsset DuelTitlesAsset;
    17.     [SerializeField]
    18.     private TextAsset DuelRulesAsset;
    19.  
    20.     ////// Panel Rules ///////////
    21.     [SerializeField]
    22.     private GameObject PanelRules;
    23.     [SerializeField]
    24.     private TMP_Text RulesText;
    25.  
    26.     #endregion
    27.  
    28.     void Start()
    29.     {
    30.         string DuelTitlesContents = DuelTitlesAsset.text;
    31.         string[] linesTitles = DuelTitlesContents.Split(new[] { Environment.NewLine },StringSplitOptions.None);
    32.  
    33.         string DuelRulesContents = DuelRulesAsset.text;
    34.         string[] linesRules = DuelRulesContents.Split(new[] { Environment.NewLine },StringSplitOptions.None);
    35.  
    36.  
    37.         // aléatoirement definit le duel ( en fonction du dossier ) et la règle qui va avec
    38.         int rand = Random.Range(0,linesTitles.Length);
    39.         DuelTitles.text = linesTitles[rand];
    40.         RulesText.text = linesRules[rand];
    41.     }
    42. }
    43.  
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Don't use environment.NewLine.

    The newLine character on mobile devices may be different from the one on your computer. Use either the example that can handle any NewLine scheme, or use the NewLine character type that your text file has.
     
  9. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Thank you su much it works perfectly