Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Meowium99 · Jan 18, 2020 at 04:40 PM · scriptableobjecttxt

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mscardinal · Jan 18, 2020 at 06:17 PM 0
Share

Why do you want to use a scriptable object i dont see the point of it?

avatar image Meowium99 mscardinal · Jan 19, 2020 at 08:00 AM 0
Share

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.

avatar image mscardinal · Jan 19, 2020 at 10:41 AM 0
Share

I fear I cant help you with that. Sorry...

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges