Skip to content

Commit d3afc62

Browse files
[CHANGE]: Add some missing tooltips and inspector conveniences to OnScreenStick (#1621)
Additionally, changing the range in dynamic origin mode now properly adjusts the size of the clickable region Co-authored-by: James McGill <[email protected]>
1 parent eda7c63 commit d3afc62

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

Packages/com.unity.inputsystem/InputSystem/Plugins/OnScreen/OnScreenStick.cs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace UnityEngine.InputSystem.OnScreen
3434
[HelpURL(InputSystem.kDocUrl + "/manual/OnScreen.html#on-screen-sticks")]
3535
public class OnScreenStick : OnScreenControl, IPointerDownHandler, IPointerUpHandler, IDragHandler
3636
{
37+
private const string kDynamicOriginClickable = "DynamicOriginClickable";
38+
3739
/// <summary>
3840
/// Callback to handle OnPointerDown UI events.
3941
/// </summary>
@@ -115,7 +117,7 @@ private void Start()
115117
if (m_Behaviour != Behaviour.ExactPositionWithDynamicOrigin) return;
116118
m_PointerDownPos = m_StartPos;
117119

118-
var dynamicOrigin = new GameObject("DynamicOriginClickable", typeof(Image));
120+
var dynamicOrigin = new GameObject(kDynamicOriginClickable, typeof(Image));
119121
dynamicOrigin.transform.SetParent(transform);
120122
var image = dynamicOrigin.GetComponent<Image>();
121123
image.color = new Color(1, 1, 1, 0);
@@ -283,6 +285,16 @@ private void DrawGizmoCircle(Vector2 center, float radius)
283285
}
284286
}
285287

288+
private void UpdateDynamicOriginClickableArea()
289+
{
290+
var dynamicOriginTransform = transform.Find(kDynamicOriginClickable);
291+
if (dynamicOriginTransform)
292+
{
293+
var rectTransform = (RectTransform)dynamicOriginTransform;
294+
rectTransform.sizeDelta = new Vector2(m_DynamicOriginRange * 2, m_DynamicOriginRange * 2);
295+
}
296+
}
297+
286298
/// <summary>
287299
/// The distance from the onscreen control's center of origin, around which the control can move.
288300
/// </summary>
@@ -297,7 +309,7 @@ public float movementRange
297309
/// </summary>
298310
/// <remarks>
299311
/// This only applies if <see cref="behaviour"/> is set to <see cref="Behaviour.ExactPositionWithDynamicOrigin"/>.
300-
/// When the first press is within this region, then the control will be apprear at that position and and have it's origin of motion placed there.
312+
/// When the first press is within this region, then the control will appear at that position and have it's origin of motion placed there.
301313
/// Otherwise, if pressed outside of this region the control will ignore it.
302314
/// This property defines the radius of the circular region. The center point being defined by the component position in the scene.
303315
/// </remarks>
@@ -306,15 +318,11 @@ public float dynamicOriginRange
306318
get => m_DynamicOriginRange;
307319
set
308320
{
321+
// ReSharper disable once CompareOfFloatsByEqualityOperator
309322
if (m_DynamicOriginRange != value)
310323
{
311324
m_DynamicOriginRange = value;
312-
var dynamicOriginGO = GameObject.Find("DynamicOriginClickable");
313-
if (dynamicOriginGO)
314-
{
315-
var rectTransform = (RectTransform)dynamicOriginGO.transform;
316-
rectTransform.sizeDelta = new Vector2(value * 2, value * 2);
317-
}
325+
UpdateDynamicOriginClickableArea();
318326
}
319327
}
320328
}
@@ -343,22 +351,28 @@ public bool useIsolatedInputActions
343351

344352
[FormerlySerializedAs("movementRange")]
345353
[SerializeField]
354+
[Min(0)]
346355
private float m_MovementRange = 50;
347356

348357
[SerializeField]
358+
[Tooltip("Defines the circular region where the onscreen control may have it's origin placed.")]
359+
[Min(0)]
349360
private float m_DynamicOriginRange = 100;
350361

351362
[InputControl(layout = "Vector2")]
352363
[SerializeField]
353364
private string m_ControlPath;
354365

355366
[SerializeField]
356-
[Tooltip("Choose how the onscreen stick will move relative to it's origin and the press position.")]
367+
[Tooltip("Choose how the onscreen stick will move relative to it's origin and the press position.\n\n" +
368+
"RelativePositionWithStaticOrigin: The control's center of origin is fixed. " +
369+
"The control will begin un-actuated at it's centered position and then move relative to the pointer or finger motion.\n\n" +
370+
"ExactPositionWithStaticOrigin: The control's center of origin is fixed. The stick will immediately jump to the " +
371+
"exact position of the click or touch and begin tracking motion from there.\n\n" +
372+
"ExactPositionWithDynamicOrigin: The control's center of origin is determined by the initial press position. " +
373+
"The stick will begin un-actuated at this center position and then track the current pointer or finger position.")]
357374
private Behaviour m_Behaviour;
358375

359-
[SerializeField]
360-
private bool m_ShowRanges;
361-
362376
[SerializeField]
363377
[Tooltip("Set this to true to prevent cancellation of pointer events due to device switching. Cancellation " +
364378
"will appear as the stick jumping back and forth between the pointer position and the stick center.")]
@@ -399,7 +413,7 @@ public Behaviour behaviour
399413
public enum Behaviour
400414
{
401415
/// <summary>The control's center of origin is fixed in the scene.
402-
/// The control will begin unactuated at it's centered position and then move relative to the press motion.</summary>
416+
/// The control will begin un-actuated at it's centered position and then move relative to the press motion.</summary>
403417
RelativePositionWithStaticOrigin,
404418

405419
/// <summary>The control's center of origin is fixed in the scene.
@@ -452,7 +466,12 @@ public override void OnInspectorGUI()
452466
if (EditorGUILayout.BeginFadeGroup(m_ShowDynamicOriginOptions.faded))
453467
{
454468
EditorGUI.indentLevel++;
469+
EditorGUI.BeginChangeCheck();
455470
EditorGUILayout.PropertyField(m_DynamicOriginRange);
471+
if (EditorGUI.EndChangeCheck())
472+
{
473+
((OnScreenStick)target).UpdateDynamicOriginClickableArea();
474+
}
456475
EditorGUI.indentLevel--;
457476
}
458477
EditorGUILayout.EndFadeGroup();

0 commit comments

Comments
 (0)