Class FileExtensions
Extension methods that enable fluent, caller-friendly file I/O on common types.
Rather than calling ScyllaFileUtil directly, these extensions allow
data to be saved in a natural, object-oriented style (e.g.
myBytes.SaveToFile(path) or texture.SaveAsPNG(path)).
Extensions are organized by the type they extend:
-
byte[]- write raw binary data, optionally with GZip compression or AES encryption. -
string- write or append UTF-8 text, with optional encoding or FileSettings overrides. -
Texture2D- encode and save Unity textures as PNG, JPG, WEBP, TGA, or EXR using ImageFileSettings presets or per-format convenience methods. -
IEnumerable<string>- write or append a sequence of text lines. -
Generic objects (
T : class) - JSON-serialize an object and write it to a file, optionally with compression and encryption (secure save).
All synchronous methods throw FileException on failure. All asynchronous variants return a Task and accept an optional CancellationToken.
Inherited Members
Namespace: Scylla.Core.Util.File
Assembly: ScyllaCore.dll
Syntax
public static class FileExtensions
Methods
AppendLinesToFile(IEnumerable<string>, string)
Appends the sequence of strings to an existing text file, writing each element on its own line, and creating the file and any missing parent directories if they do not already exist. Uses UTF-8 encoding.
Declaration
public static void AppendLinesToFile(this IEnumerable<string> lines, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | lines | The sequence of lines to append. Each element is written as a single line followed by
a line separator. May be empty but must not be |
| string | path | The absolute or relative file path to append to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the append operation fails due to an I/O error. |
AppendLinesToFileAsync(IEnumerable<string>, string, CancellationToken)
Asynchronously appends the sequence of strings to an existing text file, writing each element on its own line, and creating the file and any missing parent directories if they do not already exist. Uses UTF-8 encoding.
Declaration
public static Task AppendLinesToFileAsync(this IEnumerable<string> lines, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | lines | The sequence of lines to append. Each element is written as a single line followed by
a line separator. May be empty but must not be |
| string | path | The absolute or relative file path to append to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the append operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the append operation fails due to an I/O error. |
AppendToFile(string, string)
Appends the string to an existing text file at the specified path, creating the file and any missing parent directories if they do not already exist. Uses UTF-8 encoding.
Declaration
public static void AppendToFile(this string text, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text content to append to the file. May be |
| string | path | The absolute or relative file path to append to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the append operation fails due to an I/O error. |
AppendToFileAsync(string, string, CancellationToken)
Asynchronously appends the string to an existing text file at the specified path, creating the file and any missing parent directories if they do not already exist. Uses UTF-8 encoding.
Declaration
public static Task AppendToFileAsync(this string text, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text content to append to the file. May be |
| string | path | The absolute or relative file path to append to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the append operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the append operation fails due to an I/O error. |
SaveAsEXR(Texture2D, string, EXRFlags)
Encodes the texture as an OpenEXR high dynamic range (HDR) image and writes it to the specified path. EXR supports 16-bit and 32-bit floating-point color channels and is the standard format for HDR content in visual effects and linear-space rendering workflows. Use UnityEngine.Texture2D.EXRFlags.CompressZIP for lossless ZIP compression.
Declaration
public static void SaveAsEXR(this Texture2D texture, string path, Texture2D.EXRFlags flags = EXRFlags.None)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the EXR image to. |
| Texture2D.EXRFlags | flags | The UnityEngine.Texture2D.EXRFlags controlling EXR encoding options, such as compression method and bit depth. Defaults to UnityEngine.Texture2D.EXRFlags.None (uncompressed 16-bit half-float). |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsEXRAsync(Texture2D, string, EXRFlags, CancellationToken)
Asynchronously encodes the texture as an OpenEXR high dynamic range (HDR) image and writes it to the specified path. EXR supports 16-bit and 32-bit floating-point color channels and is the standard format for HDR content in visual effects and linear-space rendering workflows.
Declaration
public static Task SaveAsEXRAsync(this Texture2D texture, string path, Texture2D.EXRFlags flags = EXRFlags.None, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the EXR image to. |
| Texture2D.EXRFlags | flags | The UnityEngine.Texture2D.EXRFlags controlling EXR encoding options, such as compression method and bit depth. Defaults to UnityEngine.Texture2D.EXRFlags.None (uncompressed 16-bit half-float). |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encode-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsJPG(Texture2D, string, int)
Encodes the texture as a lossy JPEG image with the specified quality and writes it to the specified path. Higher quality values produce larger files with fewer artifacts; lower values produce smaller files with more visible compression. JPEG does not support transparency - any alpha channel in the source texture is discarded.
Declaration
public static void SaveAsJPG(this Texture2D texture, string path, int quality = 90)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the JPEG image to. |
| int | quality | The JPEG encoding quality in the range 1 (lowest quality, smallest file) to 100 (highest quality, largest file). Defaults to DEFAULT_QUALITY (90). |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsJPGAsync(Texture2D, string, int, CancellationToken)
Asynchronously encodes the texture as a lossy JPEG image with the specified quality and writes it to the specified path. JPEG does not support transparency - any alpha channel in the source texture is discarded.
Declaration
public static Task SaveAsJPGAsync(this Texture2D texture, string path, int quality = 90, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the JPEG image to. |
| int | quality | The JPEG encoding quality in the range 1 (lowest quality, smallest file) to 100 (highest quality, largest file). Defaults to DEFAULT_QUALITY (90). |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encode-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsPNG(Texture2D, string)
Encodes the texture as a lossless PNG image and writes it to the specified path. PNG supports transparency (alpha channel) and produces pixel-perfect output with no compression artifacts, at the cost of larger file sizes compared to lossy formats.
Declaration
public static void SaveAsPNG(this Texture2D texture, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the PNG image to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsPNGAsync(Texture2D, string, CancellationToken)
Asynchronously encodes the texture as a lossless PNG image and writes it to the specified path. PNG supports transparency (alpha channel) and produces pixel-perfect output with no compression artifacts.
Declaration
public static Task SaveAsPNGAsync(this Texture2D texture, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the PNG image to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encode-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsTGA(Texture2D, string)
Encodes the texture as a TGA (Targa) image and writes it to the specified path. TGA is a legacy lossless format that supports transparency (alpha channel) and is widely supported by 3D content creation tools and texture pipelines.
Declaration
public static void SaveAsTGA(this Texture2D texture, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the TGA image to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsTGAAsync(Texture2D, string, CancellationToken)
Asynchronously encodes the texture as a TGA (Targa) image and writes it to the specified path. TGA is a legacy lossless format that supports transparency (alpha channel) and is widely supported by 3D content creation tools and texture pipelines.
Declaration
public static Task SaveAsTGAAsync(this Texture2D texture, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the TGA image to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encode-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveAsWEBP(Texture2D, string, int)
Encodes the texture as a WebP image with the specified quality and writes it to the specified path. WebP supports both transparency (alpha channel) and lossy compression, and typically achieves smaller file sizes than JPEG or PNG at equivalent visual quality, making it well-suited for web and streaming use cases.
Declaration
public static void SaveAsWEBP(this Texture2D texture, string path, int quality = 90)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the WebP image to. |
| int | quality | The WebP encoding quality in the range 1 (lowest quality, smallest file) to 100 (highest quality, largest file). Defaults to DEFAULT_QUALITY (90). |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when WebP encoding is not supported or the write fails. |
SaveAsWEBPAsync(Texture2D, string, int, CancellationToken)
Asynchronously encodes the texture as a WebP image with the specified quality and writes it to the specified path. WebP supports both transparency (alpha channel) and lossy compression, making it well-suited for web and streaming use cases.
Declaration
public static Task SaveAsWEBPAsync(this Texture2D texture, string path, int quality = 90, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the WebP image to. |
| int | quality | The WebP encoding quality in the range 1 (lowest quality, smallest file) to 100 (highest quality, largest file). Defaults to DEFAULT_QUALITY (90). |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encode-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when WebP encoding is not supported or the write fails. |
SaveCompressedToFile(byte[], string)
Compresses the byte array using GZip and writes the compressed data to a file at the specified path. Use this for binary data that benefits from lossless compression, such as serialized game state or asset bundles. To read the data back, use the corresponding decompression API in ScyllaFileUtil.
Declaration
public static void SaveCompressedToFile(this byte[] data, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to compress and write. |
| string | path | The absolute or relative file path to write the compressed data to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the compression or write operation fails due to an I/O or compression error. |
SaveCompressedToFileAsync(byte[], string, CancellationToken)
Asynchronously compresses the byte array using GZip and writes the compressed data to a file at the specified path. Use this for binary data that benefits from lossless compression, such as serialized game state or asset bundles.
Declaration
public static Task SaveCompressedToFileAsync(this byte[] data, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to compress and write. |
| string | path | The absolute or relative file path to write the compressed data to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the compress-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the compression or write operation fails due to an I/O or compression error. |
SaveEncryptedToFile(byte[], string, string)
Encrypts the byte array using AES (via ScyllaCrypto) and writes the
encrypted data to a file at the specified path. The password is
used to derive an AES key via PBKDF2. To recover the data, use the corresponding
decryption API in ScyllaFileUtil with the same password.
Declaration
public static void SaveEncryptedToFile(this byte[] data, string path, string password)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to encrypt and write. |
| string | path | The absolute or relative file path to write the encrypted data to. |
| string | password | The plaintext password used to derive the AES encryption key. Must not be |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the encryption or write operation fails due to a cryptographic or I/O error. |
SaveEncryptedToFileAsync(byte[], string, string, CancellationToken)
Asynchronously encrypts the byte array using AES (via ScyllaCrypto) and
writes the encrypted data to a file at the specified path. The password
is used to derive an AES key via PBKDF2.
Declaration
public static Task SaveEncryptedToFileAsync(this byte[] data, string path, string password, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to encrypt and write. |
| string | path | The absolute or relative file path to write the encrypted data to. |
| string | password | The plaintext password used to derive the AES encryption key. Must not be |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encrypt-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the encryption or write operation fails due to a cryptographic or I/O error. |
SaveLinesToFile(IEnumerable<string>, string)
Writes the sequence of strings to a text file, writing each element on its own line using the platform's default line separator, and overwriting any existing file. Parent directories are created as needed. Uses UTF-8 encoding and default FileSettings.
Declaration
public static void SaveLinesToFile(this IEnumerable<string> lines, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | lines | The sequence of lines to write. Each element is written as a single line followed by
a line separator. May be empty but must not be |
| string | path | The absolute or relative file path to write to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveLinesToFile(IEnumerable<string>, string, FileSettings)
Writes the sequence of strings to a text file using the provided I/O settings, writing each element on its own line and overwriting any existing file. Parent directories are created as needed.
Declaration
public static void SaveLinesToFile(this IEnumerable<string> lines, string path, FileSettings settings)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | lines | The sequence of lines to write. Each element is written as a single line followed by
a line separator. May be empty but must not be |
| string | path | The absolute or relative file path to write to. |
| FileSettings | settings | The FileSettings controlling the write operation, including encoding and buffer size. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveLinesToFileAsync(IEnumerable<string>, string, FileSettings, CancellationToken)
Asynchronously writes the sequence of strings to a text file using the provided I/O settings, writing each element on its own line and overwriting any existing file. Parent directories are created as needed.
Declaration
public static Task SaveLinesToFileAsync(this IEnumerable<string> lines, string path, FileSettings settings, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | lines | The sequence of lines to write. Each element is written as a single line followed by
a line separator. May be empty but must not be |
| string | path | The absolute or relative file path to write to. |
| FileSettings | settings | The FileSettings controlling the write operation, including encoding and buffer size. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveLinesToFileAsync(IEnumerable<string>, string, CancellationToken)
Asynchronously writes the sequence of strings to a text file, writing each element on its own line and overwriting any existing file. Parent directories are created as needed. Uses UTF-8 encoding.
Declaration
public static Task SaveLinesToFileAsync(this IEnumerable<string> lines, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<string> | lines | The sequence of lines to write. Each element is written as a single line followed by
a line separator. May be empty but must not be |
| string | path | The absolute or relative file path to write to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveSecureToFileAsync<T>(T, string, string, CancellationToken)
Asynchronously serializes obj to JSON, GZip-compresses the result,
AES-encrypts the compressed bytes (via ScyllaCrypto), and writes the
encrypted data to the specified path. This is the recommended approach for persisting
sensitive game data such as save files.
Declaration
public static Task SaveSecureToFileAsync<T>(this T obj, string path, string password, CancellationToken cancellationToken = default) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj | The object instance to serialize and securely write. Must not be |
| string | path | The absolute or relative file path to write the encrypted data to. |
| string | password | The plaintext password used to derive the AES encryption key via PBKDF2. Must not be
|
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the serialize-compress-encrypt-write pipeline has finished. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of object to serialize. Must be a reference type and must be serializable by the ScyllaSerialization engine. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when serialization, compression, encryption, or writing fails. |
SaveSecureToFile<T>(T, string, string)
Serializes obj to JSON, GZip-compresses the result, AES-encrypts
the compressed bytes (via ScyllaCrypto), and writes the encrypted data
to the specified path. This is the recommended approach for persisting sensitive game
data such as save files. To recover the object, use the corresponding secure load API
in ScyllaFileUtil with the same password.
Declaration
public static void SaveSecureToFile<T>(this T obj, string path, string password) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj | The object instance to serialize and securely write. Must not be |
| string | path | The absolute or relative file path to write the encrypted data to. |
| string | password | The plaintext password used to derive the AES encryption key via PBKDF2. Must not be
|
Type Parameters
| Name | Description |
|---|---|
| T | The type of object to serialize. Must be a reference type and must be serializable by the ScyllaSerialization engine. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when serialization, compression, encryption, or writing fails. |
SaveToFile(byte[], string)
Writes the byte array to a file at the specified path, overwriting any existing file and creating parent directories as needed. Uses default FileSettings (64KB buffer, no async I/O).
Declaration
public static void SaveToFile(this byte[] data, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to write to the file. |
| string | path | The absolute or relative file path to write to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFile(byte[], string, FileSettings)
Writes the byte array to a file at the specified path using the provided I/O settings, which control buffer size, overwrite behavior, directory creation, and async I/O mode.
Declaration
public static void SaveToFile(this byte[] data, string path, FileSettings settings)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to write to the file. |
| string | path | The absolute or relative file path to write to. |
| FileSettings | settings | The FileSettings controlling the write operation. Use presets such as BinaryFile or LargeFile for common scenarios, or construct a custom instance. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFile(string, string)
Writes the string to a text file at the specified path, overwriting any existing file and creating parent directories as needed. Uses UTF-8 encoding and default FileSettings.
Declaration
public static void SaveToFile(this string text, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text content to write to the file. May be |
| string | path | The absolute or relative file path to write to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFile(string, string, FileSettings)
Writes the string to a text file at the specified path using the provided I/O settings, which control encoding, buffer size, overwrite behavior, and directory creation.
Declaration
public static void SaveToFile(this string text, string path, FileSettings settings)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text content to write to the file. May be |
| string | path | The absolute or relative file path to write to. |
| FileSettings | settings | The FileSettings controlling the write operation. The Encoding property determines the text encoding used. Use TextFile for BOM-aware UTF-8 text files. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFile(string, string, Encoding)
Writes the string to a text file at the specified path using the specified text encoding, overwriting any existing file and creating parent directories as needed.
Declaration
public static void SaveToFile(this string text, string path, Encoding encoding)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text content to write to the file. May be |
| string | path | The absolute or relative file path to write to. |
| Encoding | encoding | The Encoding to use when writing the text. Common choices are UTF8, Unicode, or ASCII. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFile(Texture2D, string)
Encodes the texture to the format specified by Default
(PNG) and writes it to the specified path. The texture must be readable (i.e. not
have read/write disabled in its import settings or been created with
MakeReadable = false).
Declaration
public static void SaveToFile(this Texture2D texture, string path)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the image to. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveToFile(Texture2D, string, ImageFileSettings)
Encodes the texture using the format and quality specified in settings
and writes the result to the specified path. Use ImageFileSettings presets
such as HighQuality, WebOptimized,
or HDR for common scenarios.
Declaration
public static void SaveToFile(this Texture2D texture, string path, ImageFileSettings settings)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the image to. |
| ImageFileSettings | settings | The ImageFileSettings controlling the image format, quality (for lossy formats), EXR flags, and other encoding options. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveToFileAsync(byte[], string, FileSettings, CancellationToken)
Asynchronously writes the byte array to a file at the specified path using the provided I/O settings. Parent directories are created as needed.
Declaration
public static Task SaveToFileAsync(this byte[] data, string path, FileSettings settings, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to write to the file. |
| string | path | The absolute or relative file path to write to. |
| FileSettings | settings | The FileSettings controlling the write operation. Use
Async or LargeFile for async
operations, or construct a custom instance with UseAsyncIO
set to |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFileAsync(byte[], string, CancellationToken)
Asynchronously writes the byte array to a file at the specified path, overwriting any existing file and creating parent directories as needed. Uses default FileSettings with async I/O.
Declaration
public static Task SaveToFileAsync(this byte[] data, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The raw binary data to write to the file. |
| string | path | The absolute or relative file path to write to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFileAsync(string, string, FileSettings, CancellationToken)
Asynchronously writes the string to a text file at the specified path using the provided I/O settings. Parent directories are created as needed.
Declaration
public static Task SaveToFileAsync(this string text, string path, FileSettings settings, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text content to write to the file. May be |
| string | path | The absolute or relative file path to write to. |
| FileSettings | settings | The FileSettings controlling the write operation, including encoding
and buffer size. Use Async or set
UseAsyncIO to |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFileAsync(string, string, CancellationToken)
Asynchronously writes the string to a text file at the specified path, overwriting any existing file and creating parent directories as needed. Uses UTF-8 encoding and default FileSettings with async I/O.
Declaration
public static Task SaveToFileAsync(this string text, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text content to write to the file. May be |
| string | path | The absolute or relative file path to write to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when the write operation fails due to an I/O error. |
SaveToFileAsync(Texture2D, string, ImageFileSettings, CancellationToken)
Asynchronously encodes the texture using the format and quality specified in
settings and writes the result to the specified path.
Declaration
public static Task SaveToFileAsync(this Texture2D texture, string path, ImageFileSettings settings, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the image to. |
| ImageFileSettings | settings | The ImageFileSettings controlling the image format, quality (for lossy formats), EXR flags, and other encoding options. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encode-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveToFileAsync(Texture2D, string, CancellationToken)
Asynchronously encodes the texture to the format specified by Default (PNG) and writes it to the specified path.
Declaration
public static Task SaveToFileAsync(this Texture2D texture, string path, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The UnityEngine.Texture2D to encode and save. Must not be |
| string | path | The absolute or relative file path to write the image to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the encode-and-write operation has finished. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when encoding or writing fails. |
SaveToFileAsync<T>(T, string, CancellationToken)
Asynchronously serializes obj to JSON using the
ScyllaSerialization engine with default settings and writes the resulting
JSON text to the specified file path, overwriting any existing file. Parent directories
are created as needed.
Declaration
public static Task SaveToFileAsync<T>(this T obj, string path, CancellationToken cancellationToken = default) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj | The object instance to serialize and write. Must not be |
| string | path | The absolute or relative file path to write the JSON to. |
| CancellationToken | cancellationToken | An optional CancellationToken that can be used to cancel the operation. Defaults to None. |
Returns
| Type | Description |
|---|---|
| Task | A Task that completes when the serialize-and-write operation has finished. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of object to serialize. Must be a reference type and must be serializable by the ScyllaSerialization engine. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when serialization or writing fails. |
SaveToFile<T>(T, string)
Serializes obj to JSON using the ScyllaSerialization
engine with default settings and writes the resulting JSON text to the specified file
path, overwriting any existing file. Parent directories are created as needed. To load
the object back, use the corresponding deserialization API in ScyllaFileUtil.
Declaration
public static void SaveToFile<T>(this T obj, string path) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj | The object instance to serialize and write. Must not be |
| string | path | The absolute or relative file path to write the JSON to. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of object to serialize. Must be a reference type and must be serializable by the ScyllaSerialization engine (i.e., decorated with the appropriate serialization attributes, or a type with built-in support). |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when serialization or writing fails. |
SaveToFile<T>(T, string, SerializationSettings)
Serializes obj to JSON using the ScyllaSerialization
engine with the specified serialization settings and writes the resulting JSON text to
the specified file path, overwriting any existing file. Parent directories are created
as needed.
Declaration
public static void SaveToFile<T>(this T obj, string path, SerializationSettings settings) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj | The object instance to serialize and write. Must not be |
| string | path | The absolute or relative file path to write the JSON to. |
| SerializationSettings | settings | The SerializationSettings controlling serialization behavior, such as pretty-printing, null value handling, and type discrimination. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of object to serialize. Must be a reference type and must be serializable by the ScyllaSerialization engine. |
Exceptions
| Type | Condition |
|---|---|
| FileException | Thrown when serialization or writing fails. |