Storing a .txt file in a scriptable object?

My level editor saves level layouts in .txt files. During loading screens, it then accesses them from a folder in the project. I really want my game to access them from a scriptable object however, to make loading easier on my fragile mind.

However, I’ve managed to find no information on this topic. It’s almost like no one has ever asked this question before. I’m not even sure if it can be done.

If it can be done, it would be very nice if someone could help me out.

if it can’t, it’d also be nice if someone could direct me in the way of a better solution.

public TextAsset dessert1Level; will allow you to drag in a reference to a text file. Or public TextAsset[] levelFiles; for a list, in the usual way. I’m not sure if the file still needs to be in Resources.

That might be nice for special levels. For “regular” ones, like dungeon3Level4.txt, most people would probably look it up – getLevel(int dungeonNum, int levelNum) would compute the name of the file.

https://unity3d.com/how-to/architect-with-scriptable-objects


Just understanding what serialisable objects are and how they are used will answer your question. Try reading the documentation.

Hi @Meowium99 ,

One way to do this is to create a relatively simple ScriptableObject based class, add a string property to hold the text data (I assume these are not extremely huge files), then add attributes to mark it as serializable as well as a text area (just for ease of use in the editor). The scriptable object would look something like this:

using UnityEngine;

[CreateAssetMenu(menuName = "Level Text File")]
public class LevelTextFile : ScriptableObject
{
    [SerializeField, TextArea]
    public string text;
}

To create a new “LevelTextFile”, go to the folder you want to add it to in your project, right-click to bring up the context menu, use ‘Create’ and then select “Level Text File”. You can create one of these for each of your level text files. Select the object, then paste the contents of your text files into the “text” property for the scriptable object.

Note: while I just stored the object as a plain string, if you still want to keep the text files as assets, but use them from scriptable objects, use “TextAsset” for the “text” property instead of “string”. You would then click the dot in the editor for the object and select a text file asset from your project instead. It’s all up to you.

Now, you can create an empty GameObject which will be your holder for all (or some) of your scripts in a scene. You can then attach a simple script to the game object, such as:

using UnityEngine;

public class TestScript : MonoBehaviour
{
    public LevelTextFile level1;

    void Start()
    {
        if (level1 != null)
        {
            Debug.Log(level1.textFile);
        }
    }
}

Of course, you can more properties or an array of these LevelTextFile objects, season to taste.