简体   繁体   English

从 unity3d 中的脚本创建文本文件存在延迟/错误

[英]Creating a text file from script in unity3d is having a delay / error

EDIT * Called AssetDatabase.Refresh and the file appears in the editor now but with a loading icon and cannot be written to for some time still.编辑 * 调用 AssetDatabase.Refresh,该文件现在出现在编辑器中,但带有一个加载图标,并且在一段时间内仍无法写入。 The file was previously showing in the chosen location on the SSD just not in unity.*该文件以前显示在 SSD 上的选定位置,只是不统一。 *

I am creating a text file in unity from a script, then the next line i try to write to that file.我正在从脚本统一创建一个文本文件,然后下一行我尝试写入该文件。 The issue is the file is not created before the next line is executed, i can toggle the play button off and on for the editor and the file is still not there and it only appears when i minimize the editor or open another program and then go back.问题是在执行下一行之前未创建文件,我可以关闭和打开编辑器的播放按钮,但文件仍然不存在,它仅在我最小化编辑器或打开另一个程序然后 go 时出现背部。 I can stop play on the editor with the file not there then minimize and maximize the editor window and it will appear where i chose to create it.我可以在文件不存在的情况下停止在编辑器上播放,然后最小化和最大化编辑器 window,它将出现在我选择创建它的位置。 Hopefully i'm explaining my issue correctly.希望我能正确解释我的问题。

This is the code below这是下面的代码

public class ItemDatabase : MonoBehaviour
{

public static GameObject itemContainerPrefab;
private string dataPath;
private string jsonData;
private Dictionary<string, Item> items = new Dictionary<string, Item>();
private Dictionary<string, GameObject> prefabs = new Dictionary<string, GameObject>();
private JSONDataWrapper wrapper;

private void Awake()
{
    
    File.Create(Application.streamingAssetsPath + "/JSON/HELLOFILE.json");
    File.WriteAllText(Application.streamingAssetsPath + "/JSON/HELLOFILE.json", "hello");
    itemContainerPrefab = Resources.Load<GameObject>("Prefabs/Item/ItemContainer");
    dataPath = Application.streamingAssetsPath + "/JSON/ItemData.json";
    jsonData = File.ReadAllText(dataPath);
    wrapper = JsonUtility.FromJson<JSONDataWrapper>(jsonData);
    wrapper.Initialize();
    BuildDatabase();
   
}

Any suggestions on why the first line is completed in such a strange way?关于为什么第一行以如此奇怪的方式完成的任何建议? the rest of the code wont execute until the file is created and the next line can write to that file.在创建文件并且下一行可以写入该文件之前,代码的 rest 不会执行。 I have tried this with both File.Create and File.CreateText and get the same issue.我已经用 File.Create 和 File.CreateText 尝试过这个并得到同样的问题。 I have also tried doing it in both the Awake and start methods我也尝试在 Awake 和 start 方法中这样做

The file should exist on disk almost immediately.该文件应该几乎立即存在于磁盘上。 If you are saying it does not exist in Unity, then yes that makes sense.如果您说它在 Unity 中不存在,那么是的,这是有道理的。 You would need to call AssetDatabase.Refresh .您需要调用AssetDatabase.Refresh Once called, the assets should appear where you are looking for them.调用后,资产应出现在您要查找的位置。

If the issue is that the file actually does not exist and you are getting an error, what is the error?如果问题是该文件实际上不存在并且您遇到错误,那么错误是什么? Can you post it?你能发帖吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM