void InitializeSolver() { Bone2D rootBone = Bone2D.GetChainBoneByIndex(target, numBones - 1); SetAttachedIK(null); solver.Initialize(rootBone, numBones); }
public static void InitializeIk2D(SerializedObject ikSO) { SerializedProperty targetProp = ikSO.FindProperty("m_Target"); SerializedProperty numBonesProp = ikSO.FindProperty("m_NumBones"); SerializedProperty solverProp = ikSO.FindProperty("m_Solver"); SerializedProperty solverPosesProp = solverProp.FindPropertyRelative("m_SolverPoses"); SerializedProperty rootBoneProp = solverProp.FindPropertyRelative("m_RootBone"); Bone2D targetBone = targetProp.objectReferenceValue as Bone2D; Bone2D rootBone = null; if (targetBone) { rootBone = Bone2D.GetChainBoneByIndex(targetBone, numBonesProp.intValue - 1); } for (int i = 0; i < solverPosesProp.arraySize; ++i) { SerializedProperty poseProp = solverPosesProp.GetArrayElementAtIndex(i); SerializedProperty poseBoneProp = poseProp.FindPropertyRelative("bone"); Bone2D poseBone = poseBoneProp.objectReferenceValue as Bone2D; if (poseBone) { poseBone.attachedIK = null; } } rootBoneProp.objectReferenceValue = rootBone; solverPosesProp.arraySize = 0; if (rootBone) { solverPosesProp.arraySize = numBonesProp.intValue; Bone2D bone = rootBone; for (int i = 0; i < numBonesProp.intValue; ++i) { SerializedProperty poseProp = solverPosesProp.GetArrayElementAtIndex(i); SerializedProperty poseBoneProp = poseProp.FindPropertyRelative("bone"); SerializedProperty localRotationProp = poseProp.FindPropertyRelative("defaultLocalRotation"); SerializedProperty solverPositionProp = poseProp.FindPropertyRelative("solverPosition"); SerializedProperty solverRotationProp = poseProp.FindPropertyRelative("solverRotation"); if (bone) { poseBoneProp.objectReferenceValue = bone; localRotationProp.quaternionValue = bone.transform.localRotation; solverPositionProp.vector3Value = Vector3.zero; solverRotationProp.quaternionValue = Quaternion.identity; bone = bone.child; } } } }