public static void OrientToChild(Bone2D bone, bool freezeChildren, string undoName, bool recordObject) { if (!bone || !bone.child) { return; } Vector3 l_childPosition = Vector3.zero; /* * if(recordObject) * { * Undo.RecordObject(bone.child.transform,undoName); * }else{ * Undo.RegisterCompleteObjectUndo(bone.child.transform,undoName); * } */ l_childPosition = bone.child.transform.position; Quaternion l_deltaRotation = OrientToLocalPosition(bone, bone.child.transform.localPosition, freezeChildren, undoName, recordObject); bone.child.transform.position = l_childPosition; bone.child.transform.localRotation *= Quaternion.Inverse(l_deltaRotation); EditorUtility.SetDirty(bone.child.transform); }
public static void SavePose(Pose pose, Transform root) { List <Bone2D> bones = new List <Bone2D>(50); root.GetComponentsInChildren <Bone2D>(true, bones); SerializedObject poseSO = new SerializedObject(pose); SerializedProperty entriesProp = poseSO.FindProperty("m_PoseEntries"); poseSO.Update(); entriesProp.arraySize = bones.Count; for (int i = 0; i < bones.Count; i++) { Bone2D bone = bones [i]; if (bone) { SerializedProperty element = entriesProp.GetArrayElementAtIndex(i); element.FindPropertyRelative("path").stringValue = BoneUtils.GetBonePath(root, bone); element.FindPropertyRelative("localPosition").vector3Value = bone.transform.localPosition; element.FindPropertyRelative("localRotation").quaternionValue = bone.transform.localRotation; element.FindPropertyRelative("localScale").vector3Value = bone.transform.localScale; } } poseSO.ApplyModifiedProperties(); }
override public void OnInspectorGUI() { DrawDefaultInspector(); Bone2D bone = target as Bone2D; EditorGUI.BeginChangeCheck(); Bone2D child = EditorGUILayout.ObjectField("Child", bone.child, typeof(Bone2D), true) as Bone2D; if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(bone, "Child"); bone.child = child; EditorUtility.SetDirty(bone); } EditorGUI.BeginChangeCheck(); float length = EditorGUILayout.FloatField("Length", bone.localLength); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(bone, "Length"); bone.localLength = length; EditorUtility.SetDirty(bone); } }
public static void UpdateIK(Bone2D bone, string undoName) { List <Bone2D> boneList = new List <Bone2D>(25); List <Ik2D> ikList = new List <Ik2D>(25); BuildIkList(bone, boneList, ikList); for (int i = 0; i < ikList.Count; i++) { Ik2D l_ik2D = ikList[i]; if (l_ik2D && l_ik2D.isActiveAndEnabled) { for (int j = 0; j < l_ik2D.solver.solverPoses.Count; j++) { IkSolver2D.SolverPose pose = l_ik2D.solver.solverPoses [j]; if (pose.bone) { Undo.RecordObject(pose.bone.transform, undoName); } } l_ik2D.solver.RestoreDefaultPoses(); l_ik2D.UpdateIK(); } } }
public static void DrawBoneCap(Bone2D bone) { Color color = bone.color * 0.25f; color.a = 1f; DrawBoneCap(bone, color); }
List <string> GetDuplicatedPaths(Transform root) { List <string> paths = new List <string>(50); List <string> duplicates = new List <string>(50); List <Bone2D> bones = new List <Bone2D>(50); root.GetComponentsInChildren <Bone2D>(true, bones); for (int i = 0; i < bones.Count; i++) { Bone2D bone = bones [i]; if (bone) { string bonePath = BoneUtils.GetBonePath(root, bone); if (paths.Contains(bonePath)) { duplicates.Add(bonePath); } else { paths.Add(bonePath); } } } return(duplicates); }
void UpgradeToBoneTransforms() { if (Selection.transforms.Length == 1 && m_BoneTransformsProperty.arraySize == 0 && m_BonesProperty.arraySize > 0) { serializedObject.Update(); m_BoneTransformsProperty.arraySize = m_BonesProperty.arraySize; for (int i = 0; i < m_BonesProperty.arraySize; ++i) { SerializedProperty boneElement = m_BonesProperty.GetArrayElementAtIndex(i); SerializedProperty transformElement = m_BoneTransformsProperty.GetArrayElementAtIndex(i); if (boneElement.objectReferenceValue) { Bone2D bone = boneElement.objectReferenceValue as Bone2D; transformElement.objectReferenceValue = bone.transform; } } m_BonesProperty.arraySize = 0; serializedObject.ApplyModifiedProperties(); } }
public static string GetBonePath(Transform root, Bone2D bone) { string path = ""; Transform current = bone.transform; if (root) { while (current && current != root) { path = current.name + path; current = current.transform.parent; if (current != root) { path = "/" + path; } } if (!current) { path = ""; } } return(path); }
static void CreateBone(MenuCommand menuCommand) { GameObject bone = new GameObject("New bone"); Bone2D boneComponent = bone.AddComponent <Bone2D>(); Undo.RegisterCreatedObjectUndo(bone, "Create bone"); bone.transform.position = GetDefaultInstantiatePosition(); GameObject selectedGO = Selection.activeGameObject; if (selectedGO) { bone.transform.parent = selectedGO.transform; bone.transform.localPosition = Vector3.zero; bone.transform.localRotation = Quaternion.identity; bone.transform.localScale = Vector3.one; Bone2D selectedBone = selectedGO.GetComponent <Bone2D>(); if (selectedBone) { bone.transform.position = selectedBone.endPosition; if (!selectedBone.child) { selectedBone.child = boneComponent; } } } Selection.activeGameObject = bone; }
static void CreateControl(MenuCommand menuCommand) { GameObject control = new GameObject("New control"); Undo.RegisterCreatedObjectUndo(control, "Crate Control"); Control controlComponent = control.AddComponent <Control>(); control.transform.position = GetDefaultInstantiatePosition(); GameObject selectedGO = Selection.activeGameObject; if (selectedGO) { control.transform.parent = selectedGO.transform; control.transform.localPosition = Vector3.zero; control.transform.localRotation = Quaternion.identity; Bone2D selectedBone = selectedGO.GetComponent <Bone2D>(); if (selectedBone) { control.name = "Control " + selectedBone.name; controlComponent.bone = selectedBone; control.transform.parent = selectedBone.root.transform.parent; } } EditorUtility.SetDirty(controlComponent); Selection.activeGameObject = control; }
public void Clear(string undoName) { RegisterUndo(undoName); foreach (Edge edge in edges) { DestroyObjectImmediate(edge); } foreach (Node node in nodes) { DestroyObjectImmediate(node); } DestroyBlendShapeCache(undoName); selectedBindPose = null; selectedBone = null; selectedEdge = null; selectedBlendshape = null; selection.Clear(); nodes.Clear(); edges.Clear(); indices.Clear(); boneWeights.Clear(); blendShapeWeight = 0f; m_CurrentTexVertices.Clear(); isDirty = false; m_DirtyVertices = false; }
static void CreateIkLimb(MenuCommand menuCommand) { GameObject ikLimb = new GameObject("New Ik Limb"); Undo.RegisterCreatedObjectUndo(ikLimb, "Crate Ik Limb"); IkLimb2D ikLimbComponent = ikLimb.AddComponent <IkLimb2D>(); ikLimb.transform.position = GetDefaultInstantiatePosition(); GameObject selectedGO = Selection.activeGameObject; if (selectedGO) { ikLimb.transform.parent = selectedGO.transform; ikLimb.transform.localPosition = Vector3.zero; Bone2D selectedBone = selectedGO.GetComponent <Bone2D>(); if (selectedBone) { ikLimb.transform.parent = selectedBone.root.transform.parent; ikLimb.transform.position = selectedBone.endPosition; ikLimbComponent.numBones = selectedBone.chainLength; ikLimbComponent.target = selectedBone; } } ikLimb.transform.rotation = Quaternion.identity; ikLimb.transform.localScale = Vector3.one; Selection.activeGameObject = ikLimb; }
static void BuildIkList(Bone2D bone, List <Bone2D> boneList, List <Ik2D> ikList) { if (!bone) { return; } if (boneList.Contains(bone)) { return; } boneList.Add(bone); Ik2D ik2D = bone.attachedIK; List <Bone2D> childBones = new List <Bone2D>(25); if (ik2D) { if (!ikList.Contains(ik2D)) { ikList.Add(ik2D); } for (int i = 0; i < ik2D.solver.solverPoses.Count; i++) { IkSolver2D.SolverPose pose = ik2D.solver.solverPoses [i]; if (pose.bone) { pose.bone.GetComponentsInChildren <Bone2D>(childBones); for (int j = 0; j < childBones.Count; j++) { Bone2D l_bone = childBones[j]; if (l_bone && !boneList.Contains(l_bone)) { BuildIkList(l_bone, boneList, ikList); } } } } } else { bone.GetComponentsInChildren <Bone2D>(childBones); for (int j = 0; j < childBones.Count; j++) { Bone2D l_bone = childBones[j]; if (l_bone && !boneList.Contains(l_bone)) { BuildIkList(l_bone, boneList, ikList); } } } }
public static Bone2D GetChainBoneByIndex(Bone2D chainTip, int index) { if (!chainTip) { return(null); } Bone2D bone = chainTip; int chainLength = bone.chainLength; for (int i = 0; i < chainLength && bone; ++i) { if (i == index) { return(bone); } if (bone.linkedParentBone) { bone = bone.parentBone; } else { return(null); } } return(null); }
public static SpriteMeshInstance CreateSpriteMeshInstance(SpriteMesh spriteMesh, GameObject gameObject, bool undo = true) { SpriteMeshInstance spriteMeshInstance = null; if (spriteMesh && gameObject) { if (undo) { spriteMeshInstance = Undo.AddComponent <SpriteMeshInstance>(gameObject); } else { spriteMeshInstance = gameObject.AddComponent <SpriteMeshInstance>(); } spriteMeshInstance.spriteMesh = spriteMesh; spriteMeshInstance.sharedMaterial = defaultMaterial; SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh); List <Bone2D> bones = new List <Bone2D>(); List <string> paths = new List <string>(); Vector4 zero = new Vector4(0f, 0f, 0f, 1f); foreach (BindInfo bindInfo in spriteMeshData.bindPoses) { Matrix4x4 m = spriteMeshInstance.transform.localToWorldMatrix * bindInfo.bindPose.inverse; GameObject bone = new GameObject(bindInfo.name); if (undo) { Undo.RegisterCreatedObjectUndo(bone, Undo.GetCurrentGroupName()); } Bone2D boneComponent = bone.AddComponent <Bone2D>(); boneComponent.localLength = bindInfo.boneLength; bone.transform.position = m * zero; bone.transform.rotation = m.GetRotation(); bone.transform.parent = gameObject.transform; bones.Add(boneComponent); paths.Add(bindInfo.path); } BoneUtils.ReconstructHierarchy(bones, paths); spriteMeshInstance.bones = bones; SpriteMeshUtils.UpdateRenderer(spriteMeshInstance, undo); EditorUtility.SetDirty(spriteMeshInstance); } return(spriteMeshInstance); }
void InitializeSolver() { Bone2D rootBone = Bone2D.GetChainBoneByIndex(target, numBones - 1); SetAttachedIK(null); solver.Initialize(rootBone, numBones); }
public static List <Ik2D> UpdateIK(Bone2D bone, string undoName, bool recordObject) { List <Ik2D> list = BuildIkList(bone.chainRoot.gameObject); UpdateIkList(list, undoName, recordObject); return(list); }
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; } } } }
public static void DrawBoneOutline(Bone2D bone, float outlineSize, Color color) { Handles.matrix = bone.transform.localToWorldMatrix; DrawBoneOutline(Vector3.zero, bone.localEndPosition, GetBoneRadius(bone), outlineSize / Handles.matrix.GetScale().x, color); }
void OnSceneGUI() { Bone2D bone = target as Bone2D; if (Tools.current == Tool.Move) { Tools.hidden = true; float size = HandleUtility.GetHandleSize(bone.transform.position) / 5f; Quaternion rotation = bone.transform.rotation; EditorGUI.BeginChangeCheck(); Vector3 newPosition = Handles.FreeMoveHandle(bone.transform.position, rotation, size, Vector3.zero, Handles.RectangleCap); if (EditorGUI.EndChangeCheck()) { GUI.changed = true; Bone2D linkedParentBone = bone.linkedParentBone; if (linkedParentBone) { Vector3 newLocalPosition = linkedParentBone.transform.InverseTransformPoint(newPosition); if (newLocalPosition.sqrMagnitude > 0f) { float angle = Mathf.Atan2(newLocalPosition.y, newLocalPosition.x) * Mathf.Rad2Deg; Undo.RecordObject(linkedParentBone.transform, "Move"); Undo.RecordObject(linkedParentBone, "Move"); linkedParentBone.transform.localRotation *= Quaternion.AngleAxis(angle, Vector3.forward); EditorUtility.SetDirty(linkedParentBone.transform); } } Undo.RecordObject(bone.transform, "Move"); bone.transform.position = newPosition; bone.transform.rotation = rotation; EditorUtility.SetDirty(bone.transform); IkUtils.UpdateIK(bone, "Move"); } } else { Tools.hidden = false; } }
void OnEnable() { Tools.hidden = Tools.current == Tool.Move; m_Bone = target as Bone2D; m_ColorProperty = serializedObject.FindProperty("m_Color"); m_AlphaProperty = m_ColorProperty.FindPropertyRelative("a"); m_ChildTransformProperty = serializedObject.FindProperty("m_ChildTransform"); m_LengthProperty = serializedObject.FindProperty("m_Length"); }
public static void LoadPose(Pose pose, Transform root) { SerializedObject poseSO = new SerializedObject(pose); SerializedProperty entriesProp = poseSO.FindProperty("m_PoseEntries"); List <Ik2D> iks = new List <Ik2D>(); for (int i = 0; i < entriesProp.arraySize; i++) { SerializedProperty element = entriesProp.GetArrayElementAtIndex(i); Transform boneTransform = root.Find(element.FindPropertyRelative("path").stringValue); if (boneTransform) { Bone2D boneComponent = boneTransform.GetComponent <Bone2D>(); if (boneComponent && boneComponent.attachedIK && !iks.Contains(boneComponent.attachedIK)) { iks.Add(boneComponent.attachedIK); } Undo.RecordObject(boneTransform, "Load Pose"); boneTransform.localPosition = element.FindPropertyRelative("localPosition").vector3Value; boneTransform.localRotation = element.FindPropertyRelative("localRotation").quaternionValue; boneTransform.localScale = element.FindPropertyRelative("localScale").vector3Value; BoneUtils.FixLocalEulerHint(boneTransform); } } for (int i = 0; i < iks.Count; i++) { Ik2D ik = iks[i]; if (ik && ik.target) { Undo.RecordObject(ik.transform, "Load Pose"); ik.transform.position = ik.target.endPosition; if (ik.orientChild && ik.target.child) { ik.transform.rotation = ik.target.child.transform.rotation; BoneUtils.FixLocalEulerHint(ik.transform); } } } EditorUpdater.SetDirty("Load Pose"); }
public void DeleteBone(Bone2D bone) { if (spriteMeshInstance && bone) { List <Bone2D> bones = spriteMeshInstance.bones; if (bones.Contains(bone)) { bones.Remove(bone); spriteMeshInstance.bones = bones; EditorUtility.SetDirty(spriteMeshInstance); } } }
public static string GetUniqueBoneName(Bone2D root) { string boneName = "bone"; Bone2D[] bones = null; if(root) { bones = root.GetComponentsInChildren<Bone2D>(true); boneName = boneName + " " + (bones.Length + 1).ToString(); } return boneName; }
public static string GetUniqueBoneName(Bone2D root) { string boneName = "bone"; Bone2D[] bones = null; if (root) { bones = root.GetComponentsInChildren <Bone2D>(true); boneName = boneName + " " + (bones.Length + 1).ToString(); } return(boneName); }
public void BindBones() { selectedBone = null; if (spriteMeshInstance) { bindPoses.Clear(); foreach (Bone2D bone in spriteMeshInstance.bones) { BindBone(bone); } } }
override public void OnInspectorGUI() { IkCCD2D ikCCD2D = target as IkCCD2D; base.OnInspectorGUI(); SerializedProperty numBonesProp = serializedObject.FindProperty("m_NumBones"); SerializedProperty iterationsProp = serializedObject.FindProperty("iterations"); SerializedProperty dampingProp = serializedObject.FindProperty("damping"); Bone2D targetBone = ikCCD2D.target; serializedObject.Update(); EditorGUI.BeginDisabledGroup(!targetBone); EditorGUI.BeginChangeCheck(); int chainLength = 0; if (targetBone) { chainLength = targetBone.chainLength; } EditorGUILayout.IntSlider(numBonesProp, 0, chainLength); if (EditorGUI.EndChangeCheck()) { Undo.RegisterCompleteObjectUndo(ikCCD2D, "Set num bones"); IkUtils.InitializeIk2D(serializedObject); EditorUpdater.SetDirty("Set num bones"); } EditorGUI.EndDisabledGroup(); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(iterationsProp); EditorGUILayout.PropertyField(dampingProp); if (EditorGUI.EndChangeCheck()) { EditorUpdater.SetDirty(Undo.GetCurrentGroupName()); } serializedObject.ApplyModifiedProperties(); }
override public void OnInspectorGUI() { base.OnInspectorGUI(); serializedObject.Update(); SerializedProperty targetBoneProp = serializedObject.FindProperty("m_Target"); SerializedProperty numBonesProp = serializedObject.FindProperty("m_NumBones"); SerializedProperty iterationsProp = serializedObject.FindProperty("iterations"); SerializedProperty dampingProp = serializedObject.FindProperty("damping"); Bone2D targetBone = targetBoneProp.objectReferenceValue as Bone2D; EditorGUI.BeginDisabledGroup(!targetBone); EditorGUI.BeginChangeCheck(); int chainLength = 0; if (targetBone) { chainLength = targetBone.chainLength; } EditorGUILayout.IntSlider(numBonesProp, 0, chainLength); if (EditorGUI.EndChangeCheck()) { IkUtils.InitializeIk2D(serializedObject); DoUpdateIK(); } EditorGUI.EndDisabledGroup(); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(iterationsProp); EditorGUILayout.PropertyField(dampingProp); if (EditorGUI.EndChangeCheck()) { DoUpdateIK(); } serializedObject.ApplyModifiedProperties(); }
void OnEnable() { Tools.hidden = Tools.current == Tool.Move; m_Bone = target as Bone2D; m_ColorProperty = serializedObject.FindProperty("m_Color"); m_AlphaProperty = m_ColorProperty.FindPropertyRelative("a"); //DEPRECATED m_ChildProperty = serializedObject.FindProperty("m_Child"); m_ChildTransformProperty = serializedObject.FindProperty("m_ChildTransform"); m_LengthProperty = serializedObject.FindProperty("m_Length"); UpgradeToChildTransform(); }
//----------------------------------------------------- // Boneの取得 //----------------------------------------------------- void GetBoneDirPoint() { //Boneを取得 boneNum = spriteMeshs[0].bones.Count; boneArr = new Transform[boneNum * 2]; //boneのtransformを取得し、 for (int i = 0; i < boneNum; i++) { //Boneを取得 Anima2D.Bone2D bone = spriteMeshs[0].bones[i]; //boneのtransformを取得 boneArr[i] = bone.transform; boneArr[i + boneNum] = spriteMeshs[1].bones[i].transform; } }
void UpgradeToChildTransform() { if (Selection.transforms.Length == 1 && !m_ChildTransformProperty.objectReferenceValue && m_ChildProperty.objectReferenceValue) { serializedObject.Update(); Bone2D l_bone = m_ChildProperty.objectReferenceValue as Bone2D; if (l_bone) { m_ChildTransformProperty.objectReferenceValue = l_bone.transform; } m_ChildProperty.objectReferenceValue = null; serializedObject.ApplyModifiedProperties(); } }
public void BindBone(Bone2D bone) { if (spriteMeshInstance && bone) { BindInfo bindInfo = new BindInfo(); bindInfo.bindPose = bone.transform.worldToLocalMatrix * spriteMeshInstance.transform.localToWorldMatrix; bindInfo.boneLength = bone.localLength; bindInfo.path = BoneUtils.GetBonePath(bone); bindInfo.name = bone.name; bindInfo.color = ColorRing.GetColor(bindPoses.Count); if (!bindPoses.Contains(bindInfo)) { bindPoses.Add(bindInfo); isDirty = true; } } }
static bool IsVisible(Bone2D bone) { return IsVisible(bone.gameObject); }
static bool IsLocked(Bone2D bone) { return IsLocked(bone.gameObject); }
public void Clear() { selectedBindPose = null; selectedBone = null; selectedEdge = null; selectedNodes.Clear(); nodes.Clear(); edges.Clear(); indices.Clear(); boneWeights.Clear(); isDirty = false; }
public void DeleteBone(Bone2D bone) { if(spriteMeshInstance && bone) { List<Bone2D> bones = spriteMeshInstance.bones; if(bones.Contains(bone)) { bones.Remove(bone); spriteMeshInstance.bones = bones; EditorUtility.SetDirty(spriteMeshInstance); } } }
public void BindBone(Bone2D bone) { if(spriteMeshInstance && bone) { BindInfo bindInfo = new BindInfo(); bindInfo.bindPose = bone.transform.worldToLocalMatrix * spriteMeshInstance.transform.localToWorldMatrix; bindInfo.boneLength = bone.localLength; bindInfo.path = BoneUtils.GetBonePath (bone); bindInfo.name = bone.name; bindInfo.color = ColorRing.GetColor(bindPoses.Count); if(!bindPoses.Contains(bindInfo)) { bindPoses.Add (bindInfo); isDirty = true; } } }
public static string GetBonePath(Transform root, Bone2D bone) { return GetPath(root, bone.transform); }
static List<Ik2D> BuildIkList(Bone2D bone) { return BuildIkList(bone.chainRoot.gameObject); }
public void Initialize(Bone2D _rootBone, int numChilds) { rootBone = _rootBone; Bone2D bone = rootBone; solverPoses.Clear(); for(int i = 0; i < numChilds; ++i) { if(bone) { SolverPose solverPose = new SolverPose(); solverPose.bone = bone; solverPoses.Add(solverPose); bone = bone.child; } } StoreDefaultPoses(); }
public static void UpdateLinkedParentBone(Bone2D bone, bool deattachChilren, string undoName, bool recordObject) { if(!bone) return; Bone2D linkedParentBone = bone.linkedParentBone; if(linkedParentBone) { List<Vector3> childLocalScales = new List<Vector3>(linkedParentBone.transform.childCount); List<Transform> children = new List<Transform>(linkedParentBone.transform.childCount); if(deattachChilren) { foreach(Transform child in linkedParentBone.transform) { if(recordObject) { Undo.RecordObject(child,undoName); }else{ Undo.RegisterCompleteObjectUndo(child,undoName); } children.Add(child); childLocalScales.Add(child.localScale); } linkedParentBone.transform.DetachChildren(); }else{ if(recordObject) { Undo.RecordObject(bone.transform,undoName); }else{ Undo.RegisterCompleteObjectUndo(bone.transform,undoName); } children.Add(bone.transform); childLocalScales.Add(bone.transform.localScale); bone.transform.parent = null; } Vector3 localPosition = linkedParentBone.transform.InverseTransformPoint(bone.transform.position); if(localPosition.sqrMagnitude > 0f) { float angle = Mathf.Atan2(localPosition.y,localPosition.x) * Mathf.Rad2Deg; if(recordObject) { Undo.RecordObject(linkedParentBone.transform,undoName); Undo.RecordObject(linkedParentBone,undoName); }else{ Undo.RegisterCompleteObjectUndo(linkedParentBone.transform,undoName); Undo.RegisterCompleteObjectUndo(linkedParentBone,undoName); } linkedParentBone.transform.localRotation *= Quaternion.AngleAxis(angle, Vector3.forward); EditorUtility.SetDirty(linkedParentBone.transform); } for (int i = 0; i < children.Count; i++) { Transform child = children [i]; child.parent = linkedParentBone.transform; child.localScale = childLocalScales[i]; if(linkedParentBone.child && linkedParentBone.child.transform == child) { child.position = linkedParentBone.endPosition; } EditorUtility.SetDirty (child); } } }
public static List<Ik2D> UpdateIK(Bone2D bone, string undoName, bool recordObject) { List<Ik2D> list = BuildIkList(bone.chainRoot.gameObject); UpdateIkList(list,undoName,recordObject); return list; }
public static void DrawBoneBody(Bone2D bone) { DrawBoneBody(bone,bone.color); }
public static void DrawBoneBody(Bone2D bone, Color color) { Handles.matrix = bone.transform.localToWorldMatrix; DrawBoneBody(Vector3.zero, bone.localEndPosition, GetBoneRadius(bone),color); }
public static float GetBoneRadius(Bone2D bone) { return Mathf.Min(bone.localLength / 20f, 0.125f * HandleUtility.GetHandleSize(bone.transform.position)); }
public static Bone2D GetChainBoneByIndex(Bone2D chainTip, int index) { if(!chainTip) return null; Bone2D bone = chainTip; int chainLength = bone.chainLength; for(int i = 0; i < chainLength && bone; ++i) { if(i == index) { return bone; } if(bone.linkedParentBone) { bone = bone.parentBone; }else{ return null; } } return null; }
public void BindBones() { selectedBone = null; if(spriteMeshInstance) { bindPoses.Clear(); foreach(Bone2D bone in spriteMeshInstance.bones) { BindBone(bone); } } }
public static void DrawBoneCap(Bone2D bone, Color color) { Handles.matrix = bone.transform.localToWorldMatrix; DrawBoneCap(Vector3.zero,GetBoneRadius(bone),color); }
public static string GetBonePath(Bone2D bone) { return GetBonePath(bone.root.transform,bone); }