Search Unity

Is there a way to save files to the hard disk without meta files ?

Discussion in 'Scripting' started by SharonL75, Oct 2, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    upload_2020-10-2_15-11-8.png

    Each text file have also a meta file. Is it must or needed the meta files and if not how can I avoid from being creating them ?

    This is how I'm saving to the hard disk :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEngine;
    6.  
    7. public static class SaveSystem
    8. {
    9.     private static readonly string SAVE_FOLDER = Application.dataPath + "/save_";
    10.     public static void Init()
    11.     {
    12.         if (!Directory.Exists(SAVE_FOLDER))
    13.         {
    14.             Directory.CreateDirectory(SAVE_FOLDER);
    15.         }
    16.     }
    17.  
    18.     public static void Save(string saveString)
    19.     {
    20.         int saveNumber = 1;
    21.         if (!File.Exists(SAVE_FOLDER + "/" + saveNumber + ".txt"))
    22.         {
    23.             File.WriteAllText(SAVE_FOLDER + "/" + saveNumber + ".txt", saveString);
    24.         }
    25.         while (File.Exists(SAVE_FOLDER + "/" + saveNumber + ".txt"))
    26.         {
    27.             saveNumber++;
    28.         }
    29.         File.WriteAllText(SAVE_FOLDER + "/" + saveNumber + ".txt", saveString);
    30.     }
    31.  
    32.     public static string LoadSingleRecentFile()
    33.     {
    34.         DirectoryInfo directoryInfo = new DirectoryInfo(SAVE_FOLDER);
    35.         FileInfo[] saveFiles = directoryInfo.GetFiles("*.txt");
    36.         FileInfo mostRecentFile = null;
    37.         foreach (FileInfo fileInfo in saveFiles)
    38.         {
    39.             if (mostRecentFile == null)
    40.             {
    41.                 mostRecentFile = fileInfo;
    42.             }
    43.             else
    44.             {
    45.                 if (fileInfo.LastWriteTime > mostRecentFile.LastWriteTime)
    46.                 {
    47.                     mostRecentFile = fileInfo;
    48.                 }
    49.             }
    50.         }
    51.  
    52.         if (mostRecentFile != null)
    53.         {
    54.             string saveString = File.ReadAllText(mostRecentFile.FullName);
    55.             return saveString;
    56.         }
    57.         else
    58.         {
    59.             return null;
    60.         }
    61.     }
    62.  
    63.     public static List<string> LoadMultipleFiles()
    64.     {
    65.         DirectoryInfo directoryInfo = new DirectoryInfo(SAVE_FOLDER);
    66.         FileInfo[] saveFiles = directoryInfo.GetFiles("*.txt");
    67.         List<string> savedFiles = new List<string>();
    68.         foreach (FileInfo fileInfo in saveFiles)
    69.         {
    70.             string savedFile = File.ReadAllText(fileInfo.FullName);
    71.             if(savedFile != null)
    72.             {
    73.                 savedFiles.Add(savedFile);
    74.             }
    75.         }
    76.  
    77.         return savedFiles;
    78.     }
    79. }
    80.  
    In another script I'm using jason :

    Code (csharp):
    1.  
    2. string saveString = SaveSystem.LoadSingleRecentFile();
    3.  
    4.         if (saveString != null)
    5.         {
    6.             SaveObject saveObject = JsonUtility.FromJson<SaveObject>(saveString);
    7.  
    But the saving it self is in the first script.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    To my knowledge, you are not the one creating these files. Unity is. They are used to store import setting and so on. If you delete them, Unity probably just generates a new one with default settings. You should be able to just ignore them completely. Why do you want to get rid of them?
     
    Joe-Censored likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Meta files are entirely a Unity construct. As Yoreki noted, you can mostly just ignore them.

    HOWEVER, if you are using source control (you ARE using source control right???), then make sure they get committed. They tell Unity how to import each given asset, how you want to treat it, basically all the details you see in the inspector panel (also known as the "importer" when viewing an asset).
     
    Joe-Censored and Brathnann like this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't understand why you want your save system writing into the project's Assets folder. Files created in the Assets folder get .meta files as soon as the editor notices them, because the editor thinks they are part of the project needing to be imported. If that is not the case (which I'm pretty sure), then write your save files somewhere outside of the Assets folder.

    Your use of Application.dataPath is why you're writing to the Assets folder in editor.
    https://docs.unity3d.com/ScriptReference/Application-dataPath.html
     
    PraetorBlue and Yoreki like this.