Faster BoneComponent

Used a cached Transform reference instead of using the .transform property.
( This is recommended by Unity Tech. .transform is actually shorthand for a component lookup: GetComponent<Transform>() )
This commit is contained in:
John 2014-02-20 13:59:33 +08:00
parent 453c4bcbb1
commit d50386a3a9

View File

@ -42,6 +42,16 @@ public class BoneComponent : MonoBehaviour {
/// <summary>If a bone isn't set, boneName is used to find the bone.</summary> /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
public String boneName; public String boneName;
protected Transform cachedTransform;
protected Transform skeletonComponentTransform;
void Awake () {
cachedTransform = transform;
if(skeletonComponent == null) return;
skeletonComponentTransform = skeletonComponent.transform;
}
public void LateUpdate () { public void LateUpdate () {
if (skeletonComponent == null) return; if (skeletonComponent == null) return;
if (bone == null) { if (bone == null) {
@ -52,16 +62,18 @@ public class BoneComponent : MonoBehaviour {
return; return;
} }
} }
if (transform.parent == skeletonComponent.transform) {
transform.localPosition = new Vector3(bone.worldX, bone.worldY, transform.localPosition.z); if (cachedTransform.parent == skeletonComponentTransform) {
Vector3 rotation = transform.localRotation.eulerAngles; cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, cachedTransform.localPosition.z);
transform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation); Vector3 rotation = cachedTransform.localRotation.eulerAngles;
cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation);
} else { } else {
// Best effort to set this GameObject's transform when it isn't a child of the SkeletonComponent. cachedTransform.position = skeletonComponentTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, cachedTransform.position.z));
transform.position = skeletonComponent.transform.TransformPoint(new Vector3(bone.worldX, bone.worldY, transform.position.z)); Vector3 rotation = skeletonComponentTransform.rotation.eulerAngles;
Vector3 rotation = skeletonComponent.transform.rotation.eulerAngles; cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y,
transform.rotation = Quaternion.Euler(rotation.x, rotation.y, skeletonComponentTransform.rotation.eulerAngles.z + bone.worldRotation);
skeletonComponent.transform.rotation.eulerAngles.z + bone.worldRotation);
} }
} }
}
}