Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closefilename | The filename to read from. |
readCmds | A pointer to an array of ReadCommand structs that specify offset, size, and destination buffer. |
readCmdCount | The number of read commands pointed to by readCmds. |
assetName | (Optional) The name of the object being read, for metrics purposes. |
typeID | (Optional) The TypeID of the object being read, for metrics purposes. |
subsystem | (Optional) The Subsystem tag for the read operation, for metrics purposes. |
ReadHandle Used to monitor the progress and status of the read command.
Issues an asynchronous file read operation. Returns a ReadHandle.
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, Unity includes this information as part of the AsyncReadManagerMetrics, allowing you to analyze how different types of assets affect performance.
The AsyncReadManager copies the data referenced by Read
; you can dispose or free the data immediately after calling Read
.
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 ClassIDReference 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(); } } }
fileHandle | The FileHandle to be read from, opened by AsyncReadManager.OpenFileAsync. |
readCmdArray | A struct containing the read commands to queue. |
ReadHandle A ReadHandle object you can use to check the status and monitor the progress of the read operations.
Queues a set of read operations for a file opened with OpenFileAsync.
This function makes a copy of the ReadCommandArray 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.
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); } }
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.