首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在读取文本文件时,我在Unity3D中出现了一个有关共享冲突的错误

在读取文本文件时,我在Unity3D中出现了一个有关共享冲突的错误
EN

Stack Overflow用户
提问于 2016-06-04 08:10:47
回答 1查看 1.8K关注 0票数 0

关于IOException,我有一些错误:共享违规。我的模拟程序创建每1/60的文本文件。这个代码是由Lua脚本编写的。

这是Lua密码。data.txt文件是每1/60重写一次。因此,文件中的内容每1/60改变一次。

代码语言:javascript
运行
复制
  function dataListener:post( t )
    --local fn = "data"
    local fn = "time"
    --local filename = fn.."_"..60*t..".txt"
    local filename = fn..".txt"
    local file = io.open(filename, "w+")


    --file:write(string.format("%d; %d; %d\n",x,y,z))
    --file:write("%d; %d; %d\n", xx, yy, zz)
    --file:write(xx/100," ", yy/100," ", zz/100," ", wxx/100," ", wyy/100," ", wzz/100," \n")
    file:write(60*t," \n")
    file:close()
  end

Unity3D在Update()函数中读取该文件。

这是Unity3D C#代码。

代码语言:javascript
运行
复制
using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class Textparsing : MonoBehaviour
{
    public GUISkin skin;
    private int currentTextNumber;// text number


    string s;
    // Use this for initialization
    void Start()
    {
        currentTextNumber = 0;
        s = LoadTextFile("text/time.txt");
        print(s);
    }

    // Update is called once per frame
    void Update()
    {

        s = LoadTextFile("text/time.txt");
        print(s);
    }
    string LoadTextFile(string fileName)
    {
        string t = "";
        string line = "";
        StreamReader sr = new StreamReader(Application.dataPath + "/Resources/" + fileName);
        if (sr == null)
        {
            print("Error : " + Application.dataPath + "/Resources/" + fileName);
        }
        else
        {
            line = sr.ReadLine();
            while (line != null)
            {
                t += line;
                line = sr.ReadLine();
                if (line != null)
                {
                    t += "\n";
                }
            }
            sr.Close();
            // print("Loaded " + Application.dataPath + "/Resources/db/" + fileName);
        }
        return t;
    }

    //gui
    void OnGUI()
    {
        //read
        GUI.skin = skin;

        int halfW = Screen.width / 2;
        int halfH = Screen.height / 2;

        int x = 40;
        int y = halfH + 40;

        GUI.Label(new Rect(x, y, halfW * 1, halfH * 1.2f), s);
    }
}

在unity3D中,会发生此错误。

错误

代码语言:javascript
运行
复制
IOException: Sharing violation on path C:\Users\Public\Documents\Unity Projects\textparsing\Assets\Resources\text\time.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.File.OpenRead (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:363)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:167)
System.IO.StreamReader..ctor (System.String path)
(wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
Textparsing.LoadTextFile (System.String fileName) (at Assets/Textparsing.cs:36)
Textparsing.Update () (at Assets/Textparsing.cs:29)

可以读取这个文件吗?或者有没有其他的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-04 23:43:01

您很可能在从其他程序中读取文本文件时尝试读取它。这是不可能的,你需要等到一个程序写完,关闭文件,然后团结可以打开和读取它。

此外,在Update中编写和读取文件也是个坏主意。这是太多的磁盘操作,这将是令人难以置信的缓慢,也使得同步不可能,正如您已经经历的。

对于一个解决方案:这两个程序通信的目的是什么?为什么这么快?如果您每隔一段时间就更新一次,那么您可以使用FileSystemWatcher类来监视文本文件,并且每次它更改时,都会调用观察者更改的事件。但是对于你的问题,可能还有很多其他更好的解决方案。可能是SQL数据库,也可能是用于统一、套接字通信的插件,不管用例是什么,它取决于。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37627960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档