public void AttachBodyPart(BodyPartController bpc) { #region Nearest Bone int nearestBoneIndex = -1; float minDistance = float.PositiveInfinity; for (int boneIndex = 0; boneIndex < root.childCount; boneIndex++) { float distance = Vector3.Distance(root.GetChild(boneIndex).position, bpc.transform.position); if (distance < minDistance) { minDistance = distance; nearestBoneIndex = boneIndex; } } #endregion if (bpc.AttachedBodyPart == null) { AttachedBodyPart attachedBodyPart = new AttachedBodyPart(bpc.name.Replace("(Flipped)", ""), nearestBoneIndex, new SerializableTransform(bpc.transform)); bpc.AttachedBodyPart = bpc.Flipped.AttachedBodyPart = attachedBodyPart; bpc.Drag.WorldBounds = bpc.Flipped.Drag.WorldBounds = new Bounds(new Vector3(0, 1, 0), new Vector3(4, 4, 4)); data.attachedBodyParts.Add(attachedBodyPart); } bpc.transform.SetParent(root.GetChild(nearestBoneIndex), true); }
public void Load(string creatureName) { Clear(); CreatureData data = JsonUtility.FromJson <CreatureData>(SaveUtility.Load(creatureName + ".json")); for (int i = 0; i < data.bones.Count; i++) { Bone bone = data.bones[i]; Add(i, bone.Position, bone.Rotation, bone.Size); } for (int i = 0; i < data.attachedBodyParts.Count; i++) { AttachedBodyPart attachedBodyPart = data.attachedBodyParts[i]; BodyPartController bpc = Instantiate(DatabaseManager.GetDatabaseEntry <BodyPart>("Body Parts", attachedBodyPart.BodyPartID).Prefab, root.GetChild(attachedBodyPart.BoneIndex)).GetComponent <BodyPartController>(); bpc.gameObject.name = attachedBodyPart.BodyPartID; SerializableTransform.RecurseUpdate(bpc.transform, attachedBodyPart.Transform); SetupBodyPart(bpc); AttachBodyPart(bpc); AddToStatistics(bpc.name); if (Mathf.Abs(bpc.transform.position.x) > settings.MergeThreshold) { bpc.Flipped.transform.position = new Vector3(-bpc.transform.position.x, bpc.transform.position.y, bpc.transform.position.z); bpc.Flipped.transform.rotation = Quaternion.Euler(bpc.transform.rotation.eulerAngles.x, -bpc.transform.rotation.eulerAngles.y, -bpc.transform.rotation.eulerAngles.z); if (bpc is LimbController) { LimbController limb = bpc as LimbController; LimbController flippedLimb = limb.FlippedLimb; for (int j = 0; j < limb.Bones.Length; j++) { float x = -limb.Bones[j].position.x; float y = limb.Bones[j].position.y; float z = limb.Bones[j].position.z; flippedLimb.Bones[j].position = new Vector3(x, y, z); } } } else { bpc.Flipped.gameObject.SetActive(false); } } SetColours(data.primaryColour, data.secondaryColour); SetPattern(data.patternID); SetSelected(false); SetTextured(Textured); SetInteractable(Interactable); }