Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
wthartmandev's avatar
wthartmandev
Explorer
1 year ago
Solved

Accessing GameObject associated with Interactor from an Interactable Unity

I am using the Meta XR Interaction SDK in a Unity mixed reality app. I have cube setup with a SnapInteractor and aa snap point with a SnapInteractable. I have code running for the "When Select" event with an InteractableUnityEventWrapper, but I need to reference the Interactor that has been snapped to the Interactable. I don't see a way to do this in the documentation. What am I missing / is there a reliable work around if it is not officially supported?

Thanks

  • Big_Flex's avatar
    Big_Flex
    1 year ago

    I've done some digging and learned that PointerEvents (which include the Data object) are irrelevant for snap interactions because snap doesn't use PointerEvents. I've asked the team why that is. But the good news is that I have a way for you to get the ID of the snap interactor 🙂 

    In the Interactable base class, the SelectingInteractorViews field lists all of the interactors that are currently selecting an interactable. So you can access that list from your SnapInteractable to get the ID of the selecting SnapInteractor. 

    Here's an example script I wrote that you can attach to a SnapInteractable. The script will log the IDs of any selecting interactors to a text dialog when you add or remove an object from the SnapInteractable. I tested the script in the SnapExamples scene. Below the script is a screenshot of my Unity inspector.

    using Oculus.Interaction;
    using UnityEngine;
    using TMPro;
    using System.Linq;
    
    public class LogInteractor : MonoBehaviour
    {
        [SerializeField]
        private TextMeshProUGUI textElement;
        [SerializeField]
        private SnapInteractable snapInteractable;
    
        public void GetSelectingInteractorId()
        {
            // Get all of the interactors that are currently selecting
            if (snapInteractable.SelectingInteractorViews.Any())
            {
                UpdatePanelText();
            }
            else
            {
                textElement.text = "No interactors are selecting right now.";
            }
        }
    
        public void UpdatePanelText()
        {
            string panelText = "";
            // Display each interactor ID on the panel. The IDs are unsorted, so they won't appear in order.
            foreach (IInteractorView interactorView in snapInteractable.SelectingInteractorViews)
            {
                panelText += interactorView.Identifier.ToString() + ", ";
            }
            textElement.text = "Selecting interactors are: " + panelText;
        }
    }

    Here's how I setup the script in the scene. You can see it's attached to the SnapInteractable that stores the planets and uses the dialog panel's text element to log the IDs. I'd upload a video of the script in action, but my Quest is refusing to cooperate.

    Hopefully that helps. Please let me know if you need any clarification or have additional questions! 

5 Replies

  • Big_Flex's avatar
    Big_Flex
    Meta Employee

    Hi wthartmandev, thanks for posting! I'm on the Interaction SDK team, so feel free to tag me in future SDK-related posts. To send that information to the interactable, I recommend using the Data property, which I've written about in this tutorial: https://developer.oculus.com/documentation/unity/unity-isdk-use-data/

    The scenario in the tutorial is different from yours, but the idea is the same. If you need help or have further questions, please let me know!  

    • wthartmandev's avatar
      wthartmandev
      Explorer

      What would be the difference between using the data property and just having a third class that keeps track of the data (e.g. the last snapped gameobject)?

      My understanding is that using the data property allows one to determine if an object is in use, as in the example it tells me if the hand is in use, but it cannot tell me which gameobject is interacting with it. I am looking for a way to have multiple gameobjects and multiple snap points, where when snapped a snap point intractable is able to reference the interactor it snapped with directly.

      Am I missing a way to do this with the data property?

      • Big_Flex's avatar
        Big_Flex
        Meta Employee

        I've done some digging and learned that PointerEvents (which include the Data object) are irrelevant for snap interactions because snap doesn't use PointerEvents. I've asked the team why that is. But the good news is that I have a way for you to get the ID of the snap interactor 🙂 

        In the Interactable base class, the SelectingInteractorViews field lists all of the interactors that are currently selecting an interactable. So you can access that list from your SnapInteractable to get the ID of the selecting SnapInteractor. 

        Here's an example script I wrote that you can attach to a SnapInteractable. The script will log the IDs of any selecting interactors to a text dialog when you add or remove an object from the SnapInteractable. I tested the script in the SnapExamples scene. Below the script is a screenshot of my Unity inspector.

        using Oculus.Interaction;
        using UnityEngine;
        using TMPro;
        using System.Linq;
        
        public class LogInteractor : MonoBehaviour
        {
            [SerializeField]
            private TextMeshProUGUI textElement;
            [SerializeField]
            private SnapInteractable snapInteractable;
        
            public void GetSelectingInteractorId()
            {
                // Get all of the interactors that are currently selecting
                if (snapInteractable.SelectingInteractorViews.Any())
                {
                    UpdatePanelText();
                }
                else
                {
                    textElement.text = "No interactors are selecting right now.";
                }
            }
        
            public void UpdatePanelText()
            {
                string panelText = "";
                // Display each interactor ID on the panel. The IDs are unsorted, so they won't appear in order.
                foreach (IInteractorView interactorView in snapInteractable.SelectingInteractorViews)
                {
                    panelText += interactorView.Identifier.ToString() + ", ";
                }
                textElement.text = "Selecting interactors are: " + panelText;
            }
        }

        Here's how I setup the script in the scene. You can see it's attached to the SnapInteractable that stores the planets and uses the dialog panel's text element to log the IDs. I'd upload a video of the script in action, but my Quest is refusing to cooperate.

        Hopefully that helps. Please let me know if you need any clarification or have additional questions!