Skip to content

Instantly share code, notes, and snippets.

@beardordie
Last active April 27, 2024 22:05
Show Gist options
  • Save beardordie/023781dd6ea7d1810b715b04738a1f11 to your computer and use it in GitHub Desktop.
Save beardordie/023781dd6ea7d1810b715b04738a1f11 to your computer and use it in GitHub Desktop.
Meta Interaction SDK: Hide the Controller visuals when a Grabbable is grabbed. How? Get access to the (manually) assigned Data of an Interactor. This version Uses Odin Inspector and UltEvents,. For this to work, you must manually assign the Optionals -> Data object of each Interactor to its sibling ControllerRef component.
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* Licensed under the Oculus SDK License Agreement (the "License");
* you may not use the Oculus SDK except in compliance with the License,
* which is provided at the time of installation or download, or which
* otherwise accompanies this software in either electronic or hard copy form.
*
* You may obtain a copy of the License at
*
* https://developer.oculus.com/licenses/oculussdk/
*
* Unless required by applicable law or agreed to in writing, the Oculus SDK
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Oculus.Interaction.Input;
using Oculus.Interaction.Input.Visuals;
using Sirenix.OdinInspector;
using UnityEngine;
using UltEvents;
namespace Oculus.Interaction
{
public class GrabbablePointerEvents : MonoBehaviour
{
public bool ShouldHideControllerWhenGrabbed = false;
public UltEvent<Object> onGrab = new UltEvent<Object>();
public UltEvent<Object> onRelease = new UltEvent<Object>();
public UltEvent<Grabbable> onGrabGrabbable = new UltEvent<Grabbable>();
public UltEvent<Grabbable> onReleaseGrabbable = new UltEvent<Grabbable>();
void AutosetGrabbableComponent()
{
if (_grabbable == null)
{
_grabbable = GetComponentInParent<Grabbable>();
}
}
[InlineButton(nameof(AutosetGrabbableComponent), "Autoset")]
[Required][SerializeField] private Grabbable _grabbable;
private void Awake()
{
if (_grabbable == null) Debug.LogError("Grabbable reference is null", this);
_grabbable.WhenPointerEventRaised += HandlePointerRaised;
}
private void OnDestroy()
{
_grabbable.WhenPointerEventRaised -= HandlePointerRaised;
}
private void HandlePointerRaised(PointerEvent args)
{
//Debug.Log("Set a breakpoint here to see the Data passed in through the args. Pointer event data: " + args.Data.ToString());
// We're now going to assume that the Data field of the Interactor is set to a "ControllerRef" component.
// This has to be manually setup in the inspector first. If you choose to assign a different object as "Data", this will fail.
var data = args.Data as ControllerRef;
if (data == null) return;
if(args.Type == PointerEventType.Select && _grabbable.SelectingPointsCount > 0)
{
if(ShouldHideControllerWhenGrabbed)
{
Controller grabbingController = data.gameObject.GetComponentInParent<Controller>();
if (grabbingController != null)
{
OVRControllerVisual visual = grabbingController.GetComponentInChildren<OVRControllerVisual>();
if (visual != null)
{
visual.ForceOffVisibility = true;
}
}
}
onGrab?.Invoke(data);
onGrabGrabbable?.Invoke(_grabbable);
} else if(args.Type == PointerEventType.Unselect)
{
if(ShouldHideControllerWhenGrabbed)
{
Controller grabbingController = data.gameObject.GetComponentInParent<Controller>();
if (grabbingController != null)
{
OVRControllerVisual visual = grabbingController.GetComponentInChildren<OVRControllerVisual>();
if (visual != null)
{
visual.ForceOffVisibility = false;
}
}
}
onRelease?.Invoke(data);
onReleaseGrabbable?.Invoke(_grabbable);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment