Ошибка "Sharing violation on path..." Unity

Сделал сохранение карты в файл расширением .dsmap (ds - сокращение названия игры), и возникает ошибка

"IOException: Sharing violation on path <Путь я убрал>
System.IO.FileStream..ctor (System.String path, System.IO.FileMode 
mode, System.IO.FileAccess access, System.IO.FileShare share, 
System.Int32 bufferSize, System.Boolean anonymous, 
System.IO.FileOptions options) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode 
mode, System.IO.FileAccess access, System.IO.FileShare share, 
System.Int32 bufferSize, System.IO.FileOptions options) (at 
<17d9ce77f27a4bd2afb5ba32c9bea976>:0)". 

Вот скрипт

using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.InputSystem;
[Serializable]
public class MapSaver : MonoBehaviour
{
    public string MapName;
    public static string path;
    // Start is called before the first frame update
    void Start()
    {
        if (System.IO.Directory.Exists(Application.persistentDataPath + "/maps"))
        {
            if (System.IO.Directory.Exists(Application.persistentDataPath + "/maps/" + MapName + ".dsmap"))
            {
                print("file exists!");
            }
            else
            {
                System.IO.File.Create(Application.persistentDataPath + "/maps/" + MapName + ".dsmap");
            }
        }
        else
        {
            System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/maps");
            if (System.IO.Directory.Exists(Application.persistentDataPath + "/maps/" + MapName + ".dsmap"))
            {
                print("file exists!");
            }
            else
            {
                System.IO.File.Create(Application.persistentDataPath + "/maps/" + MapName + ".dsmap");
            }
        }
        path = Application.persistentDataPath + "/maps/" + MapName + ".dsmap";
        //System.IO.File.WriteAllText(path, "hi");
        //print(System.IO.File.ReadAllText(path));
        WriteObj(0, "Assets/prefabs/ghost_blue_1.prefab", new Vector2(0, 0), gameObject.transform.rotation);
    }
    public static void WriteObj(int ID, string prefabpath, Vector2 pos, Quaternion rot)
    {
        bool IsEx = false;
        string[] text = File.ReadAllLines(path);
        List<string> LinesToWrite = new();
        string key = ID + "prefabpath";
        for (int line = 0; line < text.Length; line++)
        {
            if (text[line].StartsWith($"{key}=") && !IsEx)
            {
                LinesToWrite.Add($"{key}={prefabpath}");
                IsEx = true;
                continue;
            }

            LinesToWrite.Add(text[line]);
        }
        if (!IsEx)
        {
            LinesToWrite.Add($"{key}={prefabpath}");
        }
        key = ID + "pos";
        for (int line = 0; line < text.Length; line++)
        {
            if (text[line].StartsWith($"{key}=") && !IsEx)
            {
                LinesToWrite.Add($"{key}={pos}");
                IsEx = true;
                continue;
            }

            LinesToWrite.Add(text[line]);
        }
        if (!IsEx)
        {
            LinesToWrite.Add($"{key}={pos}");
        }
        key = ID + "rot";
        for (int line = 0; line < text.Length; line++)
        {
            if (text[line].StartsWith($"{key}=") && !IsEx)
            {
                LinesToWrite.Add($"{key}={rot}");
                IsEx = true;
                continue;
            }

            LinesToWrite.Add(text[line]);
        }
        if (!IsEx)
        {
            LinesToWrite.Add($"{key}={rot}");
        }

    }
    // Update is called once per frame
    void Update()
    {
        var r = gameObject.transform.rotation;

    }
}

С расширением .txt тоже не работает.


Ответы (0 шт):