Sandbox Rules
The Cilbox rules that are not obvious from the API, and the performance costs to plan around.
Cilbox interprets your script's compiled code inside a sandbox. Most C# works, but these rules catch nearly everyone once.
Lifecycle
- No
LateUpdate,OnGUI,OnAnimatorIK,OnParticleCollisionor coroutines. UseUpdateand delayed events. - The first
OnEnableis dropped. Set up inStart, tear down inOnDestroy. - Do not derive from
BasisNetworkBehaviour. The proxy that replaces your script is not one. UseBasisNetworkShim(see Network Events).
Structs share their value
Copying a Unity struct (Vector3, Quaternion, Color) into a variable shares the same value. Changing a field on the copy changes the original.
// Wrong: also moves `target`.
Vector3 p = target;
p.y = 0f;
// Right: build a new value.
Vector3 p = new Vector3(target.x, 0f, target.z);points[i].x = 5fon an array is silently lost. Writepoints[i] = new Vector3(5f, points[i].y, points[i].z).points[i] += offsetthrows. Writepoints[i] = points[i] + offset.- Values from Unity calls and operators (
a + b,transform.position,new Vector3(...)) are fresh and safe to change.
Calls and permissions
- A whitelisted type allows all its public methods unless that type also has a method restriction. Fields are denied unless listed.
- Parameter, generic and return types are checked too. A call can pass the name check and still be refused because of one of its types.
this.GetComponentalways works.gameObject.GetComponentis restricted, and the prop sandbox does not allow walking the hierarchy with it.AddComponent,SendMessageandBroadcastMessageare always blocked. Cilbox adds aBasisNetworkShimand the event shims for you onGetComponent.Debug.DrawLineandDebug.DrawRaydo not exist in the sandbox'sDebug, and referencing them stops every script in the content from loading.- Constants are free. A
constneeds no whitelist entry.
Events and delegates
Each time you use a method as a delegate it becomes a new wrapper, and -= may not remove the one you added. For publishers that outlive your script, poll a value in Update instead of subscribing.
Content police
Any component in your content that is neither [Cilboxable] nor on the allowlist is destroyed on load. Converting a script and allowing a component are separate. See Allowed Components, and Packages for how a package approves its own components.
Avatar scripts run on everyone's machine
Every Cilbox script on an avatar runs on every client that loads the avatar, up to the per avatar behaviour limit. Gate wearer only logic on avatar.IsOwnedLocally (see Avatar Scripting).
Performance
Interpreted code is much slower than native code. Rough costs on a desktop PC:
| Operation | Cost |
|---|---|
| Simple arithmetic on floats | ~15 ns |
| Calling one of your own methods | ~270 ns |
A Vector3 operator (a + b) | ~1.7 µs, plus garbage |
A Unity call such as Mathf.Sqrt | ~0.9 µs |
To keep scripts cheap:
- Do work on events, not every frame. Check timers, distances and UI a few times a second.
- Prefer floats over
Vector3math in loops. Vector math in a hot loop is about 100x slower than the same math on separate floats. - Let components do the heavy lifting. Animators, particle systems, physics and constraints run natively.
- Pool objects instead of calling
Instantiateevery shot. - Batch transform work.
BasisTransformSyncShimreads, writes or copies whole arrays of transforms in one call, andBasisBlendShapeSyncShimdoes the same for blendshapes.
Basis > Tools > Cilbox Permissions shows each sandbox's whitelist, and its Script Check tab scans your scripts for blocked calls before you upload.
Last updated on