I'm trying to use the following code to read a text file stored in the Resources folder or in a file on my computer, and instantiate objects in a Unity 3D scene accordingly.
Can you tell us anything about the type of the file? If it's a text or binary file you want to read as strings or bytes, it might be best to set it up as a TextAsset, so you can store a reference to it the same way you reference Unity-native assets, and it's properly tracked in build dependencies. For more complex use cases, you can write your own importer to import it as an asset.
I have a Unity C# Script which updates a variable each time the Update function is called. I would like to know how to write to a file from the update function.
The File.ReadAllLines () version works on the path to the txt file, the string.Split () version works on the TextAsset.text and the TextAsset is passed through the inspector as reference.
It has predefined content importers for most standard audio and image formats that will import the file, process it into an object, and then write the object into a binary .xnb file. At runtime, when you use a ContentManager to load your content, the binary .xnb file gets read and converted into your output type (such as Texture2D or SoundEffect).
I have successfully read in and written level data using .dat files on unity PC builds, but compiling for android stops me from being able to read in information from the resources folder.
At this moment I'm saving a file that is set to a specific path. c:\test\test.exe. Now I know that Android and IOS don't have such directory. I was wondering if it was possible to save the file to a relative path? If not I might have to adapt the code that checks on what kind of device the app is running psuedocode:
The way I would do that in normal C# is the File.ReadAllLines (path) function in the System.IO namespace. But, I don't think that I can use System.IO with Unity so the alternative that I could come up with is the TextAsset class which I can use to reference an imported text asset and get its text property as a string.
Basically set "Content" and "Copy if newer". The exception you are getting, MethodAccessException, is because that API isn't available on WP7. Use Title Storage instead. To open your file, you can use: TitleContainer.OpenStream(filename); See the information on MSDN about Reading Game Data from Title Storage and Adding Game Data Files to Title ...