# Xbox 360 avatars in C# and .NET

To use Xbox 360 avatars in C#, reference `Sona.csproj` from the SDK download, open a `SonaContext` with your licence
and add each avatar from its manifest. It runs on .NET 8 and inside Unity.

![A C# program's run, and the avatar.png it saved: an avatar mid-wave](../img/one-tutorial-csharp.webp)

## Steps

1. Download the SDK for your platform ([getting it](../web-api.md#sdk-download)), and have a manifest as base64.
2. Reference `Sona.csproj` in your download from your project.
3. Open a `SonaContext` with your licence and a source: `SonaSourceSpec.Gate()` streams the item files from 360sona,
   `SonaSourceSpec.Dir(folder)` reads a folder of your own.
4. Add the avatar with `AddBase64` and call `Update` until it is ready. Then play clips, read joints or render it.

```xml
  <SonaBindings Condition="'$(SonaBindings)' == ''">sdk/bindings/csharp</SonaBindings>
</PropertyGroup>

<ItemGroup>
  <ProjectReference Include="$(SonaBindings)/Sona.csproj" />
</ItemGroup>
```

## Open a context and add the avatar

```csharp
var options = new SonaOptions
{
    Licence = File.ReadAllBytes(args[0]),
    AppId = Environment.GetEnvironmentVariable("SONA_APP_ID"),
};
options.Sources.Add(dir != null ? SonaSourceSpec.Dir(dir) : SonaSourceSpec.Gate());   // the gate: item files from 360sona
using var ctx = new SonaContext(options);

var avatar = ctx.AddBase64(args[1]);
while (avatar.State != SonaAvatarState.Ready && avatar.State != SonaAvatarState.Failed) ctx.Update(1.0 / 60);
```

Then `avatar.Play("GenericWave")` waves, `TryGetJointWorld` reads where a joint is, and `SonaHeadless` draws a
picture of it with no window.

## More

- [The C# tutorial](../tutorials/csharp.md) builds the whole console program, which saves a picture of the wave.
- In Unity, the same binding sits under the Unity package ([Xbox 360 avatars in Unity](xbox-360-avatars-in-unity.md)).
- Credit: show "Powered by 360sona" where your users see the avatars. `EncodePng` adds the credit to a PNG's text
  chunks.
- 360sona is not affiliated with or endorsed by Microsoft.
