Skip to content

Commit 621c8d9

Browse files
author
David Kline
authored
Merge pull request #3794 from Railboy/mrtk_pressable_button_inspector
PressableButton Inspector Improvement
2 parents f631a5a + 2538ff8 commit 621c8d9

File tree

6 files changed

+340
-104
lines changed

6 files changed

+340
-104
lines changed

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Prefabs/PressableButton.prefab

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Transform:
113113
- {fileID: 1019827636}
114114
- {fileID: 1184125596}
115115
- {fileID: 8608155427408040960}
116+
- {fileID: 4932697658088180369}
116117
m_Father: {fileID: 0}
117118
m_RootOrder: 0
118119
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -146,6 +147,7 @@ MonoBehaviour:
146147
pressDistance: 0.006
147148
releaseDistanceDelta: 0.001
148149
returnRate: 25
150+
initialTransform: {fileID: 0}
149151
TouchBegin:
150152
m_PersistentCalls:
151153
m_Calls:
@@ -234,6 +236,8 @@ MonoBehaviour:
234236
m_CallState: 2
235237
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine.CoreModule, Version=0.0.0.0,
236238
Culture=neutral, PublicKeyToken=null
239+
isTouching: 0
240+
isPressing: 0
237241
--- !u!114 &316800720
238242
MonoBehaviour:
239243
m_ObjectHideFlags: 0
@@ -413,7 +417,7 @@ MonoBehaviour:
413417
eventsToReceive: 0
414418
touchableSurface: 0
415419
bounds: {x: 0.032, y: 0.032}
416-
collider: {fileID: 316800722}
420+
touchableCollider: {fileID: 316800722}
417421
--- !u!1 &937783100
418422
GameObject:
419423
m_ObjectHideFlags: 0
@@ -1112,6 +1116,36 @@ Transform:
11121116
m_Father: {fileID: 1944713263}
11131117
m_RootOrder: 2
11141118
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1119+
--- !u!1 &1919625447641687090
1120+
GameObject:
1121+
m_ObjectHideFlags: 0
1122+
m_CorrespondingSourceObject: {fileID: 0}
1123+
m_PrefabInstance: {fileID: 0}
1124+
m_PrefabAsset: {fileID: 0}
1125+
serializedVersion: 6
1126+
m_Component:
1127+
- component: {fileID: 4932697658088180369}
1128+
m_Layer: 0
1129+
m_Name: Initial Marker
1130+
m_TagString: Untagged
1131+
m_Icon: {fileID: 0}
1132+
m_NavMeshLayer: 0
1133+
m_StaticEditorFlags: 0
1134+
m_IsActive: 1
1135+
--- !u!4 &4932697658088180369
1136+
Transform:
1137+
m_ObjectHideFlags: 0
1138+
m_CorrespondingSourceObject: {fileID: 0}
1139+
m_PrefabInstance: {fileID: 0}
1140+
m_PrefabAsset: {fileID: 0}
1141+
m_GameObject: {fileID: 1919625447641687090}
1142+
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
1143+
m_LocalPosition: {x: 0, y: 0, z: 0}
1144+
m_LocalScale: {x: 1, y: 1, z: 1}
1145+
m_Children: []
1146+
m_Father: {fileID: 1944713263}
1147+
m_RootOrder: 3
1148+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
11151149
--- !u!1 &4787196022339850713
11161150
GameObject:
11171151
m_ObjectHideFlags: 0

Assets/MixedRealityToolkit.SDK/Features/UX/Scripts/PressableButtons/PressableButton.cs

Lines changed: 24 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace Microsoft.MixedReality.Toolkit.UI
1717
[RequireComponent(typeof(BoxCollider))]
1818
public class PressableButton : MonoBehaviour, IMixedRealityTouchHandler
1919
{
20+
const string InitialMarkerTransformName = "Initial Marker";
21+
2022
[SerializeField]
2123
[Tooltip("The object that is being pushed.")]
2224
private GameObject movingButtonVisuals = null;
@@ -40,6 +42,11 @@ public class PressableButton : MonoBehaviour, IMixedRealityTouchHandler
4042
[Tooltip("Speed of the object movement on release.")]
4143
private float returnRate = 25.0f;
4244

45+
[Header("Position markers")]
46+
[Tooltip("Used to mark where button movement begins. If null, it will be automatically generated.")]
47+
[SerializeField]
48+
private Transform initialTransform;
49+
4350
[Header("Events")]
4451
public UnityEvent TouchBegin;
4552
public UnityEvent TouchEnd;
@@ -54,10 +61,14 @@ public class PressableButton : MonoBehaviour, IMixedRealityTouchHandler
5461
private float currentPushDistance = 0.0f;
5562

5663
private List<Vector3> touchPoints = new List<Vector3>();
57-
private Transform initialTransform;
58-
private BoxCollider boxCollider;
5964

65+
[Header("Button State")]
66+
[ReadOnly]
67+
[SerializeField]
6068
private bool isTouching = false;
69+
70+
[ReadOnly]
71+
[SerializeField]
6172
private bool isPressing = false;
6273

6374
///<summary>
@@ -108,22 +119,6 @@ private Vector3 WorldSpacePressDirection
108119

109120
#endregion
110121

111-
private void Awake()
112-
{
113-
boxCollider = gameObject.EnsureComponent<BoxCollider>();
114-
}
115-
116-
private void OnEnable()
117-
{
118-
boxCollider.enabled = true;
119-
}
120-
121-
private void OnDisable()
122-
{
123-
IsTouching = false;
124-
boxCollider.enabled = false;
125-
}
126-
127122
private void Start()
128123
{
129124
if (gameObject.layer == 2)
@@ -157,72 +152,10 @@ private void Update()
157152

158153
UpdateMovingVisualsPosition();
159154
}
160-
else
161-
{
162-
ClearPathMarkers();
163-
}
164155

165156
touchPoints.Clear();
166157
}
167158

168-
///<summary>
169-
/// Handles drawing some editor visual elements to give you an idea of the movement and size of the button.
170-
///</summary>
171-
void OnDrawGizmos()
172-
{
173-
var collider = GetComponent<Collider>();
174-
if (collider != null)
175-
{
176-
Vector3 worldPressDirection = WorldSpacePressDirection;
177-
178-
Vector3 boundsCenter = collider.bounds.center;
179-
Vector3 startPoint;
180-
if (movingButtonVisuals != null)
181-
{
182-
startPoint = movingButtonVisuals.transform.position;
183-
}
184-
else
185-
{
186-
startPoint = transform.position;
187-
}
188-
float distance;
189-
startPoint = ProjectPointToRay(boundsCenter, worldPressDirection, startPoint, out distance);
190-
191-
Vector3 endPoint = startPoint + worldPressDirection * maxPushDistance;
192-
Vector3 pushedPoint = startPoint + worldPressDirection * currentPushDistance;
193-
194-
Gizmos.color = Color.magenta;
195-
Gizmos.DrawLine(startPoint, pushedPoint);
196-
Vector3 lastPoint = pushedPoint;
197-
198-
float releaseDistance = pressDistance - releaseDistanceDelta;
199-
if (releaseDistance > currentPushDistance)
200-
{
201-
Gizmos.color = Color.yellow;
202-
Vector3 releasePoint = startPoint + worldPressDirection * releaseDistance;
203-
Gizmos.DrawLine(lastPoint, releasePoint);
204-
lastPoint = releasePoint;
205-
}
206-
207-
if (pressDistance > currentPushDistance)
208-
{
209-
Gizmos.color = Color.cyan;
210-
Vector3 pressPoint = startPoint + worldPressDirection * pressDistance;
211-
Gizmos.DrawLine(lastPoint, pressPoint);
212-
lastPoint = pressPoint;
213-
}
214-
215-
Gizmos.color = Color.yellow;
216-
Gizmos.DrawLine(lastPoint, endPoint);
217-
218-
Gizmos.color = Color.yellow;
219-
Gizmos.DrawLine(endPoint, endPoint + transform.rotation * Vector3.up * collider.bounds.extents.y);
220-
Gizmos.DrawLine(endPoint, endPoint - transform.rotation * Vector3.up * collider.bounds.extents.y);
221-
Gizmos.DrawLine(endPoint, endPoint + transform.rotation * Vector3.right * collider.bounds.extents.x);
222-
Gizmos.DrawLine(endPoint, endPoint - transform.rotation * Vector3.right * collider.bounds.extents.x);
223-
}
224-
}
225-
226159
#region OnTouch
227160

228161
void IMixedRealityTouchHandler.OnTouchStarted(HandTrackingInputEventData eventData)
@@ -231,7 +164,7 @@ void IMixedRealityTouchHandler.OnTouchStarted(HandTrackingInputEventData eventDa
231164

232165
if (initialTransform == null)
233166
{
234-
SetPathMarkers();
167+
FindOrCreatePathMarkers();
235168
// Make sure to initialize currentPushDistance now to correctly handle back-presses in
236169
// HandlePressProgress().
237170
currentPushDistance = GetFarthestPushDistanceAlongButtonAxis();
@@ -257,44 +190,32 @@ void IMixedRealityTouchHandler.OnTouchStarted(HandTrackingInputEventData eventDa
257190
void IMixedRealityTouchHandler.OnTouchUpdated(HandTrackingInputEventData eventData)
258191
{
259192
touchPoints.Add(eventData.InputData);
260-
eventData.Use();
261193
}
262194

263195
void IMixedRealityTouchHandler.OnTouchCompleted(HandTrackingInputEventData eventData)
264196
{
265-
eventData.Use();
266197
}
267198

268199
#endregion OnTouch
269200

270201
#region private Methods
271202

272-
private void SetPathMarkers()
203+
public void FindOrCreatePathMarkers()
273204
{
274-
GameObject initialMarker = new GameObject("Initial");
275-
initialMarker.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
205+
Transform sourcePositionTransform = (movingButtonVisuals != null) ? movingButtonVisuals.transform : transform;
276206

277-
if (movingButtonVisuals != null)
278-
{
279-
initialMarker.transform.position = movingButtonVisuals.transform.position;
280-
initialMarker.transform.parent = movingButtonVisuals.transform.parent;
281-
}
282-
else
207+
// First try to search for our markers
208+
if (initialTransform == null)
283209
{
284-
initialMarker.transform.position = transform.position;
285-
initialMarker.transform.parent = transform.parent;
210+
initialTransform = transform.Find(InitialMarkerTransformName);
286211
}
287212

288-
initialTransform = initialMarker.transform;
289-
}
290-
291-
private void ClearPathMarkers()
292-
{
293-
if (initialTransform != null)
213+
// If we don't find them, create them
214+
if (initialTransform == null)
294215
{
295-
initialTransform.parent = null;
296-
DestroyImmediate(initialTransform.gameObject);
297-
initialTransform = null;
216+
initialTransform = new GameObject(InitialMarkerTransformName).transform;
217+
initialTransform.parent = transform;
218+
initialTransform.position = sourcePositionTransform.position;
298219
}
299220
}
300221

0 commit comments

Comments
 (0)