简体   繁体   中英

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. The file was previously showing in the chosen location on the SSD just not in unity.*

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. 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. 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. I have tried this with both File.Create and File.CreateText and get the same issue. I have also tried doing it in both the Awake and start methods

The file should exist on disk almost immediately. If you are saying it does not exist in Unity, then yes that makes sense. You would need to call 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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