Skip to content

Commit 3b8c451

Browse files
committed
Adding ControllerVisualizer script.
1 parent 56c5ebf commit 3b8c451

File tree

6 files changed

+118
-10
lines changed

6 files changed

+118
-10
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using UnityEngine;
6+
using UnityEngine.VR.WSA.Input;
7+
8+
namespace HoloToolkit.Unity.InputModule
9+
{
10+
/// <summary>
11+
/// This script spawns a specific GameObject when a controller is detected
12+
/// and animates the controller position and orientation.
13+
/// </summary>
14+
[RequireComponent(typeof(SetGlobalListener))]
15+
public class ControllerVisualizer : MonoBehaviour, ISourceStateHandler, ISourceOrientationHandler, ISourcePositionHandler
16+
{
17+
[Tooltip("Use a model with the tip in the positive Z direction and the front face in the positive Y direction.")]
18+
[SerializeField]
19+
private GameObject controllerModel;
20+
21+
// This will be used to keep track of our controllers, indexed by its unique source ID.
22+
private Dictionary<uint, GameObject> controllerDictionary;
23+
24+
private void Start()
25+
{
26+
if (controllerModel == null)
27+
{
28+
Debug.Log("Please make sure to add a controller model in the Inspector for ControllerVisualizer.cs on " + name + ".");
29+
}
30+
31+
controllerDictionary = new Dictionary<uint, GameObject>();
32+
}
33+
34+
/// <summary>
35+
/// When a controller is detected, the model is spawned and the controller object
36+
/// is added to the tracking dictionary.
37+
/// </summary>
38+
/// <param name="eventData">The source event data to be used to set up our controller model.</param>
39+
public void OnSourceDetected(SourceStateEventData eventData)
40+
{
41+
InteractionSourceKind sourceKind;
42+
if (controllerModel != null && eventData.InputSource.TryGetSourceKind(eventData.SourceId, out sourceKind) && sourceKind == InteractionSourceKind.Controller)
43+
{
44+
if (!controllerDictionary.ContainsKey(eventData.SourceId))
45+
{
46+
GameObject controller = Instantiate(controllerModel);
47+
GameObject parent = new GameObject();
48+
controller.transform.parent = parent.transform;
49+
50+
controllerDictionary.Add(eventData.SourceId, parent);
51+
}
52+
}
53+
}
54+
55+
/// <summary>
56+
/// When a controller is lost, the model is destroyed and the controller object
57+
/// is removed from the tracking dictionary.
58+
/// </summary>
59+
/// <param name="eventData">The source event data to be used to detect which controller to destroy.</param>
60+
public void OnSourceLost(SourceStateEventData eventData)
61+
{
62+
InteractionSourceKind sourceKind;
63+
if (eventData.InputSource.TryGetSourceKind(eventData.SourceId, out sourceKind) && sourceKind == InteractionSourceKind.Controller)
64+
{
65+
GameObject controller;
66+
if (controllerDictionary.TryGetValue(eventData.SourceId, out controller))
67+
{
68+
Destroy(controller);
69+
controllerDictionary.Remove(eventData.SourceId);
70+
}
71+
}
72+
}
73+
74+
public void OnOrientationChanged(SourceOrientationEventData eventData)
75+
{
76+
GameObject controller;
77+
if (controllerDictionary.TryGetValue(eventData.SourceId, out controller))
78+
{
79+
controller.transform.rotation = eventData.Orientation;
80+
}
81+
}
82+
83+
public void OnPositionChanged(SourcePositionEventData eventData)
84+
{
85+
GameObject controller;
86+
if (controllerDictionary.TryGetValue(eventData.SourceId, out controller))
87+
{
88+
controller.transform.position = eventData.Position;
89+
}
90+
}
91+
}
92+
}

Assets/HoloToolkit/Input/Scripts/ControllerVisualizer.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/HoloToolkit/Input/Scripts/InputEvents/ISourceOrientationHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using UnityEngine.EventSystems;
5+
46
namespace HoloToolkit.Unity.InputModule
57
{
68
/// <summary>
79
/// Interface to implement to react to source orientation changes.
810
/// </summary>
9-
public interface ISourceOrientationHandler : IInputHandler
11+
public interface ISourceOrientationHandler : IEventSystemHandler
1012
{
1113
void OnOrientationChanged(SourceOrientationEventData eventData);
1214
}

Assets/HoloToolkit/Input/Scripts/InputEvents/ISourcePositionHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using UnityEngine.EventSystems;
5+
46
namespace HoloToolkit.Unity.InputModule
57
{
68
/// <summary>
79
/// Interface to implement to react to source position changes.
810
/// </summary>
9-
public interface ISourcePositionHandler : IInputHandler
11+
public interface ISourcePositionHandler : IEventSystemHandler
1012
{
1113
void OnPositionChanged(SourcePositionEventData eventData);
1214
}

Assets/HoloToolkit/Input/Scripts/InputSources/GesturesInput.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private void UpdateSourceState(InteractionSourceState interactionSource, SourceD
537537
sourceData.Orientation.IsSupported |= sourceData.Orientation.IsAvailable;
538538
if (sourceData.Orientation.IsAvailable)
539539
{
540-
if(!(sourceData.Orientation.CurrentReading.Equals(newOrientation)))
540+
if (!(sourceData.Orientation.CurrentReading.Equals(newOrientation)))
541541
{
542542
InputManager.Instance.RaiseSourceOrientationChanged(this, sourceData.SourceId, newOrientation);
543543
}
@@ -556,7 +556,7 @@ private void UpdateSourceState(InteractionSourceState interactionSource, SourceD
556556
if (sourceData.Thumbstick.IsAvailable)
557557
{
558558
AxisButton2D newThumbstick = AxisButton2D.GetThumbstick(interactionSource);
559-
if ((sourceData.Thumbstick.CurrentReading.X != newThumbstick.X) || (sourceData.Thumbstick.CurrentReading.Y != newThumbstick.Y))
559+
if ((sourceData.Thumbstick.CurrentReading.X != newThumbstick.X) || (sourceData.Thumbstick.CurrentReading.Y != newThumbstick.Y))
560560
{
561561
InputManager.Instance.RaiseInputXYChanged(this, sourceData.SourceId, InteractionPressKind.Thumbstick, newThumbstick.X, newThumbstick.Y);
562562
}
@@ -680,4 +680,4 @@ protected void OnNavigationCanceledEvent(NavigationCanceledEventArgs obj)
680680

681681
#endregion
682682
}
683-
}
683+
}

Assets/HoloToolkit/Input/Scripts/SetGlobalListener.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using UnityEngine;
55

6-
namespace HoloToolkit.Unity.InputModule.Tests
6+
namespace HoloToolkit.Unity.InputModule
77
{
88
/// <summary>
99
/// Register this game object on the InputManager as a global listener.
@@ -15,31 +15,31 @@ public class SetGlobalListener : MonoBehaviour
1515
private void Start()
1616
{
1717
started = true;
18-
if (InputManager.Instance != null)
18+
if (InputManager.IsInitialized)
1919
{
2020
InputManager.Instance.AddGlobalListener(gameObject);
2121
}
2222
}
2323

2424
private void OnEnable()
2525
{
26-
if (started && InputManager.Instance != null)
26+
if (started && InputManager.IsInitialized)
2727
{
2828
InputManager.Instance.AddGlobalListener(gameObject);
2929
}
3030
}
3131

3232
private void OnDisable()
3333
{
34-
if (InputManager.Instance != null)
34+
if (InputManager.IsInitialized)
3535
{
3636
InputManager.Instance.RemoveGlobalListener(gameObject);
3737
}
3838
}
3939

4040
private void OnDestroy()
4141
{
42-
if (InputManager.Instance != null)
42+
if (InputManager.IsInitialized)
4343
{
4444
InputManager.Instance.RemoveGlobalListener(gameObject);
4545
}

0 commit comments

Comments
 (0)