Skip to content

Commit 3de1e48

Browse files
committed
fix(Pointers): ensure pointer id exists when checking ui pointer length
There was an issue where the pointerID didn't exist when checking the UI Pointer length which would cause a crash. This has been fixed by adding a check when retrieving the pointer max length from the dictionary and making the dictionary protected whilst providing a static public getter method.
1 parent e6ffb82 commit 3de1e48

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Assets/VRTK/Documentation/API.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7967,6 +7967,17 @@ Adding the `VRTK_UIPointer_UnityEvents` component to `VRTK_UIPointer` object all
79677967

79687968
### Class Methods
79697969

7970+
#### GetPointerLength/1
7971+
7972+
> `public static float GetPointerLength(int pointerId)`
7973+
7974+
* Parameters
7975+
* `int pointerId` - The pointer ID for the UI Pointer to recieve the length for.
7976+
* Returns
7977+
* `float` - The maximum length the UI Pointer will cast to.
7978+
7979+
The GetPointerLength method retrieves the maximum UI Pointer length for the given pointer ID.
7980+
79707981
#### SetEventSystem/1
79717982

79727983
> `public virtual VRTK_VRInputModule SetEventSystem(EventSystem eventSystem)`

Assets/VRTK/Source/Scripts/Internal/VRTK_UIGraphicRaycaster.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected virtual float GetHitDistance(Ray ray, float hitDistance)
9393
//[Pure]
9494
protected virtual void Raycast(Canvas canvas, Camera eventCamera, PointerEventData eventData, Ray ray, ref List<RaycastResult> results)
9595
{
96-
float hitDistance = GetHitDistance(ray, VRTK_UIPointer.pointerLengths[eventData.pointerId]);
96+
float hitDistance = GetHitDistance(ray, VRTK_UIPointer.GetPointerLength(eventData.pointerId));
9797
IList<Graphic> canvasGraphics = GraphicRegistry.GetGraphicsForCanvas(canvas);
9898
for (int i = 0; i < canvasGraphics.Count; ++i)
9999
{

Assets/VRTK/Source/Scripts/UI/VRTK_UIPointer.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public struct UIPointerEventArgs
5050
[AddComponentMenu("VRTK/Scripts/UI/VRTK_UIPointer")]
5151
public class VRTK_UIPointer : MonoBehaviour
5252
{
53-
public static Dictionary<int, float> pointerLengths = new Dictionary<int, float>();
54-
5553
/// <summary>
5654
/// Methods of activation.
5755
/// </summary>
@@ -182,6 +180,7 @@ public enum ClickMethods
182180
/// </summary>
183181
public event UIPointerEventHandler UIPointerElementDragEnd;
184182

183+
protected static Dictionary<int, float> pointerLengths = new Dictionary<int, float>();
185184
protected bool pointerClicked = false;
186185
protected bool beamEnabledState = false;
187186
protected bool lastPointerPressState = false;
@@ -193,6 +192,21 @@ public enum ClickMethods
193192
protected EventSystem cachedEventSystem;
194193
protected VRTK_VRInputModule cachedVRInputModule;
195194

195+
/// <summary>
196+
/// The GetPointerLength method retrieves the maximum UI Pointer length for the given pointer ID.
197+
/// </summary>
198+
/// <param name="pointerId">The pointer ID for the UI Pointer to recieve the length for.</param>
199+
/// <returns>The maximum length the UI Pointer will cast to.</returns>
200+
public static float GetPointerLength(int pointerId)
201+
{
202+
float maxLength;
203+
if (!pointerLengths.TryGetValue(pointerId, out maxLength))
204+
{
205+
maxLength = float.MaxValue;
206+
}
207+
return maxLength;
208+
}
209+
196210
public virtual void OnUIPointerElementEnter(UIPointerEventArgs e)
197211
{
198212
if (e.currentTarget != currentTarget)

0 commit comments

Comments
 (0)