# Read

> Issues an asynchronous file read operation. Returns a ReadHandle.

## Definition

* **Type:** Method
* **Namespace:** [Unity.IO.LowLevel.Unsafe](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe.md)
* **Assembly:** UnityEngine.CoreModule

## Read(string, ReadCommand\*, uint, string, ulong, AssetLoadingSubsystem)

Issues an asynchronous file read operation. Returns a ReadHandle.

```csharp
public static ReadHandle Read(string filename, ReadCommand* readCmds, uint readCmdCount, string assetName = "", ulong typeID = 0, AssetLoadingSubsystem subsystem = AssetLoadingSubsystem.Scripts)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): The filename to read from.**** (\[ReadCommand\*]\(/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/readcommand)): A pointer to an array of ReadCommand structs that specify offset, size, and destination buffer.**** (\[uint]\(https\://learn.microsoft.com/dotnet/api/system.uint32)): The number of read commands pointed to by readCmds.**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): (Optional) The name of the object being read, for metrics purposes.**** (\[ulong]\(https\://learn.microsoft.com/dotnet/api/system.uint64)): (Optional) The [TypeID](/engine/6000.5/manual/working-with-scenes/text-scene-format/class-idreference.md) of the object being read, for metrics purposes.**** (\[AssetLoadingSubsystem]\(/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/assetloadingsubsystem)): (Optional) The [Subsystem tag](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/assetloadingsubsystem.md) for the read operation, for metrics purposes.

### Returns

| Type                                                                                 | Description                                                  |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------ |
| [ReadHandle](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/readhandle.md) | Used to monitor the progress and status of the read command. |

### Remarks

You can set the `assetName`, `typeId`, and `subsystem` parameters to collect asset-specific metrics for this read operation. When you enable metrics collection with [AsyncReadManagerMetrics.StartCollectingMetrics](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/asyncreadmanagermetrics/startcollectingmetrics.md), Unity includes this information as part of the [AsyncReadManagerMetrics](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/asyncreadmanagermetrics.md), allowing you to analyze how different types of assets affect performance.

The AsyncReadManager copies the data referenced by readCmds; you can dispose or free the data immediately after calling `Read`.

### Examples

```csharp
using System.IO;
using Unity.Collections;
using Unity.IO.LowLevel.Unsafe;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;

class AsyncReadSample : MonoBehaviour
{
    private ReadHandle readHandle;
    NativeArray<ReadCommand> cmds;
    string assetName = "myfile";
    ulong typeID = 114; // from https://docs.unity.com/engine/6000.5/manual/working-with-scenes/text-scene-format/class-idreference
    AssetLoadingSubsystem subsystem = AssetLoadingSubsystem.Scripts;

    public unsafe void Start()
    {
        string filePath = Path.Combine(Application.streamingAssetsPath, "myfile.bin");
        cmds = new NativeArray<ReadCommand>(1, Allocator.Persistent);
        ReadCommand cmd;
        cmd.Offset = 0;
        cmd.Size = 1024;
        cmd.Buffer = (byte*)UnsafeUtility.Malloc(cmd.Size, 16, Allocator.Persistent);
        cmds[0] = cmd;
        readHandle = AsyncReadManager.Read(filePath, (ReadCommand*)cmds.GetUnsafePtr(), 1, assetName, typeID, subsystem);
    }

    public unsafe void Update()
    {
        if (readHandle.IsValid() && readHandle.Status != ReadStatus.InProgress)
        {
            Debug.LogFormat("Read {0}", readHandle.Status == ReadStatus.Complete ? "Successful" : "Failed");
            readHandle.Dispose();
            UnsafeUtility.Free(cmds[0].Buffer, Allocator.Persistent);
            cmds.Dispose();
        }
    }
}
```

## Read(FileHandle, ReadCommandArray)

Queues a set of read operations for a file opened with [AsyncReadManager.OpenFileAsync](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager/openfileasync.md).

```csharp
public static ReadHandle Read(in FileHandle fileHandle, ReadCommandArray readCmdArray)
```

### Parameters

**** (\[FileHandle]\(/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/filehandle)): The FileHandle to be read from, opened by [AsyncReadManager.OpenFileAsync](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/asyncreadmanager/openfileasync.md).**** (\[ReadCommandArray]\(/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/readcommandarray)): A struct containing the read commands to queue.

### Returns

| Type                                                                                 | Description                                                                                                                                                                    |
| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [ReadHandle](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/readhandle.md) | A [ReadHandle](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/readhandle.md) object you can use to check the status and monitor the progress of the read operations. |

### Remarks

This function makes a copy of the [ReadCommandArray](/engine/6000.5/script-reference/unity/io/lowlevel/unsafe/readcommandarray.md) struct passed as a parameter internally, so you do not need to maintain the array.

**Note:** WebGL builds do not support using `AsyncReadManager` to open files from a remote web server; for example, from the path `Application.streamingAssetsPath` which maps to a URL on a remote web server.

### Examples

```csharp
using System.IO;
using Unity.Collections;
using Unity.IO.LowLevel.Unsafe;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using Unity.Jobs;

class AsyncReadSample : MonoBehaviour
{
    static string TestFilename = Path.Combine(Application.streamingAssetsPath, "myfile.bin");

    public unsafe void Start()
    {
        ReadCommand cmd;
        cmd.Offset = 0;
        cmd.Size = 1024;
        cmd.Buffer = (byte*)UnsafeUtility.Malloc(cmd.Size, 16, Allocator.Persistent);

        FileHandle fileHandle = AsyncReadManager.OpenFileAsync(TestFilename);

        ReadCommandArray readCmdArray;
        readCmdArray.ReadCommands = &cmd;
        readCmdArray.CommandCount = 1;

        ReadHandle readHandle = AsyncReadManager.Read(fileHandle, readCmdArray);

        JobHandle closeJob = fileHandle.Close(readHandle.JobHandle);

        closeJob.Complete();

        // ... Use the data read into the buffer

        readHandle.Dispose();

        for (int i = 0; i < readCmdArray.CommandCount; i++)
            UnsafeUtility.Free(readCmdArray.ReadCommands[i].Buffer, Allocator.Persistent);
    }
}
```
