安卓读取StreamingAssets下的文件
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class TestController : MonoBehaviour
{
    private string _data;
    private void Awake()
    {
        StartCoroutine(Read());
    }


    IEnumerator Read()
    {
        // 获取StreamingAssets路径
        string streamingAssetsPath =
#if UNITY_ANDROID && !UNITY_EDITOR
                "jar:file://" + Application.dataPath + "!/assets/";
#else
            "file://" + Application.streamingAssetsPath + "/";
#endif

        // 构建完整路径
        string filePath = streamingAssetsPath + "Files/FPS.txt";
        // 创建UnityWebRequest对象
        UnityWebRequest www = UnityWebRequest.Get(filePath);

        // 发送请求并等待响应
        yield return www.SendWebRequest();

        // 检查是否有错误
        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.LogError("Failed to load file: " + www.error);
        }
        else
        {
            // 成功加载文件,可以在www.downloadHandler中获取文件的内容
            Debug.Log("File loaded successfully");
            // Debug.Log(www.downloadHandler.text);
            _data =  www.downloadHandler.text;
        }

        // 释放资源
        www.Dispose();
    }
}
上一篇
下一篇