How I use text files in unity

Efficiently manage Unity data arrays using text files for easy editing and sharing without requiring Unity installation.

Video Information

Title: How I use text files in unity

Using TextAssets allows Unity to directly import data from text files.

Text files are easy to understand, share, and edit without requiring Unity knowledge.

The `OnValidate` function can be used to automatically update data in the Unity editor when a TextAsset is modified.

Comma-separated values (CSV) within text files can be parsed to represent more complex data structures, like classes with multiple properties.

A custom editor script (mentioned but not detailed) can be created to automatically generate buttons for each text file, simplifying data selection.

Remote text file storage (e.g., GitHub, Pastebin, Google Sheets) can further streamline workflows by enabling collaborative editing.

Related Videos

so in today's video we're going to talk all about data there was a young lady from venus let's say you wanted an array of names for characters well there's a good chance at the moment you're probably doing it something like this [Music] well what if i told you there was a better way so let's ask ourselves first why this approach not really ideal well for starters it's very time consuming let's be honest editing each entry in that array absolute pain in the ass next whoever's doing the editing they have to use unity you can't have a non-unity person fill in the details for those names i need to install unity unity just to edit that

array so how can we fix this well that's easy text files if we instead store our data in just plain text files they're easy to understand easy to share and you don't need special tools set at them in fact actually did you know that unity has direct support for text files we can add something called a text asset it's going to show up in the inspector and let us drag text files into it next we want to respond to changes to that asset so to do that we'll create an unvalidate function any code we write in here will get called whenever the editor values change what we want to do is whenever a new file

is dropped in we'll split it into lines and then assign each of those lines into the array and if the file is null we'll just clear out the array using an online name generator i made a number of text files that are just lists of names now whenever i drag one of those files into our script unity will automatically pick it up read its contents populate the array it makes it super easy to send text to a designer have them change values and then just swap out the file if we want to have multiple sets of data or go back to previous versions it's as easy as picking a different text file so you might

be thinking yeah that's all good and well if it's the list of strings what about more complicated data like a list of items or weapons with multiple values to them okay that's a fair point but uh let's give it a try let's make ourselves an item class we'll have a string for a name in for a price and we'll we'll add a color why not we can't possibly store that in a text file right well actually it's super easy barely an inconvenience wow wow whenever we read in the lines we just do one extra step and split them by the commas parsing out the values now just to be clear i am aware this is

not currently super robust in a real project i'd be validating the input fields but validation is a whole other topic that deserves its own video we now have lists of things loaded in from files what next well it's out of scope for this video custom editor scripts those ones will read any files that you've labeled and generate buttons for them that way your custom editor will automatically show you a list of all options available and populate the array with the click of a button so our new text loading system is pretty useful but there's one remaining hassle with this approach in order for a designer to edit this you still have to take the text

file out of unity send to them and then replace it back when they give it back to you it's not a big deal but we would like to avoid doing this if you watch my previous video on loading manifest files from the web that should give you an idea about how you could change this up to have the text file stored online you could put them up in somewhere like github paste bin or google sheets and with that we're done for this one if you found that informative please like and subscribe and if you want to see me cover the editor extension stuff in more detail drop a comment down below if you want you

can also donate to the coffee and get access to the discord okay good luck see you on the next one