fileHandle | The FileHandle to be read from, opened by AsyncReadManager.OpenFileAsync. |
readCmdArray | A pointer to a struct containing the read commands to queue. |
dependency | The dependency that will trigger the read to begin. |
ReadHandle A ReadHandle object you can to use to check the status and monitor the progress of the read operations.
Queues a set of read operations for a file once the specified Jobs have completed.
This function does not make a copy of the ReadCommandArray struct passed as a parameter. You can change the read commands until the Jobs passed to this function as a dependency are complete. The read will automatically wait for the FileHandle.JobHandle to be complete.
As an unsafe, low-level API, it is the user's responsibility to avoid changing or freeing the ReadCommandArray after the read operation has started. Doing so could lead to buffer overruns and race conditions. (If you change the number of commands, remember to update the ReadCommandArray.CommandCount field, too.)
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.
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 struct ReadCommandJob : IJob { public NativeArray<ReadCommandArray> ReadCmdArrayNative;
public void Execute() { const int kReadCount = 1; const int kReadSize = 2048;
ReadCommand* readCmds = (ReadCommand*)UnsafeUtility.Malloc(sizeof(ReadCommand) * kReadCount, 16, Allocator.Persistent);
readCmds[0] = new ReadCommand() { Buffer = (byte*)UnsafeUtility.Malloc(kReadSize, 16, Allocator.Persistent), Offset = 0, Size = kReadSize };
ReadCmdArrayNative[0] = new ReadCommandArray { ReadCommands = readCmds, CommandCount = kReadCount }; } }
public unsafe void SetupReadInJob() { NativeArray<ReadCommandArray> readCmdArrayNative = new NativeArray<ReadCommandArray>(1, Allocator.Persistent); ReadCommandArray* readCmdArrayPtr = (ReadCommandArray*)readCmdArrayNative.GetUnsafePtr();
FileHandle fileHandle = AsyncReadManager.OpenFileAsync(TestFilename);
var createReadCommandJob = new ReadCommandJob { ReadCmdArrayNative = readCmdArrayNative }.Schedule();
ReadHandle readHandle = AsyncReadManager.ReadDeferred(fileHandle, readCmdArrayPtr, createReadCommandJob);
JobHandle closeJob = fileHandle.Close(readHandle.JobHandle);
createReadCommandJob.Complete(); // Ensure the NativeArray is finished with before using closeJob.Complete();
// ... Use the data read into the buffer
readHandle.Dispose();
for (int i = 0; i < readCmdArrayNative[0].CommandCount; i++) UnsafeUtility.Free(readCmdArrayNative[0].ReadCommands[i].Buffer, Allocator.Persistent); UnsafeUtility.Free(readCmdArrayNative[0].ReadCommands, Allocator.Persistent);
readCmdArrayNative.Dispose(); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.