I am at the end of project and being roadblock by one frustrating problem. I am just learning that unity cannot build with the UnityEditor namespace.
I am trying to write a text file to the resources folder, then set that text file to a TextAsset GameObject so I can use it later. Unfortunately without AssetDatabase.Refresh() the TextAsset is always null and won’t be found until the program is restarted.
Oddly, I have done this same process with a PNG and it works seamlessly. I can use System.IO to write and encode a PNG, then set it to a texture right away using Resources.Load();.
Why would it work for a PNG file, and not a TXT file? The print statement on line 17 even returns true.
Thanks so much for your help!
using UnityEngine;
using System.IO;
//using UnityEditor;
public class TextHandler : MonoBehaviour
{
public TextAsset textFile;
public string resourcesPath;
void Start ()
{
resourcesPath = Application.dataPath + "/Resources/";
}
public void WriteTextFile(string NPCName, string NPCType, string NPCDialogue, string NPCItems)
{
System.IO.File.WriteAllText(resourcesPath + NPCName + "_" + NPCType + ".txt" , NPCDialogue + "~" + NPCItems + ".txt");
//AssetDatabase.Refresh();
print(File.Exists(resourcesPath + NPCName + "_" + NPCType + ".txt"));
textFile = Resources.Load(NPCName + "_" + NPCType) as TextAsset;
}
}