fileHandle | 要从中读取的文件句柄,由 AsyncReadManager.OpenFileAsync 打开。 |
readCmdArray | 指向包含要排队的读取命令的结构的指针。 |
dependency | 将触发读取开始的依赖项。 |
ReadHandle 一个 ReadHandle 对象,您可以使用它来检查读取操作的状态和监控进度。
在指定作业完成之后,为文件排队一组读取操作。
此函数不会复制作为参数传递的 ReadCommandArray 结构。您可以在将作业作为依赖项传递到此函数之前更改读取命令。读取将自动等待 FileHandle.JobHandle 完成。
作为不安全的低级 API,用户有责任避免在读取操作开始后更改或释放 ReadCommandArray。这样做会导致缓冲区溢出和竞争条件。(如果更改命令数量,请记住也要更新 ReadCommandArray.CommandCount 字段。)
注意:WebGL 构建不支持使用 AsyncReadManager
从远程 Web 服务器打开文件;例如,从映射到远程 Web 服务器上 URL 的路径 Application.streamingAssetsPath
。
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.