9
9
namespace Microsoft . MixedReality . Toolkit . UI . BoundsControl
10
10
{
11
11
/// <summary>
12
- /// Rotation handles for <see cref="BoundsControl"/> that are used for rotating the
12
+ /// Handles for <see cref="BoundsControl"/> that are used for manipulating the
13
13
/// Gameobject BoundsControl is attached to with near or far interaction
14
14
/// </summary>
15
15
public abstract class PerAxisHandles : HandlesBase
@@ -78,14 +78,14 @@ private void UpdateColliderType()
78
78
if ( oldBoxCollider != null && config . HandlePrefabColliderType == HandlePrefabCollider . Sphere )
79
79
{
80
80
shouldCreateNewCollider = true ;
81
- Object . Destroy ( oldBoxCollider ) ;
81
+ Object . DestroyImmediate ( oldBoxCollider ) ;
82
82
}
83
83
84
84
var oldSphereCollider = handle . GetComponent < SphereCollider > ( ) ;
85
85
if ( oldSphereCollider != null && config . HandlePrefabColliderType == HandlePrefabCollider . Box )
86
86
{
87
87
shouldCreateNewCollider = true ;
88
- Object . Destroy ( oldSphereCollider ) ;
88
+ Object . DestroyImmediate ( oldSphereCollider ) ;
89
89
}
90
90
91
91
if ( shouldCreateNewCollider )
@@ -185,10 +185,12 @@ private void CreateHandles(Transform parent)
185
185
handle . transform . position = HandlePositions [ i ] ;
186
186
handle . transform . parent = parent ;
187
187
188
- Bounds midpointBounds = CreateVisual ( i , handle ) ;
189
- float maxDim = VisualUtils . GetMaxComponent ( midpointBounds . size ) ;
188
+ Bounds handleVisualBounds = CreateVisual ( i , handle ) ;
189
+ float maxDim = VisualUtils . GetMaxComponent ( handleVisualBounds . size ) ;
190
190
float invScale = maxDim == 0.0f ? 0.0f : config . HandleSize / maxDim ;
191
- VisualUtils . AddComponentsToAffordance ( handle , new Bounds ( midpointBounds . center * invScale , midpointBounds . size * invScale ) ,
191
+
192
+ // TODO: Some subclasses of PerAxisHandles shouldn't use CursorContextInfo.CursorAction.Rotate
193
+ VisualUtils . AddComponentsToAffordance ( handle , new Bounds ( handleVisualBounds . center * invScale , handleVisualBounds . size * invScale ) ,
192
194
config . HandlePrefabColliderType , CursorContextInfo . CursorAction . Rotate , config . ColliderPadding , parent , config . DrawTetherWhenManipulating ) ;
193
195
194
196
handles . Add ( handle . transform ) ;
@@ -209,11 +211,11 @@ protected override void RecreateVisuals()
209
211
{
210
212
// get old child and remove it
211
213
obsoleteChild . parent = null ;
212
- Object . Destroy ( obsoleteChild . gameObject ) ;
214
+ Object . DestroyImmediate ( obsoleteChild . gameObject ) ;
213
215
}
214
216
else
215
217
{
216
- Debug . LogError ( "couldn 't find rotation visual on recreating visuals" ) ;
218
+ Debug . LogError ( "Couldn 't find handle visual on recreating visuals" ) ;
217
219
}
218
220
219
221
// create new visual
@@ -247,40 +249,42 @@ protected override void UpdateColliderBounds(Transform handle, Vector3 visualSiz
247
249
248
250
private Bounds CreateVisual ( int handleIndex , GameObject parent )
249
251
{
250
- GameObject midpointVisual ;
252
+ GameObject handleVisual ;
251
253
GameObject prefabType = config . HandlePrefab ;
252
254
if ( prefabType != null )
253
255
{
254
- midpointVisual = Object . Instantiate ( prefabType ) ;
256
+ handleVisual = Object . Instantiate ( prefabType ) ;
255
257
}
256
258
else
257
259
{
258
- midpointVisual = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
259
- // deactivate collider on visuals and register for deletion - actual collider
260
- // of handle is attached to the handle gameobject, not the visual
261
- var collider = midpointVisual . GetComponent < SphereCollider > ( ) ;
260
+ handleVisual = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
261
+ // We only want the Primitive sphere mesh, but CreatePrimitive will
262
+ // give us a sphere collider too. Remove the sphere collider here
263
+ // so we can manually add our own properly configured collider later.
264
+ var collider = handleVisual . GetComponent < SphereCollider > ( ) ;
262
265
collider . enabled = false ;
263
- Object . Destroy ( collider ) ;
264
- }
265
-
266
- Quaternion realignment = GetRotationRealignment ( handleIndex ) ;
267
- midpointVisual . transform . localRotation = realignment * midpointVisual . transform . localRotation ;
266
+ Object . DestroyImmediate ( collider ) ;
267
+ }
268
268
269
- Bounds midpointBounds = VisualUtils . GetMaxBounds ( midpointVisual ) ;
270
- float maxDim = VisualUtils . GetMaxComponent ( midpointBounds . size ) ;
269
+ // handleVisualBounds are returned in handleVisual-local space.
270
+ Bounds handleVisualBounds = VisualUtils . GetMaxBounds ( handleVisual ) ;
271
+ float maxDim = VisualUtils . GetMaxComponent ( handleVisualBounds . size ) ;
271
272
float invScale = maxDim == 0.0f ? 0.0f : config . HandleSize / maxDim ;
272
273
273
- midpointVisual . name = visualsName ;
274
- midpointVisual . transform . parent = parent . transform ;
275
- midpointVisual . transform . localScale = new Vector3 ( invScale , invScale , invScale ) ;
276
- midpointVisual . transform . localPosition = Vector3 . zero ;
274
+ handleVisual . name = visualsName ;
275
+ handleVisual . transform . parent = parent . transform ;
276
+ handleVisual . transform . localScale = new Vector3 ( invScale , invScale , invScale ) ;
277
+ handleVisual . transform . localPosition = Vector3 . zero ;
278
+
279
+ Quaternion realignment = GetRotationRealignment ( handleIndex ) ;
280
+ parent . transform . localRotation = realignment ;
277
281
278
282
if ( config . HandleMaterial != null )
279
283
{
280
- VisualUtils . ApplyMaterialToAllRenderers ( midpointVisual , config . HandleMaterial ) ;
284
+ VisualUtils . ApplyMaterialToAllRenderers ( handleVisual , config . HandleMaterial ) ;
281
285
}
282
286
283
- return midpointBounds ;
287
+ return handleVisualBounds ;
284
288
}
285
289
286
290
#region BoundsControlHandlerBase
0 commit comments