NutIOUtils

UTILITIES

GetIndexedPath

  • string GetIndexedPath(string path, string fileName, string extension, int index)

Returns the path of the file with an index that hasn’t been used yet. For example, if you pass the values to get the indexed path of «file.txt», and it already exists, you’ll get «file 1.txt». If many more already exist, you’ll get the next possible index. It looks for the file name recursively, recalling itself as many times as needed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using NutTools.Utilities;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    void MyMethod()
    {
        byte[] bytes = File.ReadAllBytes("C:\Users\joe\Desktop\image.png");

        // If image.png already exists it'll return image 1.png.
        File.WriteAllBytes(NutIOUtils.GetIndexedPath("C:\Users\joe\Desktop\", "image", "png"), bytes);

        // If image 5.png already exists it'll return image 6.png, or keep calling until it finds an unused name.
        File.WriteAllBytes(NutIOUtils.GetIndexedPath("C:\Users\joe\Desktop\", "image", "png", 5), bytes);
    }
}

LoadSprite

  • Sprite LoadSprite(string path, int width, int height)

Loads the sprite located at “path” with the provided resolution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    void MyMethod()
    {
        Sprite sprite = NutIOUtils.LoadSprite("C:\Users\joe\Desktop\image.png", 512, 512);
    }
}

LoadToNullSprite and TryLoadToNullSprite

  • Texture2D LoadToNullSprite(ref Sprite sprite, string path, int width, int height)

Loads the image and sets sprite if sprite is null. It returns sprite even if not null.

  • bool TryLoadToNullSprite(ref Sprite sprite, string path, int width, int height)

Loads the image and sets sprite if sprite is null. It returns wether tex has been set or not.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using NutTools.Utilities;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    void MyMethod()
    {
        Sprite sprite = null;
        NutIOUtils.LoadToNullSprite(ref sprite, "C:\Users\joe\Desktop\image.png", 512, 512);

        if (NutIOUtils.TryLoadToNullSprite(ref sprite , "C:\Users\joe\Desktop\image.png", 512, 512))
        {
            // Do stuff…
        }
    }
}

LoadTexture

  • Texture2D LoadTexture(string path, int width, int height)

Loads the image located at “path” with the provided resolution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using NutTools.Utilities;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    void MyMethod()
    {
        Texture2D tex = NutIOUtils.LoadTexture("C:\Users\joe\Desktop\image.png", 512, 512);
    }
}

LoadToNullTexture and TryLoadToNullTexture

  • Texture2D LoadToNullTexture(ref Texture2D tex, string path, int width, int height)

Loads the texture and sets tex if tex is null. It returns tex even if not null.

  • bool TryLoadToNullTexture(ref Texture2D tex, string path, int width, int height)

Loads the texture and sets tex if tex is null. It returns wether it set tex or not.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using NutTools.Utilities;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    void MyMethod()
    {
        Texture2D tex = null;
        NutIOUtils.LoadToNullTexture(ref tex, "C:\Users\joe\Desktop\image.png", 512, 512);

        if (NutIOUtils.TryLoadToNullTexture(ref tex, "C:\Users\joe\Desktop\image.png", 512, 512))
        {
            // Do things…
        }
    }
}

GET HELP

Join this Discord server to get help from the community, suggest new features, and vote on future updates!

See also

  • NutArrayUtils and NutListUtils
  • NutColorUtils
  • NutLogUtils
  • NutTextureUtils