public static Unity.IO.LowLevel.Unsafe.ReadHandle Read (string filename, ReadCommand* readCmds, uint readCmdCount);

参数

filename要读取的文件名。
readCmds指向 ReadCommand 结构(用于指定偏移、大小和目标缓冲区)数组的指针。
readCmdCountreadCmds 指向的读取命令数。

返回

ReadHandle 用于监控读取命令的进度和状态。

描述

发出一个异步文件读取操作。返回 ReadHandle。

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;

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

public unsafe void Update() { if (readHandle.IsValid() &amp;&amp; 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(); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961