Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed May 20, 2024
1 parent ea56b84 commit 0aada9d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Assets/Unio/NativeArrayMemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public static unsafe Memory<T> AsMemory<T>(this NativeArray<T> nativeArray) wher
{
return new NativeArrayMemoryManager<T>((T*)nativeArray.GetUnsafeReadOnlyPtr(), nativeArray.Length).Memory;
}

public static unsafe Memory<T> AsMemory<T>(this NativeArray<T>.ReadOnly nativeArray) where T : unmanaged
{
return new NativeArrayMemoryManager<T>((T*)nativeArray.GetUnsafeReadOnlyPtr(), nativeArray.Length).Memory;
}
}

unsafe class NativeArrayMemoryManager<T> : MemoryManager<T> where T : unmanaged
Expand Down
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,42 @@ NativeFile.WriteAllBytes("/path/to/file", nativeArray);
### Unity Assets Integrations

The Unity engine provides an API that directly accepts `NativeArray<byte>`.
These APIs are useful when dynamic, unspecified texture loading is required, which cannot be pre-built as an asset.

```csharp
// Example of loading a yaml text from Addressable.
var textAsset = async Addressable.LoadAssetAsync<TextAsset>(assetPath);
var bytes = textAsset.GetData<byte>();

// Unio provides a extension to get Memory<byte> from NativeArray<byte>
var bytes = textAsset.GetData<byte>().AsMemory();

YamlSerializer.Deserialize<MyData>(bytes);
```

```csharp
// Example of loading a yaml text from url.
using var request = UnityWebRequest.Get(url);
while (!request.isDone)
{
yield return req.SendWebRequest();
}

// Unio provides a extension to get Memory<byte> from NativeArray<byte>
var bytes = request.downloadHandler.nativeData.AsMemory();
YamlSerializer.Deserialize<MyData>(bytes.AsMemory());
```

```csharp
var request = UnityWebRequest.Get(url);
// Texture2D, for example, has the ability to get/set with NativeArray<byte>.
var data = texture.GetRawTextureData<byte>();
NativeFile.WriteAllBytes("/path/to/file", data);

var savedData = NativeFile.ReadAllBytes("/path/to/file");
var texture2D = new Texture2D(w, h, format, mipChain); // Restore
texture2D.LoadRawTextureData(savedData);
texture2D.Apply();
```

There is also an API that takes a `NativeArray<byte>` as input.

## LICENSE

Expand Down

0 comments on commit 0aada9d

Please sign in to comment.