Freyja File System

Freyja File System makes it easy to create files and directories with unity

Requirements

  • Unity 2021.3.22.f1 or later

Dependencies

Name
Tags

Installation

  1. Install from Unity Package Manager -> (Window -> Package Manager -> Add package from git URL)

  1. If Freyja File System successfully installed will look like this

Example Usage

Handle Directory

  1. Create two C# scripts

    1. TestHandleDirectory.cs (used for core scripts)

    2. TestCreatingDirectoryEditor.cs (use for operations in unity editor)

TestHandleDirectory.cs
using System;

using Freyja.FileSystem;

using UnityEngine;

public class TestHandleDirectory : MonoBehaviour
{
    [SerializeField]
    private string m_DirectoryName;

    private FreyjaDirectory _freyjaDirectory;

    public string DirectoryName
    {
        get
        {
            if (string.IsNullOrEmpty(m_DirectoryName))
            {
                return string.Empty;
            }


            return m_DirectoryName;
        }
    }
    
    public string GetFullPath()
    {
        _freyjaDirectory = new FreyjaDirectory();
        return _freyjaDirectory.Path + "/" + m_DirectoryName;
    }
}
TestHandleDirectoryEditor.cs
#if UNITY_EDITOR

using UnityEditor;

using UnityEngine;

[CustomEditor(typeof(TestHandleDirectory))]
public class TestHandleDirectoryEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        var script = (TestHandleDirectory)target;
        if (GUILayout.Button("Create Directory"))
        {
            // TODO: Creating Directory
        }

        if (GUILayout.Button("Delete Directory"))
        {
            // TODO: Delete Directory
        }

        if (GUILayout.Button($"Directory {script.DirectoryName} Is Exists"))
        {
            // TODO: Check Directory Is Exists
        }
    }
}

#endif
  1. Add TestHandleDirectory.cs into inspector

Create Directory

  1. Create function CreateDirectory()inside TestHandleDirectory.cs

TestHandleDirectory
public void CreateDirectory()
{
    _freyjaDirectory = new FreyjaDirectory();

    try
    {
        if (string.IsNullOrEmpty(DirectoryName))
        {
            throw new ArgumentNullException();
        }

        if (!_freyjaDirectory.DirectoryExists(DirectoryName))
        {
            _freyjaDirectory.CreateDirectory(DirectoryName);
        }
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine(e);
        throw;
    }
}
  1. Add CreateDirectory()into TestHandleDirectoryEditor.cs

TestHandleDirectoryEditor
if (GUILayout.Button("Create Directory"))
{
    script.CreateDirectory();
}

Delete Directory

  1. Create function DeleteDirectory()inside TestHandleDirectory.cs

TestHandleDirectory
public void DeleteDirectory()
{
    _freyjaDirectory = new FreyjaDirectory();

    try
    {
        if (string.IsNullOrEmpty(DirectoryName))
        {
            throw new ArgumentNullException();
        }

        if (_freyjaDirectory.DirectoryExists(DirectoryName))
        {
            _freyjaDirectory.DeleteDirectory(DirectoryName, true);
        }
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine(e);
        throw;
    }
}
  1. add DeleteDirectory()into TestHandleDirectoryEditor.cs

TestHandleDirectoryEditor.cs
if (GUILayout.Button("Delete Directory"))
{
    script.DeleteDirectory();
}

Check If Directory Exists

  1. Create function DeleteDirectory()inside TestHandleDirectory.cs

TestHandleDirectory
public void DirectoryIsExists()
{
    _freyjaDirectory = new FreyjaDirectory();
    try
    {
        if (string.IsNullOrEmpty(DirectoryName))
        {
            throw new ArgumentNullException();
        }

        Debug.Log(_freyjaDirectory.DirectoryExists(DirectoryName));
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine(e);
        throw;
    }
}
  1. add DeleteDirectory()into TestHandleDirectoryEditor.cs

TestHandleDirectoryEditor.cs
if (GUILayout.Button($"Directory {script.DirectoryName} Is Exists"))
{
    script.DirectoryIsExists();
}

Handle Files

Notes:

  • Before we create a new file prefer we should first prepare the directory

  1. Create two C# scripts

    1. TestHandleFile.cs (used for core scripts)

    2. TestHandleFileEditor.cs (use for operations in unity editor)

TestHandleFile
using System.Collections.Generic;
using Freyja.FileSystem;
using UnityEngine;

public class TestHandleFile : MonoBehaviour
{
    private enum FileType
    {
        Sav,
        Json,
        Txt,
    }

    private Dictionary<FileType, string> FileTypeMap = new Dictionary<FileType, string>()
    {
        { FileType.Sav, ".sav" },
        { FileType.Json, ".json" },
        { FileType.Txt, ".txt" },
    };

    [SerializeField]
    private TestHandleDirectory m_TestHandleDirectory;

    [SerializeField]
    private string m_File;

    [SerializeField]
    private FileType m_FileType;

    [SerializeField]
    [TextArea]
    private string m_Text;

    private FreyjaFile _freyjaFile;
    
    private void Reset()
    {
        m_TestHandleDirectory = FindObjectOfType<TestHandleDirectory>();
    }
}
TestHandleFileEditor
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(TestHandleFile))]
public class TestHandleFileEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        var script = (TestHandleFile)target;
        if (GUILayout.Button("Create File"))
        {
        }

        if (GUILayout.Button("Read File"))
        {
        }

        if (GUILayout.Button("Delete File"))
        {
        }
    }
}
#endif
  1. Add TestHandleFile.cs into inspector

Create File

  1. Create function CreateFile()inside TestHandleFile.cs

TestHandleFile
public void CreateFile()
{
    _freyjaFile = new FreyjaFile();

    try
    {
        if (string.IsNullOrEmpty(FullPath))
        {
            throw new ArgumentNullException();
        }

        _freyjaFile.WriteAllText(FullPath, m_Text);
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine(e);
        throw;
    }
}
  1. Add CreateFile()into TestHandleFileEditor.cs

TestHandleFileEditor
if (GUILayout.Button("Create File"))
{
    script.CreateFile();
}

Read File

  1. Create function ReadFile()inside TestHandleFile.cs

TestHandleFile
public void ReadFile()
{
    _freyjaFile = new FreyjaFile();

    try
    {
        if (string.IsNullOrEmpty(FullPath))
        {
            throw new ArgumentNullException();
        }

        var text = _freyjaFile.ReadAllText(FullPath);
        Debug.Log(text);
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine(e);
        throw;
    }
}
  1. Add ReadFile()into TestHandleFileEditor.cs

TestHandleFileEditor
if (GUILayout.Button("Create File"))
{
    script.ReadFile();
}

Delete File

  1. Create function DeleteFile()inside TestHandleFile.cs

TestHandleFile
public void DeleteFile()
{
    _freyjaFile = new FreyjaFile();

    try
    {
        if (string.IsNullOrEmpty(FullPath))
        {
            throw new ArgumentNullException();
        }

        _freyjaFile.Delete(FullPath);
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine(e);
        throw;
    }
}
  1. Add DeleteFile()into TestHandleFileEditor.cs

TestHandleFileEditor
if (GUILayout.Button("Delete File"))
{
    script.DeleteFile();
}

Last updated