Avatar Scripting
Cilbox scripts on avatars, the wearer versus everyone else, avatar ready events, and syncing avatar state.
An avatar's Cilbox scripts run on every client that loads the avatar, not just the wearer's. Most avatar logic should check who is running it.
The avatar component
Inside the avatar sandbox, BasisAvatar resolves to BasisAvatarShim. Write BasisAvatar as usual.
using Basis.Scripts.BasisSdk;
using Cilbox;
using UnityEngine;
[Cilboxable]
public class AvatarGlow : MonoBehaviour
{
public GameObject glow;
BasisAvatar avatar;
void Start()
{
avatar = BasisAvatar.GetGameObject(this).GetComponent<BasisAvatar>();
avatar.OnAvatarReady += OnReady;
}
void OnReady(bool isOwner)
{
glow.SetActive(isOwner);
}
}| Member | Use |
|---|---|
OnAvatarReady(bool isOwner) | Fires once the avatar is set up. isOwner is true on the wearer's client. |
IsOwnedLocally | True on the wearer's client. |
Animator, FaceVisemeMesh, HumanScale | Read the avatar's rig. |
TryGetLinkedPlayer(out ushort playerId) | The wearer's player ID. |
PlayspaceInput | Drive the wearer's own movement (wearer only). |
Wearer only vs everyone
| Run on | For |
|---|---|
Wearer only (IsOwnedLocally) | Menus, haptics, saves, input, sending network events. |
| Everyone | Visual effects that follow synced state: particles, materials, sounds. |
void Update()
{
if (!avatar.IsOwnedLocally) return;
// wearer's input and decisions here
}Syncing avatar state
- Custom values: get a
BasisNetworkShimand send network events from the wearer, then apply them on every client. - Moving parts:
BasisSyncedTransformis allowed on avatars.
Settings tab on an avatar
void OnReady(bool isOwner)
{
if (!isOwner) return;
BasisMenuPage page = BasisMenuShim.AddSettingsTab(this, "My Avatar");
page?.AddToggle("Glow", true, on => glow.SetActive(on));
}Anything the avatar sandbox allows, a stranger's avatar can do on your machine. That is why the avatar sandbox is the strictest of the three: no cameras, no lights, no web requests, and haptics only reach the wearer.
Last updated on
Packages
How packages are installed into a Basis project, and how a package approves its own components for content police and exposes its own types to Cilbox scripts.
Networking
How to make a Basis world or prop behave the same for every player, synced variables, network events, ownership and late joiners.