# Tutorial: avatars in Unity

The 360sona Renderer package in a Unity project, an avatar in a scene, and the package's two samples: Plaza (twelve
avatars walking, the bare starter) and Components (walking, waving, something in the hand, the player editor).

## 1. Add the package

1. Download the SDK for Unity ([installing](../install.md)). It holds the `com.360sona.renderer` package and your
   `sona-licence.json`.
2. In Unity (2021.3 or later), open Window > Package Manager, choose Add package from disk, and pick the
   package's `package.json`.
3. In the package's Samples, import Plaza and Components.

## 2. The context

Open the Components scene. One GameObject holds a `SonaContext`: set its `licence` to your `sona-licence.json`, and
its `catalogue` to `sona-catalogue.bin` renamed `.bytes` (the editor needs it). Leave `useGate` on to stream avatar
files from 360sona, or set `dumpFolder` to an asset folder you own.

## 3. The avatars

`player` and `friend` are GameObjects with a `SonaAvatar` each, holding a manifest. Their Transforms place them.
The sample switches clips with a crossfade:

```csharp
public void DoWave()
{
    if (player == null || player.State != global::Sona.SonaAvatarState.Ready) return;
    player.Crossfade(Wave, 0.2f, false);
    _waving = 2;
}
```

## 4. Walking

Each frame it moves the player's Transform and crossfades between the stock walk and the idle when it starts or
stops:

```csharp
if (moving != Walking && _waving <= 0
    && player.State == global::Sona.SonaAvatarState.Ready)
{
    Walking = moving;
    player.Crossfade(moving ? Walk : "idle", 0.25f);
}
```

## 5. Something in the hand

The player has a `SonaAnimatorBridge`: a Humanoid rig of Transforms that follows the avatar. Anything parented to
one of its bones moves with the avatar:

```csharp
Torch.transform.SetParent(hand, false);
Torch.transform.localPosition = new Vector3(0, 0.1f, 0);
```

## 6. The player editor

`SonaAvatarEditor` opens the editor over the game. Its `target` wears the result when the player finishes:

```csharp
editor.mode = mode;
editor.target = player;
editor.Open();
```

## 7. Play

Press Play. Arrows or WASD walk, Space waves, and the two buttons open the editor and the wardrobe picker.

## 8. The Plaza sample

Open `Plaza.unity` and press Play: twelve `Walker` objects, each a `SonaAvatar` in a costume, walk about
a plaza built from Unity primitives, with 360sona's logo while they load. `PlazaStarter.cs` is the whole of it:

```csharp
if (Loading)
{
    foreach (var a in Walkers) if (a == null || a.State != global::Sona.SonaAvatarState.Ready) return;
    Loading = false;
    for (var i = 0; i < 12; i++) Walkers[i].Play(Walk, true, 1, i * 0.13f);
    return;
}
Seconds += Time.deltaTime;
for (var i = 0; i < 12; i++) Place(i, Seconds);
```

It is the same scene as [the plaza starter](starter-plaza.md) on every other platform.

## Next

- The [engine reference](../reference/engines.md#unity) lists every component, field, method and event.
- Credit: `SonaCredit` shows a small "Powered by 360sona" badge once an avatar is ready. Keep it, or credit 360sona
  in your credits, about page or store page.
