Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[RELEASED] File Management: Easy way to save and read files.

Discussion in 'Assets and Asset Store' started by jemonsuarez, Jul 29, 2016.

  1. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,075
    Thanks. That should do it nicely. Great asset BTW.
     
    jemonsuarez likes this.
  2. doq

    doq

    Joined:
    Aug 17, 2015
    Posts:
    121
    @jemonsuarez Hi I just installed 2017.3.1.p3 and I'm running into a WWW.error Unknown Error in FileManagement.cs:633

    Did something change in the Unity API? I can't load my audio files now.
     
  3. doq

    doq

    Joined:
    Aug 17, 2015
    Posts:
    121
    I see in the docs

    https://docs.unity3d.com/ScriptReference/WWW.html
    The code needs an extra slash, but it looks like you need to detect if you're on windows first.
     
  4. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hey, thanks for the info, I'll take a look at the documentation to fix that.
    Best regards.
     
    doq likes this.
  5. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    New FileManagement feature (V1.7):
    Load INI files, modify them, and then save to disk.

    There is a new class called FM_IniFile with several useful interfaces to read, modify and add values to an INI file. Very useful to read and save settings.

    It also allows to save multiple values (with labels) in a single file to be able to export to another application.

    When saves the INI file content, the class formats the file structure so the INI file is always nice and clean. You can also sort the labels and sections alphabetically.

    There are also another powerful feature: To merge INI files. It's done automatically and updates existing values.

    For more information, here is the documentation.
     
  6. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    431
    does it works with Unity 2018?
     
  7. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Andreas12345 likes this.
  8. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    431
    in the Docu. stand "need to rename PlayerPrefs with FileManagement."
    i use already PlayerPrefs in my project, need i to change all items to FileManagement?
     
  9. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi @Andreas12345 you can simple replace PlayerPrefs with FileManagement and it will work the same way. You'll not need to PlayerPrefs.Save() but the use of this method just does nothing, so you can leave it if you prefer.

    Please note that FileManagement will NOT migrate your ancient PlayerPrefs stored data into FileManagement save data, you have to do it yourself.
    Here is an example:
    Code (CSharp):
    1.         // Checks if the key exists in PlayerPrefs but not in disk:
    2.         if (!FileManagement.HasKey("MyString") && PlayerPrefs.HasKey("MyString"))
    3.         {
    4.             string val = PlayerPrefs.GetString("MyString"); // Read the PlayerPrefs key.
    5.             FileManagement.SetString("MyString", val);      // Save the key to disk.
    6.             PlayerPrefs.DeleteKey("MyString");              // Delete the PlayerPrefs key.
    7.         }
    8.  
    The example checks for a string value and migrates it to a single file using FileManagement. You have to do this for evey key of your game, if you have to restore some saved data (as with updated may occur).

    If you need multiple values into a single file you can use the INI file class, included with FileManagement.
    Just drop a line if you need some more information.
    Best regards.
     
    Andreas12345 likes this.
  10. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    431
    sounds good, i ll will buy it. Looks promising. Thanks for the explain
     
    jemonsuarez likes this.
  11. Peter_Apogee

    Peter_Apogee

    Joined:
    Mar 26, 2018
    Posts:
    3
    Hi, does anyone know how to create a new ini file?
    Here is my code that creates empty ini file but cant read it.
    What am I doing wrong?
    I am getting this warning msg:
    [FM_IniFile.LoadNewIniFile] The requested INI file wouldn't be parsed.

    Code (CSharp):
    1. [Header("userData.ini:")]
    2.     [SerializeField] string path;
    3.     [SerializeField] string pathFile;
    4.     [SerializeField] bool enc = false;
    5.     [SerializeField] bool checkSA = false;
    6.     [SerializeField] bool fullPath = true;
    7.  
    8.     public void SetPathAndIniFile()
    9.     {
    10.         path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    11.         path += @"\Game Data";
    12.  
    13.         // Create Directory
    14.         if (!FileManagement.DirectoryExists(path, false, true))
    15.         {
    16.             FileManagement.CreateDirectory(path, true);
    17.         }
    18.  
    19.         pathFile = path + @"\userData.ini";
    20.        
    21.         // Create ini file
    22.         if (!FileManagement.FileExists(pathFile, checkSA, fullPath))
    23.         {
    24.             FileManagement.SaveFile(pathFile, "", enc, fullPath);
    25.         }
    26.     }
    27.  
    28.     public void SetUserData()
    29.     {
    30.         iniFile.LoadNewIniFile(pathFile, enc, checkSA, fullPath);
    31.        
    32.         // Update ini file
    33.  
    34.         iniFile.Save(pathFile, true, enc, fullPath);
    35.     }
     
  12. Peter_Apogee

    Peter_Apogee

    Joined:
    Mar 26, 2018
    Posts:
    3
    I figured it out. Line 24 should be like this:
    Code (CSharp):
    1. FileManagement.SaveFile(pathFile, "[section] key = value", enc, fullPath);
    It just needs at least one section/key pair to be recognised as ini file.
    Now I can write and read this ini file without any problems.
     
  13. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi @Peter_Apogee your example is saving an empty file, please note that the message you get is "The requested INI file wouldn't be parsed." instead of "File not exists".
    I'll include here the way the INI file is intended to be used in FileManagement:

    Code (CSharp):
    1.             // Create your INI file in memory:
    2.             FM_IniFile iniFile = new FM_IniFile("MyCfg.ini");   // This line loads an existing file or creates a new one automatically.
    3.  
    4.             // Add some data:
    5.             iniFile.AddKey("key1", "value1", "section");        // Sets a string.
    6.             iniFile.AddKey("key2", "value2", "section");        // Sets a string.
    7.  
    8.             // Modify that data:
    9.             iniFile.SetKey("key2", 25, "section");              // Modifies value from string to int.
    10.  
    11.             // Save the updated INI file:
    12.             iniFile.Save("MyCfg.ini");                          // Every modification is in memory until you save the INI file.
    13.  
    When you Save() the INI file, it will be automatically parsed and structured, you don't have to do it manually.
    There are some other options as remove keys, remove sections, sort alphabetically, merge INI files, etc.

    Hope it helps to improve your product, and please let me know if you need more information.
     
  14. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Dear Sir

    I am going to buy your asset, but I have to be sure that it is possible to save data into any folder in PC by using your asset.

    As you know
    https://docs.unity3d.com/ScriptReference/Application-dataPath.html

    By using Application.data, we can save the data into existing file which is created by programmer before program runs.
    But, I want to give a choice to end-user should select data file address in order to save the data in PC after program runs.





     
  15. Peter_Apogee

    Peter_Apogee

    Joined:
    Mar 26, 2018
    Posts:
    3
    I see. It's clear to me now, thank you. I didnt realize that new FM_IniFile("MyCfg.ini"); will create new file if there is none.
     
  16. Spookytooth3D

    Spookytooth3D

    Joined:
    Oct 31, 2013
    Posts:
    73
    I'm trying to get this line to work, and don't have a clear example to follow.
    Maybe someone else has an idea

    Here's the Code:
    Code (CSharp):
    1.  public void OpenFileBrowser()
    2.     {
    3.         GameObject browserInstance = GameObject.Instantiate(fileBrowser);
    4.         browserInstance.GetComponent<FileBrowser>().SetBrowserWindow(OnPathSelected);
    5.  
    6.         string[] filter = { ".txt"};
    7.         browserInstance.GetComponent<FileBrowser>().SetBrowserWindowFilter(filter);
    8.     }
    9.  
    10.     void OnPathSelected(string path)
    11.     {
    12.         string[] myLoadedCams = FileManagement.ReadAllLines(path, false, false, false);
    13.         Debug.Log(myLoadedCams[0]);
    14.     }
    The File Browser opens just fine, and Destroys as expected once I've chosen the file I want to read in, which is a string array.

    Here's the error I'm getting when I read the file back in...

    ArgumentException: Toggle null is not part of ToggleGroup Content (UnityEngine.UI.ToggleGroup)
    UnityEngine.UI.ToggleGroup.ValidateToggleIsInGroup (UnityEngine.UI.Toggle toggle) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/ToggleGroup.cs:47)
    UnityEngine.UI.ToggleGroup.NotifyToggleOn (UnityEngine.UI.Toggle toggle, Boolean sendCallback) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/ToggleGroup.cs:56)
    UnityEngine.UI.ToggleGroup.EnsureValidState () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/ToggleGroup.cs:99)
    UnityEngine.UI.Toggle.OnDestroy () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:144)
     
  17. Spookytooth3D

    Spookytooth3D

    Joined:
    Oct 31, 2013
    Posts:
    73
    This would be great! Keep me posted!
     
  18. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi @Spookytooth3D, to fix the problem with the Toggle groups you need an update, please send me an email to jmonsuarez@gmail.com regarding this issue and I'll send you back the new-unreleased FileManagement package.
    Best regards.
     
  19. Spookytooth3D

    Spookytooth3D

    Joined:
    Oct 31, 2013
    Posts:
    73
    I have also copied you on it Javier! Thank you! - Michael
     
  20. Spookytooth3D

    Spookytooth3D

    Joined:
    Oct 31, 2013
    Posts:
    73
    Did you receive my email?
     
  21. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hope the new package worked properly. This new package includes also an improvement in Android, that avoids using the WWW class to get files from the StreamingAssets folder. It's thread safe now.
    Best regards.
     
    Spookytooth3D likes this.
  22. Arakjin

    Arakjin

    Joined:
    Nov 12, 2017
    Posts:
    10
    I sent email before finding this thread, anyway. can it save hierarchy and UI elements?
    Does the objects to be saved need to be serializable by unity itself?
     
    Last edited: Jan 4, 2020
  23. unity_j4UGtNK5J9W0rw

    unity_j4UGtNK5J9W0rw

    Joined:
    Aug 24, 2020
    Posts:
    2
    I just purchased the app and am trying to use it on Android. It runs fine inside Unity but once compiled for Android using standard settings I'm afraid I get an error message right away. That error message is included below:

    {"tid":1,"div":"RequestEventHelper","msg":"HandleRequestStateChange Finished","ex": [{"msg": "java.io.FileNotFoundException: FMSA_Index", "stack": "java.io.FileNotFoundException: FMSA_Index\n\tat android.content.res.AssetManager.nativeOpenAsset(Native Method)\n\tat android.content.res.AssetManager.open(AssetManager.java:747)\n\tat android.content.res.AssetManager.open(AssetManager.java:724)\n\tat com.unity3d.player.UnityPlayer.nativeRender(Native Method)\n\tat com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)\n\tat com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95)\n\tat android.os.Handler.dispatchMessage(Handler.java:102)\n\tat android.os.Looper.loop(Looper.java:215)\n\tat com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)\n  at UnityEngine.AndroidJNISafe.CheckException () [0x0008d] in \/Users\/bokken\/buildslave\/unity\/build\/Modules\/AndroidJNI\/AndroidJNISafe.cs:24 \n  at UnityEngine.AndroidJNISafe.CallObjectMethod (System.IntPtr obj, System.IntPtr methodID, UnityEngine.j


    The file it references "FMSA_Index" is not anything from my project so I'm assuming it must have to do with Unity / Android. Can anyone offer any advice on what I've done wrong?

    Thanks!
     
  24. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi @unity_j4UGtNK5J9W0rw the "FMSA_Index" file is an automated index file generated by FileManagement package (please check the documentation).

    So to fix that issue there must be a "StreamingAssets" folder in the "Assets" folder of your project, and don't delete the "StreamingAssetsIndexer.cs" script, who generates this index (used on Android and WebGL exports). No matters if the StreamingAssets folder is empty.
    Check the StramingAssets folder to verify the file existence (FMSA_Index) and then export.
    If you delete this file, FileManagement (and then FTS) will not be able to find embedded files.

    Please let me know if you managed to fix the issue, and send me an email to jmonsuarez@gmail.com.
    Best regards.
     
  25. unity_j4UGtNK5J9W0rw

    unity_j4UGtNK5J9W0rw

    Joined:
    Aug 24, 2020
    Posts:
    2
    Thanks for the pointer. I did manage to fix the issue but it turns out that it does seem to require that there be *something* in the StreamingAssets folder. In my case I just created a folder called 'junk' and that was all it needed to move past the error on Android.

    I have another issue now though. I'm able to save data without issue. In my case I am downloading some mp3 content from the web and storing it locally so it can be pulled up in the app. These assets are very dynamic so I have to be able to load them at runtime.

    Now that I have them saved I need to load them into audio clips. Unfortunately these *must* stay as mp3 and ogg files. I have pre-recorded literally 100's of thousands of them. I tried using the Application.persistentDataPath but it turns out that on my mac, when running the unity editor - that path isn't correct.

    In my case it says the path will be:
    /Users/scott/Library/Application Support/ivu/IVU Base/music/db/myAudioClip.mp3

    That doesn't turn out to be true. When I search the drive they are actually located here:
    /Users/⁨scott/⁨unity/⁨IVU Base/⁨Users/⁨scott/⁨Library/⁨Application Support/⁨ivu/⁨IVU Base⁩/⁨music/db/myAudioClip.mp3

    Any idea why there would be such a strange discrepancy?⁩

    Thanks in advance!
    Scott
     
  26. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    This is weird, the StreamingAssets folder can be empty, indeed the StreamingAssets folder is created automatically by the "StreamingAssetsIndexer.cs" script. Are you using an outdated version of FM?

    Here the path added on top the the original.
    Original:
    /Users/scott/Library/Application Support/ivu/IVU Base/music/db/myAudioClip.mp3
    /Users/⁨scott/⁨unity/⁨IVU Base/ (*) ⁨Users/⁨scott/⁨Library/⁨Application Support/⁨ivu/⁨IVU Base⁩/⁨music/db/myAudioClip.mp3

    Are you using the automated PersistentData path? or are you forcing a fixed path?
    Try printing the FileManagement.PersistentDataPath on both situations and check differences.

    Please send me an email to jmonsuarez@gmail.com or I'm not being able to give you a good support. I can't send you any possible fix updates through this mean.

    Best regards.
     
  27. sebaFP

    sebaFP

    Joined:
    Jun 5, 2020
    Posts:
    2
    Hi,
    I red the documentation but i want to be 100% sure, In the webgl final build, if I add files in the streamingassets folder, Can´t I access to the new files throught the file manager?
    Thanks!
     
  28. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi @sebaFP here you have a WebGL build of the test application included with FileManagement.

    In screen 6 of this application you can see how those images are downloaded in real time from the "StreamingAssets" folder created in the final build in server side (You can verify the server requests from the developer console of your web browser -> F12).
    The URLs you'll see are:
    http://etoilestudio.free.fr/portfolio/fm/StreamingAssets/ex1.jpg
    http://etoilestudio.free.fr/portfolio/fm/StreamingAssets/ex2.png

    If you open the FileBrowser in screen 8 you'll see both images listed as existing locally but they are not, this is done to give a mean to navigate this remote content (the content is not read dynamically, there is a prebuilt index to simulate the navigation).

    Please check the documentation to know how FileManagement merges both the PersistentData and StreamingAssets folders.
    Both folders are accessed as one, and StreamingAssets is treated as Read-Only to grant compatibility across all supported platforms. Please note that if you write a file with the same name as an already existing one in StreamingAssets, it will be not overwritten but overridden, so if you delete the newly created file the old one will be seen again.

    Please don't hesitate on send me an email to jmonsuarez@gmail.com if you need help.
    Best regards.
     
  29. frangagn

    frangagn

    Joined:
    Sep 20, 2018
    Posts:
    44
    I'm trying to use the FileManagement to save a file, tried many different way and still am getting an access error. Something very simple like:

    filename = "g:/mydata.txt"
    filename = FileManagement.NormalizePath(filename);
    FileManagement.SaveFile<List<ImgItem>>(filename, AllImgItem, false, true);

    and I keep getting the following error:

    UnauthorizedAccessException: Access to the path "g:/" is denied.
    System.IO.Directory.CreateDirectoriesInternal (System.String path) (at <437ba245d8404784b9fbab9b439ac908>:0)
    System.IO.Directory.CreateDirectory (System.String path) (at <437ba245d8404784b9fbab9b439ac908>:0)
    FileManagement.CreateDirectory (System.String name, System.Boolean fullPath) (at Assets/eToile/FileManagement/FileManagement.cs:909)
    FileManagement.SaveRawFile (System.String name, System.Byte[] content, System.Boolean enc, System.Boolean fullPath) (at Assets/eToile/FileManagement/FileManagement.cs:458)
    FileManagement.SaveFile[T] (System.String name, T content, System.Boolean enc, System.Boolean fullPath) (at Assets/eToile/FileManagement/FileManagement.cs:699)

    I am using FileManagement on Window right now and have no problem saving that same data using the WPF library, however I intend to port my project to Unix and need a more generic way to do it.

    What am I doing wrong? :(
     
  30. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi @frangagn, at first glance it looks like you are not doing nothing wrong, just that Unix platforms doesn't use drive letters (it's a Windows only feature).

    You said that you are experiencing the issue under Unix platform, which should behave similar to Linux and all Mono methods should run normally. So you should list your available partitions, then chose the right one and write there using FileManagement. Try listing drives using this (I've not tested, but I will) answer.

    You can also check your available paths manually using the FileBrowser (if you are using Unity, if course) there you can see the full paths and check the G:/ existence (I'm pretty sure it doesn't exists).

    Please let me know your results. You can send me an email to jmonsuarez@gmail.com
    Best regards.
     
  31. frangagn

    frangagn

    Joined:
    Sep 20, 2018
    Posts:
    44
    Thank you for your answer. As mentioned in my initial post, I am using Windows right now, but will eventually want to use a Unix based platform (VR headsets)

    So on my windows computer right now, I DO have a G: drive. The disk exists. When using the WPF library within Unity, I do access and write to the G: drive without problem. The file g:\mydata.txt does not exist before I run the code and I can create it with WPF without error. I was hoping to use FileManagement to have a generic solution to file creation.

    I have tried using other drives with the same code and other file names, I get the same error. So basically it does not work....

    Is there an example somewhere of a piece code that use FileManagement to create and read a file? Even a text file would be good. Something I could copy and test on my system.
     
  32. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi @frangagn, when you say WPF you mean UWP? Because I'm not sure if WPF is supported in Unity.
    Anyways, it looks like I have to check closely the UWP support because there are a few thing that are not working anymore.

    By the way, do not expect to UWP to work the same way as standalone applications does, it's far more restrictive, just like in mobile applications. So maybe there is your problem, probably you have to provide (or prompt the user, I don't know) the permissions.

    It looks like your application works well when running from the editor, because it's standalone, then when exporting to UWP it doesn't works anymore (restriction). It works for sure in Unix because it's also standalone (Android has it's restrictions, but it also works).

    UWP use it's own set of base methods (WebGL too) so the issue is under UWP only for sure.
    Thanks for the feedback, i'll check this out and will return to you ASAP.
    Best regards.
     
  33. frangagn

    frangagn

    Joined:
    Sep 20, 2018
    Posts:
    44
    I mean WPF. I'm developing using SteamVR and there is no problem using WPF routines, and it's actually better since the UWP (which I used in other projects) is a real pain with permissions.... I'm currently using WPF and was looking to converting to FIleManagement to extend compatibility to other environments.

    When using FileManagement, the application is not working both in the editor and stand alone.

    Is FileManagment using UWP behind the scene?

    Is this the right place to get support?
     
  34. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Hi, this is the right place to get support (I prefer an email, but I check this thread too).

    I'm sorry if you feel as I'm not trying to help, but I had to make sure we are talking about the same thing (I receive dozens of emails of unexperienced developers asking the wrong questions, so I try to guide always).

    As you may have guessed, WPF is not supported, only UWP, so there is the reason it's not working as you expect.

    But I'm interested in extending the FileManagement compatibility level, so can you please answer this question from your experience to save me some research?
    Is the WPF platform widely supported across all Windows devices being: Windows phone, store-desktop and XBox?
    If so, then it's clear that I should migrate FileManagement from UWP to WPF.

    Thanks in advance.
     
  35. frangagn

    frangagn

    Joined:
    Sep 20, 2018
    Posts:
    44
    You are right, WPF is an old platform and it's not exactly "kasher" to use it with Windows Mixed Reality. However it works, and if you use UWP, things don't work. I'm writing a software that needs to read many files (videos, photos) outside the application's folder, and the "BroadFileAccess" does not work. And even if it did work, the users have to allow specifically and manually the application to access these files.... It's a mess. Even Microsoft agrees UWP is not the future of Windows: https://www.thurrott.com/dev/221688/microsoft-explains-the-future-of-the-windows-10-app-platform.

    I am sorry your system works with UWP because it would impose all kinds of limitation on my project which will render it useless. If you were to port it to WPF, please let me know. You will find that very easy, so much easier than UWP...
     
  36. jemonsuarez

    jemonsuarez

    Joined:
    Sep 24, 2012
    Posts:
    131
    Yes, I would like to migrate if that doesn’t means losing some other compatible platforms.
    To reproduce your particular situation I need you to provide me some information:
    1. What’s the Unity version you are using?
    2. What’s the platform you are exporting to? (The one in Build Settings)
    3. What’s the method you know for sure it’s working under WPF to save a file.
    You can send me an email to jmonsuarez@gmail.com, if you prefer, so I can send you some fixed source code eventually.
    Thanks in advance.
     
unityunity