XRShared Tools

The XRShared addon includes small tools used by the other addons and samples. Each one is independent: this page gives, for each tool, what it is for and how to use it.

Networking helpers

SharedAuthorityWatcher

In shared topology, objects whose state authority left the session may have no state authority. SharedAuthorityWatcher gives the state authority of such an object to a remaining user, and handles fast authority changes:

C#

protected SharedAuthorityWatcher _watcher = new SharedAuthorityWatcher();

public override void Render()
{
    _watcher.AffectStateAuthorityIfNone(Object);
}

It requests no authority outside of shared topology. Some SharedAuthorityExtensions methods are obsolete: use a SharedAuthorityWatcher instance instead.

NetworkReferenceChangeDetector

A [Networked] property referencing a NetworkBehaviour (or a NetworkObject) synchronizes the id of the referenced object. A ChangeDetector or [OnChangedRender] compares those ids: while the referenced object is not resolved yet on a client (in loaded scenes, or for late joiners), the property returns null, and when it becomes available a few frames later, the id did not change, so no change is detected.

NetworkReferenceChangeDetector compares the resolved values instead: a reference becoming available later is reported as a change (from null to the object), as well as a reference becoming null when the referenced object is despawned.

C#

[Networked] public NetworkGrabber CurrentGrabber { get; set; }
NetworkReferenceChangeDetector<NetworkGrabber> grabberChangeDetector;

public override void Spawned()
{
    // Initial value as baseline, like GetChangeDetector. Use new NetworkReferenceChangeDetector<NetworkGrabber>() to also report the initial value
    grabberChangeDetector = new NetworkReferenceChangeDetector<NetworkGrabber>(CurrentGrabber);
}

public override void Render()
{
    if (grabberChangeDetector.DetectChange(CurrentGrabber, out var previousGrabber, out var currentGrabber)) { /* ... */ }
}

AuthorityVisualization

AuthorityVisualization changes the material of target renderers depending on the state authority of the object: a quick way to debug authority changes.

Actions and messages

NamedActionManager

NamedActionManager centralizes actions identified by a name, so that any UI (a watch menu button, a hand menu, a keyboard shortcut) can trigger them without referencing the objects implementing them. A scene contains one NamedActionManager (NamedActionManager.SharedInstance).

Actions are declared:

  • in the inspector, in namedActionDescriptors: a name and a UnityEvent,
  • by code, with RegisterAction(actionName, action), for objects created at runtime (a network rig, ...) that the scene cannot reference. Call UnregisterAction with the same name and action when the object is destroyed.

NamedActionManager.Invoke(actionName) triggers an action.

Optional providers describe how the UI should display an action, and can be given to RegisterAction or set separately:

  • an availability condition (SetAvailabilityCondition): for instance, the buttons of an action are only displayed while it is available,
  • an active state (SetActiveStateProvider): the state a toggle action switches,
  • a display color (SetDisplayColorProvider).

The UI reads them with TryGetAvailability, TryGetActiveState and TryGetDisplayColor. The Watch Menu addon named action buttons follow them, and the Line Drawing and Finger Drawing addons register their actions this way.

PopupMessageHandler

PopupMessageHandler displays a message popup in front of the hardware rig camera (PopupMessageHandler.Instance):

  • ShowMessage(text, displayDuration) shows a message for a few seconds,
  • ShowPermanentMessage(text) shows it until HideMessage() is called.

The popup follows the user's head smoothly (positionOffsetRelativeToHeadset, smoothingSpeed).

TogglePassthrough

TogglePassthrough switches between VR and passthrough (mixed reality): ToggleMode(), ActivatePassthrough(), DesactivatePassthrough().

It changes the camera clear flags and background color (mrClearFlags / mrBackgroundcolor for passthrough, the VR ones fetched from the camera defaults), and enables the game objects listed for each mode (passthroughOnlyGameObjects, vrOnlyGameObjects). With vrOnlyLocomotionPointers, the locomotion pointers of the rig (its teleport beamers) are also VR only, including the ones created later (for instance on Meta building block rigs).

ignoreARCameraManagerPresence can be checked when targeting Android XR only, which does not require an ARCameraManager for passthrough.

Colors

NetworkColorSelection

NetworkColorSelection synchronizes a color chosen in a color list (CurrentColor networked property), and provides it as an IColorProvider:

  • ChangeColor(index), NextColor() and PreviousColor() change the color: they have to be called on the state authority,
  • when the color changes, ApplyColorChange() is called on all users: by default, it tints the coloredRenderers, and it can be overridden,
  • the start color is the first one of colorList (white if the list is empty), which DefaultColor() can override.

Drawing tools read their color from an IColorProvider: placed on a user network rig, a NetworkColorSelection gives the same synchronized color to all the drawing tools of the user (pens with searchColorProviderInParents, finger drawing tools).

GrabbableColorSelection is a NetworkColorSelection whose color can be changed with a controller input while the object is grabbed (the pens of the 3D & 2D drawing addon use it).

Hands and tracking

LocalHardwareHandTracker

A LocalHardwareHandTracker object, created for a network hand, gives access to the local hardware hand associated with it (null for remote users), and to its index tip. After a Refresh(), IsAvailable tells whether the hand is tracked for the local user and its index tip is found.

The HardwareHandExtensions methods also help with hardware hands:

  • IsTracked(): the rig part is tracked (false for a missing rig part),
  • FindIndexTip(): the index tip of a hardware hand (its IndexTipFollowerTransform on a Meta building blocks rig, otherwise its IndexTipMarker),
  • FindGrabCandidateProviders(): the grabbers of a hand able to tell if a grab would occur.

OneEuroFilter

OneEuroFilter is a serializable One Euro filter: a low pass filter whose cutoff frequency increases with the speed. Slow moves are strongly smoothed (for instance to remove the jitter of hand tracking), while fast moves are followed with little lag.

Markers

  • IndexTipMarker: the index tip of a hand, also an interaction tip for UI interaction (with XSCInputModule).
  • WristMarker: the wrist of a hand (top and bottom), registered to the wrist trackers of its parents. The Watch Menu addon places the watch on it.
  • ObjectTip: the end of an object, like the tip of a pen.

Platform and display helpers

  • DeleteForAndroid, DeleteForDesktop, DeleteForWebGL: destroy their game object on the matching builds.
  • DisableForDesktop: disables a component in desktop builds (and optionally in the editor).
  • HideForLocalUser: makes sure the user camera cannot see their own avatar, by changing its layer and the camera culling mask.
  • CullingMaskSetter: adds or removes layers, by name, from the culling mask of its camera.
  • LayerSetter: sets a layer by name, or warns that the layer does not exist yet in the project.

Permissions

On Android, several components may need to request permissions (microphone, scene, camera, ...), and simultaneous requests conflict. A PermissionsRequester launches the permission requests one after the other, and PermissionWaiter components wait for a permission to be granted before starting what needs it.

Photon Voice 2.64 and newer ship their own PermissionsRequester and PermissionWaiter (with iOS, visionOS and WebGL support). When Photon Voice is installed, XRShared's classes become obsolete subclasses of Photon Voice's ones: existing scenes keep working and keep their settings, and only one permission requester exists.

Editor and build tools

CLIBuilder builds the project from the command line, with options to change the App Ids, the built scenes, or to add defines for one build:

  • with the editor closed, in Unity batch mode (-executeMethod Fusion.XRShared.BuildTools.CLIBuilder.Build): Unity exits with the build exit code (0 success, 1 build failed, 2 refused by a validator, 3 invalid arguments), and a JSON build report is written,
  • with the editor open and the Unity Pipeline package installed: the registered xrshared.build command.

IBuildValidator implementations can refuse a build before it starts.

Last updated on

Back to top