Exemplo n.º 1
0
 void OnEnable()
 {
     ik = (InverseKinematics)target;
 }
Exemplo n.º 2
0
        // Restore a saved pose for the Skeleton
        public void RestorePose(Pose pose)
        {
            var bones = gameObject.GetComponentsInChildren <Bone>();

            /// WILL BE DEPRECIATED IN FUTURE RELEASE AS ControlPoint IS NO LONGER SUPPORTED ///
            var cps = gameObject.GetComponentsInChildren <ControlPoint>();

            var poseSkin2Ds = gameObject.GetComponentsInChildren <Skin2D>();

                        #if UNITY_EDITOR
            Undo.RegisterCompleteObjectUndo(bones, "Assign Pose");
            Undo.RegisterCompleteObjectUndo(cps, "Assign Pose");
            Undo.RegisterCompleteObjectUndo(poseSkin2Ds, "Assign Pose");
                        #endif

            if (bones.Length > 0)
            {
                for (int i = 0; i < pose.rotations.Length; i++)
                {
                    bool hasRot = false;
                    for (int b = 0; b < bones.Length; b++)
                    {
                        if (bones[b].name == pose.rotations[i].name)
                        {
                                                        #if UNITY_EDITOR
                            Undo.RecordObject(bones[b].transform, "Assign Pose");
                                                        #endif

                            // Set the bone rotation to the pose rotation
                            bones[b].transform.localRotation = pose.rotations[i].rotation;

                                                        #if UNITY_EDITOR
                            EditorUtility.SetDirty(bones[b].transform);
                                                        #endif
                            hasRot = true;
                        }
                    }

                    if (!hasRot)
                    {
                        if (verboseLogs)
                        {
                            Debug.Log("This skeleton has no bone '" + pose.rotations[i].name + "' enabled");
                        }
                    }
                }

                for (int j = 0; j < pose.positions.Length; j++)
                {
                    bool hasPos = false;
                    for (int o = 0; o < bones.Length; o++)
                    {
                        if (bones[o].name == pose.positions[j].name)
                        {
                                                        #if UNITY_EDITOR
                            Undo.RecordObject(bones[o].transform, "Assign Pose");
                                                        #endif

                            // Set the bone position to the pose position
                            bones[o].transform.localPosition = pose.positions[j].position;

                                                        #if UNITY_EDITOR
                            EditorUtility.SetDirty(bones[o].transform);
                                                        #endif
                            hasPos = true;
                        }
                    }

                    if (!hasPos)
                    {
                        if (verboseLogs)
                        {
                            Debug.Log("This skeleton has no bone '" + pose.positions[j].name + "' enabled");
                        }
                    }
                }

                for (int k = 0; k < pose.targets.Length; k++)
                {
                    bool hasTarget = false;
                    for (int n = 0; n < bones.Length; n++)
                    {
                        if (bones[n].name == pose.targets[k].name)
                        {
                            InverseKinematics ik = bones[n].GetComponent <InverseKinematics>();

                            if (ik != null)
                            {
                                                                #if UNITY_EDITOR
                                Undo.RecordObject(ik.target, "Assign Pose");
                                                                #endif

                                // Set IK position to the pose IK target position
                                ik.target.transform.localPosition = pose.targets[k].position;

                                                                #if UNITY_EDITOR
                                EditorUtility.SetDirty(ik.target.transform);
                                                                #endif
                            }
                            else
                            {
                                if (verboseLogs)
                                {
                                    Debug.Log("This skeleton has no ik for bone '" + bones[n].name + "' enabled");
                                }
                            }
                            hasTarget = true;
                        }
                    }

                    if (!hasTarget)
                    {
                        if (verboseLogs)
                        {
                            Debug.Log("This skeleton has no bone '" + pose.targets[k].name + "' enabled");
                        }
                    }
                }

                if (pose.sortOrders.Length > 0)
                {
                    for (int so = 0; so < pose.sortOrders.Length; so++)
                    {
                        bool hasSortingOrder = false;

                        for (int p = 0; p < bones.Length; p++)
                        {
                            List <Renderer> rens = new List <Renderer>();

                            for (int c = 0; c < bones[p].transform.childCount; c++)
                            {
                                Renderer ren = bones[p].transform.GetChild(c).GetComponent <Renderer>();

                                if (ren != null)
                                {
                                    rens.Add(ren);
                                }
                            }

                            if (rens.Count > 0)
                            {
                                for (int r = 0; r < rens.Count; r++)
                                {
                                    string    pathToRenderer = rens[r].name;
                                    Transform nextParent     = rens[r].transform.parent;

                                    while (nextParent != bones[p].transform)
                                    {
                                        pathToRenderer = nextParent.name + "/" + name;
                                        nextParent     = nextParent.parent;
                                    }

                                    if (bones[p].name + "/" + pathToRenderer == pose.sortOrders[so].name)
                                    {
                                                                                #if UNITY_EDITOR
                                        Undo.RecordObject(rens[r], "Assign Pose");
                                                                                #endif

                                        rens[r].sortingOrder = pose.sortOrders[so].sortingOrder;

                                                                                #if UNITY_EDITOR
                                        EditorUtility.SetDirty(rens[r]);
                                                                                #endif

                                        hasSortingOrder = true;
                                    }
                                }
                            }
                            else
                            {
                                if (verboseLogs)
                                {
                                    Debug.Log("There are no Renderers in this Bone: " + name + "/" + bones[p].name + " enabled");
                                }
                            }
                        }

                        if (!hasSortingOrder)
                        {
                            if (verboseLogs)
                            {
                                Debug.Log("There is no sorting order '" + pose.sortOrders[so].name + "' enabled");
                            }
                        }
                    }
                }
                else
                {
                    Debug.Log("There are no sorting orders for this Pose.");
                }
            }

            if (pose.controlPoints.Length > 0)
            {
                for (int l = 0; l < pose.controlPoints.Length; l++)
                {
                    bool hasControlPoint = false;

                    /// WILL BE DEPRECIATED IN FUTURE RELEASE AS CONTROL POINT IS NO LONGER SUPPORTED ///
                    if (cps.Length > 0)
                    {
                        for (int c = 0; c < cps.Length; c++)
                        {
                            if (cps[c].name == pose.controlPoints[l].name)
                            {
                                                                #if UNITY_EDITOR
                                Undo.RecordObject(cps[c].transform, "Assign Pose");
                                                                #endif

                                // Set the control point transform position to the control point position
                                cps[c].transform.localPosition = pose.controlPoints[l].position;

                                                                #if UNITY_EDITOR
                                EditorUtility.SetDirty(cps[c].transform);
                                                                #endif
                                hasControlPoint = true;
                            }
                        }
                    }

                    // Move control points in Skin2D component
                    if (poseSkin2Ds.Length > 0)
                    {
                        for (int s = 0; s < poseSkin2Ds.Length; s++)
                        {
                            if (poseSkin2Ds[s].points != null && poseSkin2Ds[s].controlPoints != null &&
                                poseSkin2Ds[s].controlPoints.Length > 0 &&
                                pose.controlPoints[l].name.StartsWith(poseSkin2Ds[s].name + " Control Point"))
                            {
                                                                #if UNITY_EDITOR
                                Undo.RecordObject(poseSkin2Ds[s], "Assign Pose");
                                Undo.RecordObject(poseSkin2Ds[s].points, "Assign Pose");
                                                                #endif

                                // Get control point index by name in pose
                                int index = GetControlPointIndex(pose.controlPoints[l].name);
                                poseSkin2Ds[s].controlPoints[index].position = pose.controlPoints[l].position;
                                poseSkin2Ds[s].points.SetPoint(poseSkin2Ds[s].controlPoints[index]);

                                                                #if UNITY_EDITOR
                                EditorUtility.SetDirty(poseSkin2Ds[s]);
                                EditorUtility.SetDirty(poseSkin2Ds[s].points);
                                                                #endif

                                hasControlPoint = true;

                                // Debug.Log("Found " + pose.controlPoints[l].name + " set to " + index + poseSkin2Ds[s].points.GetPoint(poseSkin2Ds[s].controlPoints[index]));
                            }
                        }
                    }

                    if (!hasControlPoint)
                    {
                        if (verboseLogs)
                        {
                            Debug.Log("There is no control point '" + pose.controlPoints[l].name + "'" + " enabled.");
                        }
                    }
                }
            }
        }