summaryrefslogtreecommitdiff
path: root/Assets/Samples/XR Interaction Toolkit/3.1.2/Starter Assets/Scripts/TeleportVolumeAnchorAffordanceStateLink.cs
blob: 7cc9a21b73cfbe7dbec6986f742b3f3a86a9e403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using UnityEngine.XR.Interaction.Toolkit.AffordanceSystem.State;
using UnityEngine.XR.Interaction.Toolkit.Interactables;
using UnityEngine.XR.Interaction.Toolkit.Locomotion.Teleportation;

namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets
{
    /// <summary>
    /// Helper component that binds an <see cref="XRInteractableAffordanceStateProvider"/> to a
    /// <see cref="TeleportationMultiAnchorVolume"/> when the teleport volume sets its destination anchor to a child transform
    /// of the state provider's originally bound interactable.
    /// </summary>
    [RequireComponent(typeof(XRInteractableAffordanceStateProvider))]
    [Obsolete("The Affordance System namespace and all associated classes have been deprecated. The existing affordance system will be moved, replaced and updated with a new interaction feedback system in a future version of XRI.")]
    public class TeleportVolumeAnchorAffordanceStateLink : MonoBehaviour
    {
        [SerializeField]
        [Tooltip("The teleport volume that will drive affordance states when its destination anchor belongs to this interactable.")]
        TeleportationMultiAnchorVolume m_ContainingTeleportVolume;

        /// <summary>
        /// The teleport volume that will drive affordance states when its destination anchor belongs to the
        /// state provider's originally bound interactable.
        /// </summary>
        public TeleportationMultiAnchorVolume containingTeleportVolume
        {
            get => m_ContainingTeleportVolume;
            set => m_ContainingTeleportVolume = value;
        }

        XRInteractableAffordanceStateProvider m_AffordanceStateProvider;
        IXRInteractable m_Interactable;

        /// <summary>
        /// See <see cref="MonoBehaviour"/>.
        /// </summary>
        protected void OnEnable()
        {
            m_AffordanceStateProvider = GetComponent<XRInteractableAffordanceStateProvider>();
            if (m_AffordanceStateProvider == null)
            {
                Debug.LogError($"Missing {nameof(XRInteractableAffordanceStateProvider)} on {gameObject.name}.", this);
                enabled = false;
                return;
            }

            if (m_ContainingTeleportVolume == null)
            {
                Debug.LogError($"Missing {nameof(TeleportationMultiAnchorVolume)} reference on {gameObject.name}.", this);
                enabled = false;
                return;
            }

            var interactableSource = m_AffordanceStateProvider.interactableSource;
            m_Interactable = interactableSource != null && interactableSource is IXRInteractable interactable
                    ? interactable
                    : m_AffordanceStateProvider.GetComponentInParent<IXRInteractable>();

            if (m_Interactable == null)
            {
                Debug.LogError($"Interactable source must be an {nameof(IXRInteractable)}.", this);
                enabled = false;
                return;
            }

            m_ContainingTeleportVolume.destinationAnchorChanged += OnDestinationAnchorChanged;
        }

        /// <summary>
        /// See <see cref="MonoBehaviour"/>.
        /// </summary>
        protected void OnDisable()
        {
            if (m_ContainingTeleportVolume != null)
                m_ContainingTeleportVolume.destinationAnchorChanged -= OnDestinationAnchorChanged;

            if (m_AffordanceStateProvider != null)
                m_AffordanceStateProvider.SetBoundInteractionReceiver(m_Interactable);
        }

        void OnDestinationAnchorChanged(TeleportationMultiAnchorVolume anchorVolume)
        {
            var anchor = anchorVolume.destinationAnchor;
            if (anchor == null)
            {
                m_AffordanceStateProvider.SetBoundInteractionReceiver(m_Interactable);
                return;
            }

            // Use teleport volume to drive affordance states if its current anchor belongs to this interactable
            m_AffordanceStateProvider.SetBoundInteractionReceiver(
                anchor.IsChildOf(m_Interactable.transform)
                    ? m_ContainingTeleportVolume
                    : m_Interactable);
        }
    }
}