Skip to content

Commit

Permalink
refactor(Structure): replace occurrences of Equals with ==
Browse files Browse the repository at this point in the history
Unity3d creates garbage when dealing with `Equals` as it's considered
a Boxing method and therefore is less efficient than simply comparing
with `==`

This information can be found at: goo.gl/apMyGU

This refactor replaces all of the occurrences of `Equals` with `==`
to provide the efficiency improvements.

The MoveInPlace script has also been slightly refactored to use a
`switch` statement rather than a collection of `if/else` statements
that all in turn used the `Equals` method as well.
  • Loading branch information
thestonefox committed Aug 17, 2017
1 parent 0573576 commit bf90d21
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void Update()
fired = false;
fireOffset = Time.time;
}
if (!releaseRotation.Equals(baseRotation))
if (releaseRotation != baseRotation)
{
transform.localRotation = Quaternion.Lerp(releaseRotation, baseRotation, (Time.time - fireOffset) * 8);
}
Expand Down Expand Up @@ -161,7 +161,7 @@ private void PullString()
currentPull = Mathf.Clamp((Vector3.Distance(holdControl.transform.position, stringControl.transform.position) - pullOffset) * pullMultiplier, 0, maxPullDistance);
bowAnimation.SetFrame(currentPull);

if (!currentPull.ToString("F2").Equals(previousPull.ToString("F2")))
if (currentPull.ToString("F2") != previousPull.ToString("F2"))
{
VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(holdControl.gameObject), bowVibration);
VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(stringControl.gameObject), stringVibration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ protected virtual IEnumerator DoDestinationMarkerSetAtEndOfFrame(DestinationMark

protected virtual void ToggleCursor(object sender, bool state)
{
if ((hidePointerCursorOnHover || hideDirectionIndicatorOnHover) && sender.GetType().Equals(typeof(VRTK_Pointer)))
if ((hidePointerCursorOnHover || hideDirectionIndicatorOnHover) && sender.GetType() == typeof(VRTK_Pointer))
{
VRTK_Pointer pointer = (VRTK_Pointer)sender;
if (pointer != null && pointer.pointerRenderer != null)
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRTK/SDK/Base/SDK_BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ protected virtual bool CheckControllerLeftHand(GameObject controller, bool actua
VRTK_SDKManager sdkManager = VRTK_SDKManager.instance;
if (sdkManager != null && controller != null)
{
return (actual ? controller.Equals(sdkManager.loadedSetup.actualLeftController) : controller.Equals(sdkManager.scriptAliasLeftController));
return (actual ? controller == sdkManager.loadedSetup.actualLeftController : controller == sdkManager.scriptAliasLeftController);
}
return false;
}
Expand All @@ -389,7 +389,7 @@ protected virtual bool CheckControllerRightHand(GameObject controller, bool actu
VRTK_SDKManager sdkManager = VRTK_SDKManager.instance;
if (sdkManager != null && controller != null)
{
return (actual ? controller.Equals(sdkManager.loadedSetup.actualRightController) : controller.Equals(sdkManager.scriptAliasRightController));
return (actual ? controller == sdkManager.loadedSetup.actualRightController : controller == sdkManager.scriptAliasRightController);
}
return false;
}
Expand Down
12 changes: 6 additions & 6 deletions Assets/VRTK/Scripts/Controls/3D/VRTK_Slider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,47 +86,47 @@ protected override bool DetectSetup()
Vector3 sliderDiff = transform.localScale / 2f;

//The right ray has found the min on the right, so max is on the left
if (rightHasHit && hitRight.collider.gameObject.Equals(minimumLimit.gameObject))
if (rightHasHit && hitRight.collider.gameObject == minimumLimit.gameObject)
{
finalDirection = Direction.x;
minimumLimitDiff = CalculateDiff(minimumLimit.transform.localPosition, Vector3.right, minimumLimit.transform.localScale.x, sliderDiff.x, false);
maximumLimitDiff = CalculateDiff(maximumLimit.transform.localPosition, Vector3.right, maximumLimit.transform.localScale.x, sliderDiff.x, true);
}

//The right ray has found the max on the right, so min is on the left
if (rightHasHit && hitRight.collider.gameObject.Equals(maximumLimit.gameObject))
if (rightHasHit && hitRight.collider.gameObject == maximumLimit.gameObject)
{
finalDirection = Direction.x;
minimumLimitDiff = CalculateDiff(minimumLimit.transform.localPosition, Vector3.right, minimumLimit.transform.localScale.x, sliderDiff.x, true);
maximumLimitDiff = CalculateDiff(maximumLimit.transform.localPosition, Vector3.right, maximumLimit.transform.localScale.x, sliderDiff.x, false);
}

// the up ray has found the min above, so max is below
if (upHasHit && hitUp.collider.gameObject.Equals(minimumLimit.gameObject))
if (upHasHit && hitUp.collider.gameObject == minimumLimit.gameObject)
{
finalDirection = Direction.y;
minimumLimitDiff = CalculateDiff(minimumLimit.transform.localPosition, Vector3.up, minimumLimit.transform.localScale.y, sliderDiff.y, false);
maximumLimitDiff = CalculateDiff(maximumLimit.transform.localPosition, Vector3.up, maximumLimit.transform.localScale.y, sliderDiff.y, true);
}

//the up ray has found the max above, so the min ix below
if (upHasHit && hitUp.collider.gameObject.Equals(maximumLimit.gameObject))
if (upHasHit && hitUp.collider.gameObject == maximumLimit.gameObject)
{
finalDirection = Direction.y;
minimumLimitDiff = CalculateDiff(minimumLimit.transform.localPosition, Vector3.up, minimumLimit.transform.localScale.y, sliderDiff.y, true);
maximumLimitDiff = CalculateDiff(maximumLimit.transform.localPosition, Vector3.up, maximumLimit.transform.localScale.y, sliderDiff.y, false);
}

//the forward ray has found the min in front, so the max is behind
if (forwardHasHit && hitForward.collider.gameObject.Equals(minimumLimit.gameObject))
if (forwardHasHit && hitForward.collider.gameObject == minimumLimit.gameObject)
{
finalDirection = Direction.z;
minimumLimitDiff = CalculateDiff(minimumLimit.transform.localPosition, Vector3.forward, minimumLimit.transform.localScale.y, sliderDiff.y, false);
maximumLimitDiff = CalculateDiff(maximumLimit.transform.localPosition, Vector3.forward, maximumLimit.transform.localScale.y, sliderDiff.y, true);
}

//the forward ray has found the max in front, so the min is behind
if (forwardHasHit && hitForward.collider.gameObject.Equals(maximumLimit.gameObject))
if (forwardHasHit && hitForward.collider.gameObject == maximumLimit.gameObject)
{
finalDirection = Direction.z;
minimumLimitDiff = CalculateDiff(minimumLimit.transform.localPosition, Vector3.forward, minimumLimit.transform.localScale.z, sliderDiff.z, true);
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRTK/Scripts/Interactions/VRTK_InteractGrab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ protected virtual void ControllerUntouchInteractableObject(object sender, Object

protected virtual void ManageGrabListener(bool state)
{
if (controllerEvents != null && subscribedGrabButton != VRTK_ControllerEvents.ButtonAlias.Undefined && (!state || !grabButton.Equals(subscribedGrabButton)))
if (controllerEvents != null && subscribedGrabButton != VRTK_ControllerEvents.ButtonAlias.Undefined && (!state || grabButton != subscribedGrabButton))
{
controllerEvents.UnsubscribeToButtonAliasEvent(subscribedGrabButton, true, DoGrabObject);
controllerEvents.UnsubscribeToButtonAliasEvent(subscribedGrabButton, false, DoReleaseObject);
subscribedGrabButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
}

if (controllerEvents != null && state && grabButton != VRTK_ControllerEvents.ButtonAlias.Undefined && !grabButton.Equals(subscribedGrabButton))
if (controllerEvents != null && state && grabButton != VRTK_ControllerEvents.ButtonAlias.Undefined && grabButton != subscribedGrabButton)
{
controllerEvents.SubscribeToButtonAliasEvent(grabButton, true, DoGrabObject);
controllerEvents.SubscribeToButtonAliasEvent(grabButton, false, DoReleaseObject);
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRTK/Scripts/Interactions/VRTK_InteractUse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ protected virtual void ControllerUntouchInteractableObject(object sender, Object

protected virtual void ManageUseListener(bool state)
{
if (controllerEvents != null && subscribedUseButton != VRTK_ControllerEvents.ButtonAlias.Undefined && (!state || !useButton.Equals(subscribedUseButton)))
if (controllerEvents != null && subscribedUseButton != VRTK_ControllerEvents.ButtonAlias.Undefined && (!state || useButton != subscribedUseButton))
{
controllerEvents.UnsubscribeToButtonAliasEvent(subscribedUseButton, true, DoStartUseObject);
controllerEvents.UnsubscribeToButtonAliasEvent(subscribedUseButton, false, DoStopUseObject);
subscribedUseButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
}

if (controllerEvents != null && state && useButton != VRTK_ControllerEvents.ButtonAlias.Undefined && !useButton.Equals(subscribedUseButton))
if (controllerEvents != null && state && useButton != VRTK_ControllerEvents.ButtonAlias.Undefined && useButton != subscribedUseButton)
{
controllerEvents.SubscribeToButtonAliasEvent(useButton, true, DoStartUseObject);
controllerEvents.SubscribeToButtonAliasEvent(useButton, false, DoStopUseObject);
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRTK/Scripts/Internal/VRTK_ControllerReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public bool Equals(VRTK_ControllerReference other)
{
return false;
}
return (index.Equals(other.index));
return (index == other.index);
}

protected virtual GameObject GetValidObjectFromIndex()
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRTK/Scripts/Internal/VRTK_VRInputModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected virtual bool CheckTransformTree(Transform target, Transform source)
return false;
}

if (target.Equals(source))
if (target == source)
{
return true;
}
Expand Down
107 changes: 46 additions & 61 deletions Assets/VRTK/Scripts/Locomotion/VRTK_MoveInPlace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public enum DirectionalMethod
protected Dictionary<Transform, List<float>> movementList;
protected Dictionary<Transform, float> previousYPositions;
// Used to determine the direction when using a decoupling method.
protected Vector3 initalGaze;
protected Vector3 initialGaze;
// The current move speed of the player. If Move In Place is not active, it will be set to 0.00f.
protected float currentSpeed;
// The current direction the player is moving. If Move In Place is not active, it will be set to Vector.zero.
Expand All @@ -124,13 +124,13 @@ public virtual void SetControlOptions(ControlOptions givenControlOptions)
controlOptions = givenControlOptions;
trackedObjects.Clear();

if (controllerLeftHand != null && controllerRightHand != null && (controlOptions.Equals(ControlOptions.HeadsetAndControllers) || controlOptions.Equals(ControlOptions.ControllersOnly)))
if (controllerLeftHand != null && controllerRightHand != null && (controlOptions == ControlOptions.HeadsetAndControllers || controlOptions == ControlOptions.ControllersOnly))
{
trackedObjects.Add(VRTK_DeviceFinder.GetActualController(controllerLeftHand).transform);
trackedObjects.Add(VRTK_DeviceFinder.GetActualController(controllerRightHand).transform);
}

if (headset != null && (controlOptions.Equals(ControlOptions.HeadsetAndControllers) || controlOptions.Equals(ControlOptions.HeadsetOnly)))
if (headset != null && (controlOptions == ControlOptions.HeadsetAndControllers || controlOptions == ControlOptions.HeadsetOnly))
{
trackedObjects.Add(headset.transform);
}
Expand Down Expand Up @@ -164,7 +164,7 @@ protected virtual void OnEnable()
trackedObjects = new List<Transform>();
movementList = new Dictionary<Transform, List<float>>();
previousYPositions = new Dictionary<Transform, float>();
initalGaze = Vector3.zero;
initialGaze = Vector3.zero;
direction = Vector3.zero;
previousDirection = Vector3.zero;
averagePeriod = 60;
Expand Down Expand Up @@ -310,70 +310,55 @@ protected virtual float CalculateListAverage()

protected virtual Vector3 SetDirection()
{
Vector3 returnDirection = Vector3.zero;
switch (directionMethod)
{
case DirectionalMethod.SmartDecoupling:
case DirectionalMethod.DumbDecoupling:
return CalculateCouplingDirection();
case DirectionalMethod.ControllerRotation:
return CalculateControllerRotationDirection(DetermineAverageControllerRotation() * Vector3.forward);
case DirectionalMethod.LeftControllerRotationOnly:
return CalculateControllerRotationDirection((controllerLeftHand != null ? controllerLeftHand.transform.rotation : Quaternion.identity) * Vector3.forward);
case DirectionalMethod.RightControllerRotationOnly:
return CalculateControllerRotationDirection((controllerRightHand != null ? controllerRightHand.transform.rotation : Quaternion.identity) * Vector3.forward);
case DirectionalMethod.EngageControllerRotationOnly:
return CalculateControllerRotationDirection((engagedController != null ? engagedController.scriptAlias.transform.rotation : Quaternion.identity) * Vector3.forward);
case DirectionalMethod.Gaze:
return new Vector3(headset.forward.x, 0, headset.forward.z);
}

return Vector2.zero;
}

protected virtual Vector3 CalculateCouplingDirection()
{
// If we haven't set an inital gaze yet, set it now.
// If we're doing dumb decoupling, this is what we'll be sticking with.
if (initialGaze == Vector3.zero)
{
initialGaze = new Vector3(headset.forward.x, 0, headset.forward.z);
}

// If we're doing a decoupling method...
if (directionMethod == DirectionalMethod.SmartDecoupling || directionMethod == DirectionalMethod.DumbDecoupling)
// If we're doing smart decoupling, check to see if we want to reset our distance.
if (directionMethod == DirectionalMethod.SmartDecoupling)
{
// If we haven't set an inital gaze yet, set it now.
// If we're doing dumb decoupling, this is what we'll be sticking with.
if (initalGaze.Equals(Vector3.zero))
bool closeEnough = true;
float curXDir = headset.rotation.eulerAngles.y;
if (curXDir <= smartDecoupleThreshold)
{
initalGaze = new Vector3(headset.forward.x, 0, headset.forward.z);
curXDir += 360;
}

// If we're doing smart decoupling, check to see if we want to reset our distance.
if (directionMethod == DirectionalMethod.SmartDecoupling)
closeEnough = closeEnough && (Mathf.Abs(curXDir - controllerLeftHand.transform.rotation.eulerAngles.y) <= smartDecoupleThreshold);
closeEnough = closeEnough && (Mathf.Abs(curXDir - controllerRightHand.transform.rotation.eulerAngles.y) <= smartDecoupleThreshold);

// If the controllers and the headset are pointing the same direction (within the threshold) reset the direction the player's moving.
if (closeEnough)
{
bool closeEnough = true;
float curXDir = headset.rotation.eulerAngles.y;
if (curXDir <= smartDecoupleThreshold)
{
curXDir += 360;
}

closeEnough = closeEnough && (Mathf.Abs(curXDir - controllerLeftHand.transform.rotation.eulerAngles.y) <= smartDecoupleThreshold);
closeEnough = closeEnough && (Mathf.Abs(curXDir - controllerRightHand.transform.rotation.eulerAngles.y) <= smartDecoupleThreshold);

// If the controllers and the headset are pointing the same direction (within the threshold) reset the direction the player's moving.
if (closeEnough)
{
initalGaze = new Vector3(headset.forward.x, 0, headset.forward.z);
}
initialGaze = new Vector3(headset.forward.x, 0, headset.forward.z);
}
returnDirection = initalGaze;
}
// if we're doing controller rotation movement
else if (directionMethod.Equals(DirectionalMethod.ControllerRotation))
{
Vector3 calculatedControllerDirection = DetermineAverageControllerRotation() * Vector3.forward;
returnDirection = CalculateControllerRotationDirection(calculatedControllerDirection);
}
// if we're doing left controller only rotation movement
else if (directionMethod.Equals(DirectionalMethod.LeftControllerRotationOnly))
{
Vector3 calculatedControllerDirection = (controllerLeftHand != null ? controllerLeftHand.transform.rotation : Quaternion.identity) * Vector3.forward;
returnDirection = CalculateControllerRotationDirection(calculatedControllerDirection);
}
// if we're doing right controller only rotation movement
else if (directionMethod.Equals(DirectionalMethod.RightControllerRotationOnly))
{
Vector3 calculatedControllerDirection = (controllerRightHand != null ? controllerRightHand.transform.rotation : Quaternion.identity) * Vector3.forward;
returnDirection = CalculateControllerRotationDirection(calculatedControllerDirection);
}
// if we're doing engaged controller only rotation movement
else if (directionMethod.Equals(DirectionalMethod.EngageControllerRotationOnly))
{
Vector3 calculatedControllerDirection = (engagedController != null ? engagedController.scriptAlias.transform.rotation : Quaternion.identity) * Vector3.forward;
returnDirection = CalculateControllerRotationDirection(calculatedControllerDirection);
}
// Otherwise if we're just doing Gaze movement, always set the direction to where we're looking.
else if (directionMethod.Equals(DirectionalMethod.Gaze))
{
returnDirection = (new Vector3(headset.forward.x, 0, headset.forward.z));
}

return returnDirection;
return initialGaze;
}

protected virtual Vector3 CalculateControllerRotationDirection(Vector3 calculatedControllerDirection)
Expand Down Expand Up @@ -441,7 +426,7 @@ protected virtual void EngageButtonReleased(object sender, ControllerInteraction
Transform trackedObj = trackedObjects[i];
movementList[trackedObj].Clear();
}
initalGaze = Vector3.zero;
initialGaze = Vector3.zero;

active = false;
engagedController = null;
Expand Down
Loading

0 comments on commit bf90d21

Please sign in to comment.