You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity开发古兰经阅读APP:安卓APK中TXT文件文本不显示问题求助

Troubleshooting Android Text Loading Issue in Unity Quran App

Hi Mehmet, let's work through why your text isn't showing up on Android builds—this is a common cross-platform quirk in Unity, and we can fix it easily!

Why It Works in Editor but Fails on Android

In the Unity Editor (and Device Simulator), Application.streamingAssetsPath points to a local folder on your computer, so using File.ReadAllLines works perfectly. But on Android, the StreamingAssets folder is packaged inside the compressed APK archive. The standard File class can't access files inside the APK directly—you need to use Unity's UnityWebRequest to load content from this protected location.

Step 1: Fix the Cross-Platform File Loading Logic

Replace your current script with this updated version, which handles Android-specific loading and fixes a small typo (MonoBehavior → MonoBehaviour):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Linq;
using UnityEngine.Networking;

public class GetTextSureSure : MonoBehaviour 
{
    public Transform contentWindowSure;
    public GameObject recallTextObject;
    public int sureNo;

    private void Start()
    {
        // Use coroutine for asynchronous file loading (required for Android)
        StartCoroutine(LoadAndDisplaySurahText());
    }

    private IEnumerator LoadAndDisplaySurahText()
    {
        string filePath = Path.Combine(Application.streamingAssetsPath, "Kuran/SabanPirisTrCeviri.txt");
        string fullFileContent = string.Empty;

        // Handle Android vs. Editor/Desktop platforms
        if (Application.platform == RuntimePlatform.Android)
        {
            using (UnityWebRequest fileRequest = UnityWebRequest.Get(filePath))
            {
                yield return fileRequest.SendWebRequest();

                if (fileRequest.result != UnityWebRequest.Result.Success)
                {
                    Debug.LogError($"Failed to load text file: {fileRequest.error}");
                    yield break;
                }
                fullFileContent = fileRequest.downloadHandler.text;
            }
        }
        else
        {
            // For Editor, Windows, or Mac builds
            if (File.Exists(filePath))
            {
                fullFileContent = File.ReadAllText(filePath);
            }
            else
            {
                Debug.LogError($"Text file not found at path: {filePath}");
                yield break;
            }
        }

        // Split content into lines and filter for the target surah
        List<string> fileLines = fullFileContent.Split('\n').ToList();
        foreach (string line in fileLines)
        {
            // Skip empty lines to avoid unnecessary instantiations
            if (!string.IsNullOrWhiteSpace(line) && line.StartsWith($"{sureNo}|"))
            {
                GameObject newTextElement = Instantiate(recallTextObject, contentWindowSure);
                newTextElement.GetComponent<Text>().text = line;
            }
        }
    }
}

Step 2: Verify Critical Setup Details

  • File Encoding: Ensure your SabanPirisTrCeviri.txt is saved in UTF-8 encoding (without BOM if possible). Non-UTF-8 text can cause silent loading failures on Android.
  • Build Packaging: Double-check that the text file is placed exactly in Assets/StreamingAssets/Kuran/—Unity automatically includes all StreamingAssets files in the APK, but confirm it wasn't accidentally excluded (check the file's Inspector for default import settings).
  • Device Debugging: Connect your Android device to Unity via USB and enable "Connect Player" in the Console window. This will show you any error messages (like missing files or request failures) that don't appear in the editor.

Bonus Optimization

Your original code set the text before instantiating the object, which can cause unnecessary overhead. The updated script instantiates first, then sets the text—this is more efficient and avoids potential UI glitches.

Let me know if you hit any snags after implementing these changes—I’m happy to help you troubleshoot further!

内容的提问来源于stack exchange,提问作者Mehmet Uçar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:18:14