Version: 2021.1
public static Unity.IO.LowLevel.Unsafe.ReadHandle Read (string filename, ReadCommand* readCmds, uint readCmdCount, string assetName, ulong typeID, Unity.IO.LowLevel.Unsafe.AssetLoadingSubsystem subsystem);

参数

filename 要读取的文件名。
readCmds 指向 ReadCommand 结构(用于指定偏移、大小和目标缓冲区)数组的指针。
readCmdCount 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 用于监控读取命令的进度和状态。

描述

发出一个异步文件读取操作。返回 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() &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(); } } }