Ошибка: DirectoryNotFoundException: Could not find a part...
Добавлено: 10 янв 2021, 12:33
Всем привет! Решил реализовать сохранение в Unity через текстовый файл (версия 2019.4.8f1), в результате столкнулся с ошибкой: DirectoryNotFoundException: Could not find a part of the path "C:\Users\popov\SkyF\Assets\StreamingAssets\CurrentMapAndLevel.txt". SkyF - имя проекта, StreamingAssets - папка, созданная вручную, CurrentMapAndLevel.txt - сам файл.
Как работает система: Есть FileChecker, который проверяет, создан ли файл. В противном случае мы получаем исключение, обрабатывая который мы создаем файл и записываем основные данные:
С помощью FileReader я пытаюсь получить данные:
Буду очень благодарен, если поможете
P.S. Если создать файл вручную (вписав значение) все работает...
Как работает система: Есть FileChecker, который проверяет, создан ли файл. В противном случае мы получаем исключение, обрабатывая который мы создаем файл и записываем основные данные:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class FileChecker : MonoBehaviour
{
public static string path = Application.dataPath + "/";
public static string nameFile = "CurrentMapAndLevel.txt";
public static void IsFileCreated()
{
try
{
StreamReader sr = new StreamReader(path + "/" + nameFile);
}
catch
{
File.Create(path + "/" + nameFile);
StreamWriter writer = new StreamWriter(path + "/" + nameFile);
writer.Write("1-1");
writer.Close();
}
}
}
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class FileChecker : MonoBehaviour
{
public static string path = Application.dataPath + "/";
public static string nameFile = "CurrentMapAndLevel.txt";
public static void IsFileCreated()
{
try
{
StreamReader sr = new StreamReader(path + "/" + nameFile);
}
catch
{
File.Create(path + "/" + nameFile);
StreamWriter writer = new StreamWriter(path + "/" + nameFile);
writer.Write("1-1");
writer.Close();
}
}
}
С помощью FileReader я пытаюсь получить данные:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class FileReader : MonoBehaviour
{
public static string path = Application.dataPath + "/StreamingAssets";
public static string nameFile = "CurrentMapAndLevel.txt";
public static string LevelInfo = "";
public static (byte, byte) GetLevel()
{
StreamReader sr = new StreamReader(path + "/" + nameFile);
LevelInfo = sr.ReadLine();
sr.Close();
char[] array = LevelInfo.ToCharArray();
return (Convert.ToByte(array[0].ToString()), Convert.ToByte(array[2].ToString())); //returns: current map, current level
}
}
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class FileReader : MonoBehaviour
{
public static string path = Application.dataPath + "/StreamingAssets";
public static string nameFile = "CurrentMapAndLevel.txt";
public static string LevelInfo = "";
public static (byte, byte) GetLevel()
{
StreamReader sr = new StreamReader(path + "/" + nameFile);
LevelInfo = sr.ReadLine();
sr.Close();
char[] array = LevelInfo.ToCharArray();
return (Convert.ToByte(array[0].ToString()), Convert.ToByte(array[2].ToString())); //returns: current map, current level
}
}
Буду очень благодарен, если поможете
P.S. Если создать файл вручную (вписав значение) все работает...