Skip to content

Commit

Permalink
Fix bugs that can occur when destroying objects as they are being man…
Browse files Browse the repository at this point in the history
…ipulated;

Fix exceptions that can occur if the controller is hovering an object on the first frame
  • Loading branch information
mtschoen-unity committed Aug 13, 2020
1 parent fa2d7a6 commit 6abe169
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Runtime/Scripts/Core/EditorXRDirectSelectionModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ void OnObjectsDropped(Transform rayOrigin, Transform[] grabbedObjects)
var eventObjects = new List<Transform>();
foreach (var grabbedObject in grabbedObjects)
{
// Avoid exceptions for destroyed objects
if (grabbedObject == null)
continue;

objects.Remove(grabbedObject);

if (m_ViewerModule != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ public bool Raycast(Ray ray, out RaycastHit hit, out GameObject obj, float maxDi
if (ignoreList != null && ignoreList.Contains(renderer.gameObject))
continue;

// Skip destroyed objects
if (renderer == null)
continue;

var transform = renderer.transform;

IntersectionUtils.SetupCollisionTester(m_CollisionTester, transform);
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Tools/SelectionTool/SelectionTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public void ProcessInput(ActionMapInput input, ConsumeControlDelegate consumeCon

// Only overwrite an existing selection if it does not contain the hovered object
// In the case of multi-select, only add, do not remove
if (selectionTool.m_SelectionInput.select.wasJustPressed && !Selection.objects.Contains(directHoveredObject))
if (selectionTool.m_SelectionInput != null && selectionTool.m_SelectionInput.select.wasJustPressed && !Selection.objects.Contains(directHoveredObject))
this.SelectObject(directHoveredObject, directRayOrigin, m_MultiSelect);

m_HoverGameObjects[directRayOrigin] = directHoveredObject;
Expand Down
42 changes: 42 additions & 0 deletions Runtime/Tools/TransformTool/TransformTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public void Reset()
{
var grabbedTransform = grabbedTransforms[i];

// Avoid exceptions for destroyed objects
if (grabbedTransform == null)
continue;

var inverseRotation = Quaternion.Inverse(rayOrigin.rotation);
m_PositionOffsets[i] = inverseRotation * (grabbedTransform.position - pivot);
m_RotationOffsets[i] = inverseRotation * grabbedTransform.rotation;
Expand Down Expand Up @@ -252,6 +256,11 @@ public void Cancel()
for (var i = 0; i < length; i++)
{
var grabbedTransform = grabbedTransforms[i];

// Avoid exceptions for destroyed objects
if (grabbedTransform == null)
continue;

grabbedTransform.position = m_OriginalPositions[i];
grabbedTransform.rotation = m_OriginalRotations[i];
grabbedTransform.localScale = m_OriginalScales[i];
Expand Down Expand Up @@ -524,6 +533,8 @@ public void ProcessInput(ActionMapInput input, ConsumeControlDelegate consumeCon
}

var transformInput = transformTool.m_Input;
if (transformInput == null)
continue;

if (transformInput.select.wasJustPressed)
{
Expand Down Expand Up @@ -622,6 +633,37 @@ public void ProcessInput(ActionMapInput input, ConsumeControlDelegate consumeCon
}
}

// Check if objects were destroyed in drop handler, or since last frame
if (hasLeft)
{
// Drop all objects in left hand if any of them have been destroyed
foreach (var grabbedTransform in m_LeftGrabData.grabbedTransforms)
{
if (grabbedTransform == null)
{
m_LeftGrabData.Cancel();
DropHeldObjects(Node.LeftHand);
hasLeft = false;
break;
}
}
}

if (hasRight)
{
// Drop all objects in right hand if any of them have been destroyed
foreach (var grabbedTransform in m_RightGrabData.grabbedTransforms)
{
if (grabbedTransform == null)
{
m_RightGrabData.Cancel();
DropHeldObjects(Node.RightHand);
hasRight = false;
break;
}
}
}

if (hasLeft && hasRight && leftHeld && rightHeld && m_Scaling) // Two-handed scaling
{
var rightRayOrigin = m_RightGrabData.rayOrigin;
Expand Down

0 comments on commit 6abe169

Please sign in to comment.