Skip to content

Commit

Permalink
fix(Components): simplify getting components and child components
Browse files Browse the repository at this point in the history
The `GetComponentsInChildren` method will also return any types
found on the current object as well so it's not required to do a
`GetComponents` to affect the current object and then followed by
`GetComponentsInChildren` to affect the child objects.

`GetComponentsInChildren` is enough to deal with all current object
types and all child types as well. It's inefficient to make the
call twice and update the same object in both calls.

There are still occasions where a check to see if the item exists
on the current object using `GetComponent` is valid, because it
it a cheaper call to execute and if the object contains the
component then doing the more expensive `GetComponentInChildren`
is not required to be made.
  • Loading branch information
thestonefox committed Sep 16, 2016
1 parent 6a480a0 commit 338508a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 38 deletions.
18 changes: 4 additions & 14 deletions Assets/VRTK/Scripts/Helper/CurveGenerator.cs
Expand Up @@ -304,27 +304,17 @@ private void SetObjects(Material material)

private void setMeshMaterial(GameObject item, Material material)
{
if (item.GetComponent<MeshRenderer>())
foreach (MeshRenderer itemRenderer in item.GetComponentsInChildren<MeshRenderer>())
{
item.GetComponent<MeshRenderer>().material = material;
}

foreach (MeshRenderer mr in item.GetComponentsInChildren<MeshRenderer>())
{
mr.material = material;
itemRenderer.material = material;
}
}

private void setSkinnedMeshMaterial(GameObject item, Material material)
{
if (item.GetComponent<SkinnedMeshRenderer>())
{
item.GetComponent<SkinnedMeshRenderer>().material = material;
}

foreach (SkinnedMeshRenderer mr in item.GetComponentsInChildren<SkinnedMeshRenderer>())
foreach (SkinnedMeshRenderer itemRenderer in item.GetComponentsInChildren<SkinnedMeshRenderer>())
{
mr.material = material;
itemRenderer.material = material;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRTK/Scripts/Helper/Utilities.cs
Expand Up @@ -97,9 +97,9 @@ public static Transform AddCameraFade()
return camera;
}

public static void CreateColliders(GameObject go)
public static void CreateColliders(GameObject obj)
{
Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
foreach (Renderer renderer in renderers)
{
if (!renderer.gameObject.GetComponent<Collider>())
Expand Down
25 changes: 13 additions & 12 deletions Assets/VRTK/Scripts/VRTK_BezierPointer.cs
Expand Up @@ -106,18 +106,23 @@ protected override void InitPointer()
beamTraceMaterial = Instantiate(renderer.sharedMaterial);
}
}

if (customPointerCursor)
{
Renderer renderer = customPointerCursor.GetComponentInChildren<MeshRenderer>();
if (renderer != null)
var customPointerCursorRenderer = customPointerCursor.GetComponentInChildren<MeshRenderer>();

if (customPointerCursorRenderer != null)
{
customPointerMaterial = Instantiate(renderer.sharedMaterial);
customPointerMaterial = Instantiate(customPointerCursorRenderer.sharedMaterial);
}

pointerCursor = Instantiate(customPointerCursor);
foreach (Renderer mr in pointerCursor.GetComponentsInChildren<Renderer>())

foreach (var pointerCursorRenderer in pointerCursor.GetComponentsInChildren<Renderer>())
{
mr.material = customPointerMaterial;
pointerCursorRenderer.material = customPointerMaterial;
}

if (validTeleportLocationObject != null)
{
validTeleportLocationInstance = Instantiate(validTeleportLocationObject);
Expand All @@ -131,6 +136,7 @@ protected override void InitPointer()
{
pointerCursor = CreateCursor();
}

pointerCursor.name = string.Format("[{0}]WorldPointer_BezierPointer_PointerCursor", gameObject.name);
Utilities.SetPlayerObject(pointerCursor, VRTK_PlayerObject.ObjectTypes.Pointer);
pointerCursor.layer = LayerMask.NameToLayer("Ignore Raycast");
Expand Down Expand Up @@ -162,14 +168,9 @@ protected override void SetPointerMaterial()

if (customPointerCursor == null)
{
if (pointerCursor.GetComponent<Renderer>())
{
pointerCursor.GetComponent<Renderer>().material = pointerMaterial;
}

foreach (Renderer mr in pointerCursor.GetComponentsInChildren<Renderer>())
foreach (var pointerCursorRenderers in pointerCursor.GetComponentsInChildren<Renderer>())
{
mr.material = pointerMaterial;
pointerCursorRenderers.material = pointerMaterial;
}
}
base.SetPointerMaterial();
Expand Down
7 changes: 1 addition & 6 deletions Assets/VRTK/Scripts/VRTK_InteractTouch.cs
Expand Up @@ -146,11 +146,6 @@ public void ToggleControllerRigidBody(bool state)
if (controllerCollisionDetector && touchRigidBody)
{
touchRigidBody.isKinematic = !state;
foreach (var collider in controllerCollisionDetector.GetComponents<Collider>())
{
collider.isTrigger = !state;
}

foreach (var collider in controllerCollisionDetector.GetComponentsInChildren<Collider>())
{
collider.isTrigger = !state;
Expand Down Expand Up @@ -372,7 +367,7 @@ private bool CustomRigidBodyIsChild()
{
foreach (var childTransform in GetComponentsInChildren<Transform>())
{
if (childTransform == customRigidbodyObject.transform)
if (childTransform != transform && childTransform == customRigidbodyObject.transform)
{
return true;
}
Expand Down
3 changes: 0 additions & 3 deletions Assets/VRTK/Scripts/VRTK_PlayerPresence.cs
Expand Up @@ -199,7 +199,6 @@ private void OnGrabObject(object sender, ObjectInteractEventArgs e)
{
if(e.target)
{
IgnoreCollisions(e.target.GetComponents<Collider>(), true);
IgnoreCollisions(e.target.GetComponentsInChildren<Collider>(), true);
}
}
Expand All @@ -208,7 +207,6 @@ private void OnUngrabObject(object sender, ObjectInteractEventArgs e)
{
if (e.target && e.target.GetComponent<VRTK_InteractableObject>())
{
IgnoreCollisions(e.target.GetComponents<Collider>(), false);
IgnoreCollisions(e.target.GetComponentsInChildren<Collider>(), false);
}
}
Expand Down Expand Up @@ -351,7 +349,6 @@ private void InitControllerListeners(GameObject controller, bool state)
{
if (controller)
{
IgnoreCollisions(controller.GetComponents<Collider>(), true);
IgnoreCollisions(controller.GetComponentsInChildren<Collider>(), true);

var grabbingController = controller.GetComponent<VRTK_InteractGrab>();
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRTK/Scripts/VRTK_SimplePointer.cs
Expand Up @@ -88,7 +88,7 @@ protected override void InitPointer()
Renderer renderer = customPointerCursor.GetComponentInChildren<MeshRenderer>();
if (renderer)
{
customPointerMaterial = Material.Instantiate(renderer.sharedMaterial);
customPointerMaterial = Instantiate(renderer.sharedMaterial);
}
pointerTip = Instantiate(customPointerCursor);
foreach (Renderer mr in pointerTip.GetComponentsInChildren<Renderer>())
Expand Down

0 comments on commit 338508a

Please sign in to comment.