@@ -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