Version: 2020.1
LanguageEnglish
  • C#

AsyncReadManager.Read

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

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

Parameters

filename 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.

Returns

ReadHandle Used to monitor the progress and status of the read command.

Description

Issues an asynchronous file read operation. Returns a 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() && 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(); } } }

Did you find this page useful? Please give it a rating: