summaryrefslogtreecommitdiff
path: root/Assets/Samples/OpenXR Plugin/1.14.3/Controller/Scripts/DisplayDeviceInfoFromActionISX.cs
blob: 606d244c973f9498a5f0a21832718edca1f31adc (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
using UnityEngine.UI;
using UnityEngine.InputSystem;

namespace UnityEngine.XR.OpenXR.Samples.ControllerSample
{
    public class DisplayDeviceInfoFromActionISX : MonoBehaviour
    {
        [SerializeField]
        InputActionProperty m_Property;
        public InputActionProperty property { get => m_Property; set => m_Property = value; }

        [SerializeField]
        GameObject m_RootObject = null;
        public GameObject rootObject { get { return m_RootObject; } set { m_RootObject = value; } }

        [SerializeField]
        Text m_TargetText;
        public Text targetText { get { return m_TargetText; } set { m_TargetText = value; } }

        void OnEnable()
        {
            if (targetText == null)
                Debug.LogWarning("DisplayDeviceInfo Monobehaviour has no Target Text set. No information will be displayed.");
        }

        void Update()
        {
            if (m_Property != null && m_Property.action != null && m_Property.action.controls.Count > 0)
            {
                if (m_RootObject != null)
                    m_RootObject.SetActive(true);


                var device = m_Property.action.controls[0].device;
                if (targetText != null)
                {
                    m_TargetText.text = $"{device.name}\n{device.deviceId}\n";
                    bool useComma = false;
                    foreach (var usg in device.usages)
                    {
                        if (!useComma)
                        {
                            useComma = true;
                            m_TargetText.text += $"{usg}";
                        }
                        else
                        {
                            m_TargetText.text += $"{usg},";
                        }
                    }

                    if (m_TargetText.text.Length > 30)
                        m_TargetText.text = m_TargetText.text.Substring(0, 30);
                }
                return;
            }
            else
            {
                if (m_RootObject != null)
                    m_RootObject.SetActive(false);

                // No Matching devices:
                if (m_TargetText != null)
                    m_TargetText.text = "<No Device Connected>";
            }
        }
    }
}