- Home /
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.
Why do you want to use a scriptable object i dont see the point of it?
The main reason why is because I'm loading level data into an empty scene, actually constructing it at run time. Having a simple way of slipping level info in, without having to worry about file names or the like seems like a good idea. The other reason is because I'm also storing other info in it, such as the location of certain objects in that scene, starting locations and what music is supposed to play. Having all of that data stored in one container also seems like a good idea.
Answer by Owen-Reynolds · Jan 19, 2020 at 06:24 PM
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.
Answer by sacredgeometry · Jan 18, 2020 at 06:26 PM
https://unity3d.com/how-to/architect-with-scriptable-objects https://docs.unity3d.com/Manual/script-Serialization.html https://docs.unity3d.com/ScriptReference/Serializable.html
Just understanding what serialisable objects are and how they are used will answer your question. Try reading the documentation.
Answer by JDelekto · Jan 19, 2020 at 05:37 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612221737im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D: How to keep player within boundary and more in the picture below 2 Answers
Confused about custom GameObjects,Custom GameObject confusion 0 Answers
Variable will not change? 1 Answer
Unique function for each instance of a scriptable object 2 Answers
How do you dynamically create serialized fields in the inspector? 1 Answer