Last active
March 3, 2026 01:10
-
-
Save wotakuro/27ba59b0d2c981c98bcd27bf6654f637 to your computer and use it in GitHub Desktop.
AsyncReadManagerが同期読み込みでも使えたサンプルです
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using Unity.IO.LowLevel.Unsafe; | |
| using Unity.Collections; | |
| using Unity.Collections.LowLevel.Unsafe; | |
| // AsyncReadManagerを使った同期ファイル読み込みです | |
| // AndroidでもStreamingAsset以下にあるファイルにアクセスすることが可能になっています | |
| public class AsyncReadManagerSyncReadSample : MonoBehaviour | |
| { | |
| void Start() | |
| { | |
| unsafe { | |
| // StreamingAssets以下の test.txt | |
| string filename = System.IO.Path.Combine(Application.streamingAssetsPath, "test.txt"); | |
| FileInfoResult result; | |
| var info = AsyncReadManager.GetFileInfo(filename, &result); | |
| // Asyncの処理を即時で終了させます | |
| info.JobHandle.Complete(); | |
| // ファイルサイズの取得 | |
| Debug.Log(result.FileSize); | |
| using (var buffer = new NativeArray<byte>((int)result.FileSize, Allocator.Temp)) | |
| { | |
| ReadCommand readCommand = new ReadCommand() | |
| { | |
| Buffer = buffer.GetUnsafePtr(), | |
| Offset = 0, | |
| Size = result.FileSize | |
| }; | |
| var handle = AsyncReadManager.Read(filename, &readCommand, 1); | |
| // Asyncの処理を即時で終了させます | |
| handle.JobHandle.Complete(); | |
| // これでbufferにデータが入っています | |
| // stringに変換してデバッグ出力します。 | |
| string str = System.Text.UTF8Encoding.UTF8.GetString(buffer.AsReadOnlySpan()); | |
| Debug.Log(str); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment