unity 电脑端和安卓端存储数据

1.本地txt存储数据


    ///


    /// 书写文本
    ///

    ///
    ///
    public void WriteTXT(string fileName, string s)
    {
        string url = "";
        if (Application.platform == RuntimePlatform.Android)
        {
            url = Application.persistentDataPath + "/" + fileName;

        }
        else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            url = Application.streamingAssetsPath + "/" + fileName;

        }
        if (File.Exists(url))
        {
            Debug.Log("已存在");
            File.WriteAllText(url, string.Empty);//清空一次
            File.WriteAllText(url, s);
            return;
        }
        else
        {
            FileStream fs = new FileStream(url, FileMode.Create);
            fs.Close();
            File.WriteAllText(url, string.Empty);//清空一次
            File.WriteAllText(url, s);
            Debug.Log("创建");
        }

    }
    ///


    /// 读取文本
    ///

    ///
    ///
    public string LoadTXTtoString(string fileName)
    {
        string url = "";
        string s = "";

        if (Application.platform == RuntimePlatform.Android)
        {
            url = Application.persistentDataPath + "/" + fileName;
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            url = Application.streamingAssetsPath + "/" + fileName;
        }
        s = File.ReadAllText(url);
        return s;
    }

2.PlayerPrefs注册列表存储数据

PlayerPrefs.SetInt("HuiYiShiID", id);

PlayerPrefs.SetString("HuiYiShiData", data);

PlayerPrefs.GetInt("HuiYiShiID");

 PlayerPrefs.GetString("HuiYiShiData");

你可能感兴趣的:(unity,android,unity,电脑)