版本: Unity 6 (6000.0)
语言英语
  • C#

AsyncReadManager.ReadDeferred

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交的内容,但我们会阅读用户提出的每项建议更改,并在适用时进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static Unity.IO.LowLevel.Unsafe.ReadHandle ReadDeferred(ref Unity.IO.LowLevel.Unsafe.FileHandle fileHandle, ReadCommandArray* readCmdArray, Unity.Jobs.JobHandle dependency);

参数

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(); } }