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

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 :

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public static class SaveSystem
{
    private static readonly string SAVE_FOLDER = Application.dataPath + "/save_";
    public static void Init()
    {
        if (!Directory.Exists(SAVE_FOLDER))
        {
            Directory.CreateDirectory(SAVE_FOLDER);
        }
    }

    public static void Save(string saveString)
    {
        int saveNumber = 1;
        if (!File.Exists(SAVE_FOLDER + "/" + saveNumber + ".txt"))
        {
            File.WriteAllText(SAVE_FOLDER + "/" + saveNumber + ".txt", saveString);
        }
        while (File.Exists(SAVE_FOLDER + "/" + saveNumber + ".txt"))
        {
            saveNumber++;
        }
        File.WriteAllText(SAVE_FOLDER + "/" + saveNumber + ".txt", saveString);
    }

    public static string LoadSingleRecentFile()
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(SAVE_FOLDER);
        FileInfo[] saveFiles = directoryInfo.GetFiles("*.txt");
        FileInfo mostRecentFile = null;
        foreach (FileInfo fileInfo in saveFiles)
        {
            if (mostRecentFile == null)
            {
                mostRecentFile = fileInfo;
            }
            else
            {
                if (fileInfo.LastWriteTime > mostRecentFile.LastWriteTime)
                {
                    mostRecentFile = fileInfo;
                }
            }
        }

        if (mostRecentFile != null)
        {
            string saveString = File.ReadAllText(mostRecentFile.FullName);
            return saveString;
        }
        else
        {
            return null;
        }
    }

    public static List<string> LoadMultipleFiles()
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(SAVE_FOLDER);
        FileInfo[] saveFiles = directoryInfo.GetFiles("*.txt");
        List<string> savedFiles = new List<string>();
        foreach (FileInfo fileInfo in saveFiles)
        {
            string savedFile = File.ReadAllText(fileInfo.FullName);
            if(savedFile != null)
            {
                savedFiles.Add(savedFile);
            }
        }

        return savedFiles;
    }
}

In another script I’m using jason :

string saveString = SaveSystem.LoadSingleRecentFile();

        if (saveString != null)
        {
            SaveObject saveObject = JsonUtility.FromJson<SaveObject>(saveString);

But the saving it self is in the first script.

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?

1 Like

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).

2 Likes

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.

2 Likes