Skip to content

Commit b104118

Browse files
committed
Added new Input System support (requires manual modifications, see documentation)
1 parent f42adcb commit b104118

File tree

13 files changed

+216
-27
lines changed

13 files changed

+216
-27
lines changed

.github/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ You can also connect the hierarchy to the inspector so that whenever an object r
3939

4040
Note that these connections are *one-directional*, meaning that assigning the inspector to the hierarchy will not automatically assign the hierarchy to the inspector or vice versa. Also note that the inspector and the hierarchy are **not** singletons and therefore, you can have several instances of them in your scene at a time with different configurations.
4141

42+
### NEW INPUT SYSTEM SUPPORT
43+
44+
This plugin supports Unity's new Input System but it requires some manual modifications (if both the legacy and the new input systems are active at the same time, no changes are needed):
45+
46+
- the plugin mustn't be installed as a package, i.e. it must reside inside the *Assets* folder and not the *Packages* folder (it can reside inside a subfolder of Assets like *Assets/Plugins*)
47+
- if Unity 2019.2.5 or earlier is used, add `ENABLE_INPUT_SYSTEM` compiler directive to **Player Settings/Scripting Define Symbols** (these symbols are platform specific, so if you change the active platform later, you'll have to add the compiler directive again)
48+
- add `Unity.InputSystem` assembly to **RuntimeInspector.Runtime** Assembly Definition File's *Assembly Definition References* list
49+
4250
## E. FEATURES
4351

4452
- The hierarchy costs **1 SetPass call** and **~5 batches** (assuming that **Sprite Packing** is enabled in *Editor Settings*)

Plugins/RuntimeInspector/README.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
Online documentation available at: https://github.com/yasirkula/UnityRuntimeInspector
44
55

6-
1. ABOUT
6+
### ABOUT
77
This asset is a simple yet powerful runtime Inspector and Hierarchy solution for Unity 3D that should work on pretty much any platform that Unity supports, including mobile platforms.
88

9-
2. HOW TO
9+
10+
### HOW TO
1011
Please see the online documentation for an in-depth documentation of the Scripting API: https://github.com/yasirkula/UnityRuntimeInspector
1112

1213
- To use the hierarchy in your scene, drag&drop the RuntimeHierarchy prefab to your canvas
@@ -16,4 +17,12 @@ You can connect the inspector to the hierarchy so that whenever the selection in
1617

1718
You can also connect the hierarchy to the inspector so that whenever an object reference in the inspector is highlighted, the selection in hierarchy is updated. To do this, assign the hierarchy to the Connected Hierarchy property of the inspector.
1819

19-
Note that these connections are one-directional, meaning that assigning the inspector to the hierarchy will not automatically assign the hierarchy to the inspector or vice versa. Also note that the inspector and the hierarchy are not singletons and therefore, you can have several instances of them in your scene at a time with different configurations.
20+
Note that these connections are one-directional, meaning that assigning the inspector to the hierarchy will not automatically assign the hierarchy to the inspector or vice versa. Also note that the inspector and the hierarchy are not singletons and therefore, you can have several instances of them in your scene at a time with different configurations.
21+
22+
23+
### NEW INPUT SYSTEM SUPPORT
24+
This plugin supports Unity's new Input System but it requires some manual modifications (if both the legacy and the new input systems are active at the same time, no changes are needed):
25+
26+
- the plugin mustn't be installed as a package, i.e. it must reside inside the Assets folder and not the Packages folder (it can reside inside a subfolder of Assets like Assets/Plugins)
27+
- if Unity 2019.2.5 or earlier is used, add ENABLE_INPUT_SYSTEM compiler directive to "Player Settings/Scripting Define Symbols" (these symbols are platform specific, so if you change the active platform later, you'll have to add the compiler directive again)
28+
- add "Unity.InputSystem" assembly to "RuntimeInspector.Runtime" Assembly Definition File's "Assembly Definition References" list

Plugins/RuntimeInspector/Scripts/RuntimeHierarchy.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ protected override void Awake()
339339
ShowHorizontalScrollbar = !m_showHorizontalScrollbar;
340340

341341
RuntimeInspectorUtils.IgnoredTransformsInHierarchy.Add( drawArea );
342+
343+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
344+
// On new Input System, scroll sensitivity is much higher than legacy Input system
345+
scrollView.scrollSensitivity *= 0.25f;
346+
#endif
342347
}
343348

344349
private void Start()
@@ -708,6 +713,18 @@ public void OnDrawerPointerEvent( HierarchyField drawer, PointerEventData eventD
708713
{
709714
currentlyPressedDrawer = null;
710715
pressedDrawerActivePointer = null;
716+
717+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
718+
// On new Input System, DraggedReferenceItems aren't tracked by the PointerEventDatas that initiated them. However, when a DraggedReferenceItem is
719+
// created by holding a HierarchyField, the PointerEventData's dragged object will be set as the RuntimeHierarchy's ScrollRect. When it happens,
720+
// trying to scroll the RuntimeHierarchy by holding the DraggedReferenceItem at top/bottom edge of the ScrollRect doesn't work because scrollbar's
721+
// value is overwritten by the original PointerEventData. We can prevent this issue by stopping original PointerEventData's drag operation here
722+
if( eventData.dragging && eventData.pointerDrag == scrollView.gameObject && DraggedReferenceItem.InstanceItem )
723+
{
724+
eventData.dragging = false;
725+
eventData.pointerDrag = null;
726+
}
727+
#endif
711728
}
712729
else if( m_createDraggedReferenceOnHold )
713730
{

Plugins/RuntimeInspector/Scripts/RuntimeHierarchy/Helpers/HierarchyDragDropListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void IDropHandler.OnDrop( PointerEventData eventData )
351351

352352
void IPointerEnterHandler.OnPointerEnter( PointerEventData eventData )
353353
{
354-
if( !eventData.dragging || !hierarchy.CanReorganizeItems || hierarchy.IsInSearchMode )
354+
if( !hierarchy.CanReorganizeItems || hierarchy.IsInSearchMode )
355355
return;
356356

357357
if( !RuntimeInspectorUtils.GetAssignableObjectFromDraggedReferenceItem( eventData, typeof( Transform ) ) )

Plugins/RuntimeInspector/Scripts/RuntimeInspector.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using UnityEngine;
55
using UnityEngine.EventSystems;
66
using UnityEngine.UI;
7+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
8+
using Pointer = UnityEngine.InputSystem.Pointer;
9+
#endif
710
using Object = UnityEngine.Object;
811

912
namespace RuntimeInspectorNamespace
@@ -280,6 +283,11 @@ private void Initialize()
280283

281284
RuntimeInspectorUtils.IgnoredTransformsInHierarchy.Add( drawArea );
282285
RuntimeInspectorUtils.IgnoredTransformsInHierarchy.Add( poolParent );
286+
287+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
288+
// On new Input System, scroll sensitivity is much higher than legacy Input system
289+
scrollView.scrollSensitivity *= 0.25f;
290+
#endif
283291
}
284292

285293
private void OnDestroy()
@@ -345,7 +353,13 @@ protected override void Update()
345353
// Check if a pointer has remained static over a drawer for a while; if so, show a tooltip
346354
if( hoveringPointer != null )
347355
{
356+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
357+
// PointerEventData.delta isn't set to (0,0) for static pointers in the new Input System, so we use the active Pointer's delta instead
358+
// The default value isn't Vector2.zero but Vector2.one because we don't want to show tooltip if there is no pointer
359+
Vector2 pointerDelta = Pointer.current != null ? Pointer.current.delta.ReadValue() : Vector2.one;
360+
#else
348361
Vector2 pointerDelta = hoveringPointer.delta;
362+
#endif
349363
if( pointerDelta.x != 0f || pointerDelta.y != 0f )
350364
hoveredDrawerTooltipShowTime = time + m_tooltipDelay;
351365
else if( time > hoveredDrawerTooltipShowTime )

Plugins/RuntimeInspector/Scripts/RuntimeInspector/Fields/EnumField.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public override void Initialize()
4747
{
4848
base.Initialize();
4949
input.onValueChanged.AddListener( OnValueChanged );
50+
51+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
52+
// On new Input System, scroll sensitivity is much higher than legacy Input system
53+
templateRoot.GetComponent<ScrollRect>().scrollSensitivity *= 0.25f;
54+
#endif
5055
}
5156

5257
public override bool SupportsType( Type type )

Plugins/RuntimeInspector/Scripts/RuntimeInspector/Helpers/DraggedReferenceItem.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using UnityEngine;
22
using UnityEngine.EventSystems;
3+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
4+
using UnityEngine.InputSystem;
5+
#endif
36

47
namespace RuntimeInspectorNamespace
58
{
@@ -8,8 +11,31 @@ public class DraggedReferenceItem : PopupBase, IDragHandler, IEndDragHandler
811
private Object m_reference;
912
public Object Reference { get { return m_reference; } }
1013

14+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
15+
// In new Input System, it is just not possible to change a PointerEventData's pointerDrag and dragging variables inside Update/LateUpdate,
16+
// EventSystemUIInputModule will reverse these changes immediately. So we'll allow only a single DraggedReferenceItem
17+
// with the new Input System and track its PointerEventData manually using Pointer.current
18+
internal static DraggedReferenceItem InstanceItem { get; private set; }
19+
20+
private readonly System.Collections.Generic.List<RaycastResult> hoveredUIElements = new System.Collections.Generic.List<RaycastResult>( 4 );
21+
#endif
22+
1123
public void SetContent( Object reference, PointerEventData draggingPointer )
1224
{
25+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
26+
if( InstanceItem )
27+
InstanceItem.DestroySelf();
28+
29+
InstanceItem = this;
30+
31+
draggingPointer = new PointerEventData( EventSystem.current )
32+
{
33+
pressPosition = draggingPointer.pressPosition,
34+
position = draggingPointer.position,
35+
delta = draggingPointer.delta
36+
};
37+
#endif
38+
1339
m_reference = reference;
1440
label.text = reference.GetNameWithType();
1541

@@ -19,8 +45,46 @@ public void SetContent( Object reference, PointerEventData draggingPointer )
1945
SetPointer( draggingPointer );
2046
}
2147

48+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
49+
private void LateUpdate()
50+
{
51+
if( Pointer.current == null || !Pointer.current.press.isPressed )
52+
{
53+
// We must execute OnDrop manually
54+
if( EventSystem.current )
55+
{
56+
hoveredUIElements.Clear();
57+
EventSystem.current.RaycastAll( pointer, hoveredUIElements );
58+
59+
int i = 0;
60+
while( i < hoveredUIElements.Count && !ExecuteEvents.ExecuteHierarchy( hoveredUIElements[i].gameObject, pointer, ExecuteEvents.dropHandler ) )
61+
i++;
62+
}
63+
64+
OnEndDrag( pointer );
65+
}
66+
else
67+
{
68+
Vector2 pointerPos = Pointer.current.position.ReadValue();
69+
Vector2 pointerDelta = pointerPos - pointer.position;
70+
if( pointerDelta.x != 0f || pointerDelta.y != 0f )
71+
{
72+
pointer.position = pointerPos;
73+
pointer.delta = pointerDelta;
74+
75+
OnDrag( pointer );
76+
}
77+
}
78+
}
79+
#endif
80+
2281
protected override void DestroySelf()
2382
{
83+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
84+
if( InstanceItem == this )
85+
InstanceItem = null;
86+
#endif
87+
2488
RuntimeInspectorUtils.PoolDraggedReferenceItem( this );
2589
}
2690

@@ -34,7 +98,7 @@ public void OnDrag( PointerEventData eventData )
3498

3599
public void OnEndDrag( PointerEventData eventData )
36100
{
37-
RuntimeInspectorUtils.PoolDraggedReferenceItem( this );
101+
DestroySelf();
38102
}
39103
}
40104
}

Plugins/RuntimeInspector/Scripts/RuntimeInspector/Helpers/DraggedReferenceSourceCamera.cs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System.Collections.Generic;
22
using UnityEngine;
33
using UnityEngine.EventSystems;
4+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
5+
using UnityEngine.InputSystem;
6+
#endif
47

58
namespace RuntimeInspectorNamespace
69
{
@@ -37,6 +40,10 @@ public class DraggedReferenceSourceCamera : MonoBehaviour
3740
private DraggedReferenceItem draggedReference;
3841
private PointerEventData draggingPointer;
3942

43+
#if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
44+
private readonly List<RaycastResult> hoveredUIElements = new List<RaycastResult>( 4 );
45+
#endif
46+
4047
public RaycastHitProcesserDelegate ProcessRaycastHit;
4148

4249
private void Awake()
@@ -46,83 +53,115 @@ private void Awake()
4653

4754
private void Update()
4855
{
56+
#if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
57+
// On new Input System, DraggedReferenceItem's PointerEventData is tracked by DraggedReferenceItem itself, not this component
4958
if( draggingPointer != null )
5059
{
5160
if( !draggedReference || !draggedReference.gameObject.activeSelf )
5261
draggingPointer = null;
53-
else if( Input.GetMouseButtonUp( 0 ) )
62+
else if( IsPointerHeld() )
63+
{
64+
draggingPointer.position = GetPointerPosition();
65+
ExecuteEvents.Execute( draggedReference.gameObject, draggingPointer, ExecuteEvents.dragHandler );
66+
}
67+
else
5468
{
5569
ExecuteEvents.Execute( draggedReference.gameObject, draggingPointer, ExecuteEvents.endDragHandler );
5670
if( EventSystem.current != null )
5771
{
58-
List<RaycastResult> hoveredUIElements = new List<RaycastResult>();
72+
hoveredUIElements.Clear();
5973
EventSystem.current.RaycastAll( draggingPointer, hoveredUIElements );
6074

6175
int i = 0;
62-
while( i < hoveredUIElements.Count && ExecuteEvents.ExecuteHierarchy( hoveredUIElements[i].gameObject, draggingPointer, ExecuteEvents.dropHandler ) == null )
76+
while( i < hoveredUIElements.Count && !ExecuteEvents.ExecuteHierarchy( hoveredUIElements[i].gameObject, draggingPointer, ExecuteEvents.dropHandler ) )
6377
i++;
6478
}
6579

6680
draggingPointer = null;
6781
}
68-
else
69-
{
70-
draggingPointer.position = Input.mousePosition;
71-
ExecuteEvents.Execute( draggedReference.gameObject, draggingPointer, ExecuteEvents.dragHandler );
72-
}
7382
}
7483
else
84+
#endif
7585
{
7686
if( !pointerDown )
7787
{
78-
if( Input.GetMouseButtonDown( 0 ) && EventSystem.current && !EventSystem.current.IsPointerOverGameObject() )
88+
if( IsPointerDown() && EventSystem.current && !EventSystem.current.IsPointerOverGameObject() )
7989
{
8090
RaycastHit hit;
81-
if( Physics.Raycast( _camera.ScreenPointToRay( Input.mousePosition ), out hit, raycastRange, interactableObjectsMask ) )
91+
if( Physics.Raycast( _camera.ScreenPointToRay( GetPointerPosition() ), out hit, raycastRange, interactableObjectsMask ) )
8292
{
8393
hitObject = ( ProcessRaycastHit != null ) ? ProcessRaycastHit( hit ) : hit.collider.gameObject;
8494
if( hitObject )
8595
{
8696
pointerDown = true;
8797
pointerDownTime = Time.realtimeSinceStartup;
88-
pointerDownPos = Input.mousePosition;
98+
pointerDownPos = GetPointerPosition();
8999
}
90100
}
91101
}
92102
}
93103
else
94104
{
95-
if( Input.GetMouseButton( 0 ) )
105+
if( IsPointerHeld() )
96106
{
97-
if( ( (Vector2) Input.mousePosition - pointerDownPos ).sqrMagnitude >= 100f )
107+
if( ( GetPointerPosition() - pointerDownPos ).sqrMagnitude >= 100f )
98108
pointerDown = false;
99109
else if( Time.realtimeSinceStartup - pointerDownTime >= holdTime )
100110
{
101111
pointerDown = false;
102112

103-
if( hitObject && EventSystem.current != null )
113+
if( hitObject && EventSystem.current )
104114
{
105115
draggingPointer = new PointerEventData( EventSystem.current )
106116
{
117+
#if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
107118
pointerId = Input.touchCount > 0 ? Input.GetTouch( 0 ).fingerId : -1,
108-
pressPosition = Input.mousePosition,
109-
position = Input.mousePosition,
119+
#endif
120+
pressPosition = GetPointerPosition(),
121+
position = GetPointerPosition(),
110122
button = PointerEventData.InputButton.Left
111123
};
112124

113125
draggedReference = RuntimeInspectorUtils.CreateDraggedReferenceItem( hitObject, draggingPointer, draggedReferenceSkin, draggedReferenceCanvas );
114-
if( draggedReference == null )
126+
if( !draggedReference )
115127
{
116128
pointerDown = false;
117129
draggingPointer = null;
118130
}
119131
}
120132
}
121133
}
122-
else if( Input.GetMouseButtonUp( 0 ) )
134+
else
123135
pointerDown = false;
124136
}
125137
}
126138
}
139+
140+
private bool IsPointerDown()
141+
{
142+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
143+
return Pointer.current != null && Pointer.current.press.wasPressedThisFrame;
144+
#else
145+
return Input.GetMouseButtonDown( 0 );
146+
#endif
147+
}
148+
149+
private bool IsPointerHeld()
150+
{
151+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
152+
return Pointer.current != null && Pointer.current.press.isPressed;
153+
#else
154+
return Input.GetMouseButton( 0 );
155+
#endif
156+
}
157+
158+
private Vector2 GetPointerPosition()
159+
{
160+
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
161+
return Pointer.current != null ? Pointer.current.position.ReadValue() : Vector2.zero;
162+
#else
163+
return Input.mousePosition;
164+
#endif
165+
}
127166
}
128167
}

0 commit comments

Comments
 (0)