【问题标题】:When reading text file, I have an error about sharing violation in Unity3D读取文本文件时,我在 Unity3D 中遇到共享冲突错误
【发布时间】:2016-10-04 07:43:24
【问题描述】:

我有一些关于 IOException:共享冲突的错误。 我的模拟程序每 1/60 秒创建一个文本文件。 此代码由 Lua 脚本编写。

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

  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# 代码。

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中,会出现这个错误。

错误

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)

可以读取这个文件吗?或者有什么替代方法吗?

【问题讨论】:

    标签: c# parsing unity3d lua stream


    【解决方案1】:

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

    此外,在 Update 中写入和读取文件是一个坏主意。这些磁盘操作太多了,这将非常慢,并且还使同步变得不可能,正如您已经体验过的那样。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多