Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 obsidianz · Apr 03, 2021 at 09:42 PM · resourcesfilesfilesystem

How to make a File System?

I'm making a game inside of a simulated computer. I've made the basis for the UI and now it's time to make the File System. I need a system that allows files to be moved at runtime. I had a few ideas on how to implement this, but they all had flaws:


First, I thought about using the Resources folder itself as the File System but there's no way to move files around from script (and even if there was, after building this folder wouldn't exist so it wouldn't work there).


Then I thought about creating the File System on a MonoBehaviour and attaching it to a gameObject that I would then save as a prefab. The problem here is that there's no type that can represent every type of file (TextAsset, Sprite, AudioClip, etc.). I created an abstract file class and then create a subclass for each type of file but by doing this I lose the ability to see the file variables in the inspector. To load this class I use the Resources.LoadAll method for each type of asset I want to keep.


 [System.Serializable]
 public class FileSystem
 {
     [SerializeField]
     public List<File> files;
 
     public FileSystem()
     {
         files = new List<File>();
     }
 }
 
 [System.Serializable]
 public abstract class File
 {
     public File(string name)
     {
         this.name = name;
     }
 
     public string name;
 }
 
 [System.Serializable]
 public class TextFile : File
 {
     public TextFile(string name, TextAsset asset) : base(name)
     {
         this.asset = asset;
     }
 
     public TextAsset asset;
 }
 
 [System.Serializable]
 public class ImageFile : File
 {
     public ImageFile(string name, Sprite asset) : base(name)
     {
         this.asset = asset;
     }
 
     public Sprite asset;
 }



Is there a better way of doing this that I'm missing?

Comment
Add comment
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

1 Reply

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

Answer by obsidianz · Apr 04, 2021 at 08:05 PM

For anyone interested, this is what I ended up using:

 [System.Serializable]
 public class OSFileSystem
 {
     [SerializeField] private string homePath;
     [SerializeField] private List<OSFolder> folders;
 }
 
 [System.Serializable]
 public class OSFolder : IComparable<OSFolder>
 {
     public string folderPath;
     public List<UnityEngine.Object> files;
     public int subfolderIdx = -1;
 }



I use a big list of Folders for the file system. For the Folders, I just sticked with the UnityEngine.Object class, and at the end it isn't that bad.

I still need to construct every folder manually, but with some custom unity editors and a validation/sorting method it gets easier to work with.

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

114 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

Related Questions

Include a file to build - and keep it editable 0 Answers

How do I get a list of filenames in a Resources folder? 6 Answers

Loading Resources folder contents in StandAlone Player 1 Answer

Restructuring Project without Breaking Everything? 1 Answer

Saving Level data to a file 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