// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using UltimateXR.Core; using UltimateXR.Manipulation; using UltimateXR.Manipulation.Helpers; namespace UltimateXR.Examples.FullScene.Lab { /// /// Component that adds the smooth slide-in/slide-out effects to the grabbable battery. /// public class Battery : UxrAutoSlideInObject { #region Unity /// /// Subscribes to the avatars updated event so that the manipulation logic is done after UltimateXR's manipulation /// logic has been updated. /// protected override void OnEnable() { base.OnEnable(); UxrManager.AvatarsUpdated += UxrManager_AvatarsUpdated; } /// /// Unsubscribes from the avatars updated event. /// protected override void OnDisable() { base.OnDisable(); UxrManager.AvatarsUpdated -= UxrManager_AvatarsUpdated; } #endregion #region Event Handling Methods /// /// Called after UltimateXR has done all the frame updating. Does the manipulation logic. /// private void UxrManager_AvatarsUpdated() { if (GrabbableObject.CurrentAnchor != null) { // Constrain transform when the battery is inside a door GeneratorDoor generatorDoor = GrabbableObject.CurrentAnchor.GetComponentInParent(); if (generatorDoor != null && !generatorDoor.IsLockOpen) { // Lock is closed? Battery can't move GrabbableObject.IsLockedInPlace = true; } else { // Battery can move GrabbableObject.IsLockedInPlace = false; } } } #endregion #region Event Trigger Methods /// protected override void OnPlacedAfterSlidingIn() { base.OnPlacedAfterSlidingIn(); if (GrabbableObject.CurrentAnchor != null) { GeneratorDoor generatorDoor = GrabbableObject.CurrentAnchor.GetComponentInParent(); if (generatorDoor != null) { // Turn lights on when the battery finished sliding in generatorDoor.IsBatteryInContact = true; } } } /// /// Called right after the battery was grabbed. /// /// Event parameters protected override void OnObjectGrabbed(UxrManipulationEventArgs e) { base.OnObjectGrabbed(e); if (!GrabbableObject.IsLockedInPlace && e.GrabbableAnchor != null) { GeneratorDoor generatorDoor = e.GrabbableAnchor.GetComponentInParent(); if (generatorDoor != null) { // Turn lights off when the battery is moved away generatorDoor.IsBatteryInContact = true; } } } #endregion } }