Skip to main content

PhysicalButton

A class in Root.Township, the global namespace.

A push button driven by physics: the button part slides along one axis and fires Pressed and Released as it passes set depths. Runs locally on whichever machine simulates it; not networked by itself.

Constructors​

MemberWhat it is
PhysicalButton()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

28 private and internal fields
MemberWhat it is
private static readonly int _ColorShader property ID for _Color.
private Color activeColorColour when enabled. White by default.
private Material activeMaterialMaterial when enabled, if not changing colour. Defaults to the renderer's material.
private Renderer androidButtonRendererA second button renderer that is also kept out of atlasing.
private Collider buttonColliderBacking field for ButtonCollider.
private Renderer buttonRendererBacking field for ButtonRenderer.
private Rigidbody buttonRigidbodyRigidbody on the button collider: the part that slides.
private static SoundEffectSet defaultSoundEffectSetCache for DefaultSoundEffectSet.
private float delayEnableSeconds after Awake before the button enables. 0.05 by default.
private Task delayStartThe running DelayStart task.
private CancellationTokenSource delayTokenCancels DelayStart if IsEnabled is set or the button is destroyed first.
private Vector.Axis freeAxisLocal axis the button slides along. Y by default.
private Color inActiveColorColour when disabled. Grey by default.
private Material inactiveMaterialMaterial when disabled, if not changing colour.
private bool isActiveAtAllScalesIf false, presses only count while the button's scale is over 0.9 (so not while shrunk).
private bool isChangingColorOnEnableIf true, enabling swaps colours. If false, it swaps materials.
private bool isEnabledBacking field for IsEnabled.
private bool isPhysicallyPressedWhether the button is currently pushed past the press threshold.
private bool isPlayingSoundsPlays the button sound on press and release. True by default.
private bool isTestPressEditor button that fires a press.
private bool isTestReleaseEditor button that fires a release.
private float maxDepthFully out position along the axis. 0.67 by default.
private float minDepthFully in position along the axis. 0.31 by default.
private AltaEvent pressedBacking event for Pressed.
private float pressThresholdProgress (0 out, 1 fully in) past which the button presses. 0.8 by default.
private MaterialPropertyBlock propertyBlockProperty block used to set the colour.
private AltaEvent releasedBacking event for Released.
private float releaseThresholdProgress below which the button releases. 0.5 by default.

Properties​

MemberWhat it is
Collider ButtonCollider { get; }The moving part's collider. Its rigidbody is the part that slides.
Renderer ButtonRenderer { get; }Renderer recoloured (or given another material) when the button is enabled or disabled.
bool IsEnabled { get; set; }Whether the button can be pressed. Disabling it freezes the moving part, changes its look and stops presses.
bool State { get; private set; }True between a press and the following release.
1 private and internal property
MemberWhat it is
private static SoundEffectSet DefaultSoundEffectSet { get; }The global button pressed sound, resolved once and cached.

Events​

MemberWhat it is
event Action PressedRaised when the button is pushed past the press threshold while enabled and not already on.
event Action ReleasedRaised when the button returns past the release threshold with no one touching it.

Methods​

MemberWhat it is
void SetStateInternal(bool state)Sets State without firing events or sounds.
protected override void OnValidate()Editor: keeps the depth range valid and runs the test press or release buttons.
14 private and internal methods
MemberWhat it is
private void Awake()Finds the button rigidbody, disables the button and starts DelayStart to enable it shortly after.
private void ChangeVisuals(bool isActive)Sets the renderer to the active or inactive colour (through a property block) or material.
private async Task DelayStart()Waits delayEnable seconds, then enables the button unless cancelled.
private void FakePress(Interactable interactable = null, Interactor interactor = null)When a hand starts interacting, pushes the button in to minDepth on local Z and makes it kinematic.
private void FakeRelease(Interactable interactable = null, Interactor interactor = null)When the last hand lets go, moves the button partly out and makes it non-kinematic again.
private Vector3 GetClampedVector(Vector3 original, out float progress)Clamps a position to the depth range on the free axis (other axes 0) and returns progress, 1 at minDepth.
private bool Alta.Utilities.Atlas.IPreventAtlasing.IsPreventAtlasing(Renderer renderer)Keeps the button renderers out of texture atlasing, so their colour or material can change.
private void LateUpdate()Clamps the button along its axis and fires press or release as it crosses the thresholds. Skips buttons scaled under 0.9.
private void OnDestroy()Cancels a pending DelayStart.
private void OnDisable()Unhooks FakePress and FakeRelease.
private void OnDrawGizmos()Editor: draws the button's travel line.
private void OnEnable()Puts the button at minDepth, hooks FakePress and FakeRelease to interactions and marks it physically pressed.
private void OnPressed()Sets State, raises Pressed and plays the button sound if isPlayingSounds.
private void OnReleased()Clears State, raises Released and plays the button sound if isPlayingSounds.

Used in​

Members of other types that take, return or hold a PhysicalButton: