示例#1
0
        public static void AutoRig()
        {
            Puppet2D_Guides[] guides = FindObjectsOfType <Puppet2D_Guides>();

            foreach (Puppet2D_Guides guide in guides)
            {
                Undo.RegisterCompleteObjectUndo(guides, "guides");

                // Set controls
                Transform _hipPoint   = guide.transform.Find("hip_guide").transform;
                Transform _chestPoint = guide.transform.Find("chest_guide").transform;

                Transform _thighLPoint = guide.transform.Find("thighL_guide").transform;
                Transform _footLPoint  = guide.transform.Find("footL_guide").transform;

                Transform _thighRPoint = guide.transform.Find("thighR_guide").transform;
                Transform _footRPoint  = guide.transform.Find("footR_guide").transform;

                Transform _armLPoint  = guide.transform.Find("armL_guide").transform;
                Transform _handLPoint = guide.transform.Find("handL_guide").transform;

                Transform _armRPoint  = guide.transform.Find("armR_guide").transform;
                Transform _handRPoint = guide.transform.Find("handR_guide").transform;

                Transform _headPoint = guide.transform.Find("head_guide").transform;

                Transform _elbowLPoint = guide.transform.Find("elbowL_guide").transform;
                Transform _kneeLPoint  = guide.transform.Find("kneeL_guide").transform;

                Transform _elbowRPoint = guide.transform.Find("elbowR_guide").transform;
                Transform _kneeRPoint  = guide.transform.Find("kneeR_guide").transform;


                // Hide other global controls

                Puppet2D_GlobalControl[] globalControls = FindObjectsOfType <Puppet2D_GlobalControl>();
                foreach (Puppet2D_GlobalControl gc in globalControls)
                {
                    gc.gameObject.SetActive(false);
                }

                GameObject[] gos      = guide.Biped;
                Bounds[]     bounds   = new Bounds[gos.Length];
                string[]     geoNames = new string[gos.Length];


                MakeSpine(_hipPoint, _chestPoint, _footLPoint.position);

                Vector3 _footLEnd = new Vector3(_footLPoint.position.x, guide.Bounds.min.y * .95f, _footLPoint.position.z);
                MakeLimb(_thighLPoint, _footLPoint, _kneeLPoint, _footLEnd, GameObject.Find("Spine_01"), "legL", "thighL", "kneeL", "footL", true);

                Vector3 _footREnd = new Vector3(_footRPoint.position.x, guide.Bounds.min.y * .95f, _footRPoint.position.z);
                MakeLimb(_thighRPoint, _footRPoint, _kneeRPoint, _footREnd, GameObject.Find("Spine_01"), "legR", "thighR", "kneeR", "footR", true);

                MakeClav(GameObject.Find("Spine_05"), "clavL");
                Vector3 _handLEnd = new Vector3(guide.Bounds.max.x * .95f, _handLPoint.position.y, _handLPoint.position.z);
                MakeLimb(_armLPoint, _handLPoint, _elbowLPoint, _handLEnd, GameObject.Find("clavL"), "armL", "shoulderL", "elbowL", "handL", false);
                //            Selection.activeGameObject = GameObject.Find("clavL");
                //            Puppet2D_CreateControls.CreateOrientControl();

                MakeClav(GameObject.Find("Spine_05"), "clavR");
                Vector3 _handREnd = new Vector3(guide.Bounds.min.x * .95f, _handRPoint.position.y, _handRPoint.position.z);
                MakeLimb(_armRPoint, _handRPoint, _elbowRPoint, _handREnd, GameObject.Find("clavR"), "armR", "shoulderR", "elbowR", "handR", false);
                //            Selection.activeGameObject = GameObject.Find("clavR");
                //            Puppet2D_CreateControls.CreateOrientControl();


                Vector3 headTop = new Vector3(_headPoint.position.x, guide.Bounds.max.y * .95f, _headPoint.position.z);
                MakeHead(_headPoint, _chestPoint, headTop);


                //SORTING ORDER ON BONES
                GameObject.Find("clavL").GetComponent <Puppet2D_Bone>().Order     = -20;
                GameObject.Find("shoulderL").GetComponent <Puppet2D_Bone>().Order = -30;
                GameObject.Find("elbowL").GetComponent <Puppet2D_Bone>().Order    = -40;
                GameObject.Find("handL").GetComponent <Puppet2D_Bone>().Order     = -50;
                GameObject.Find("handLEnd").GetComponent <Puppet2D_Bone>().Order  = -50;

                GameObject.Find("thighL").GetComponent <Puppet2D_Bone>().Order   = -10;
                GameObject.Find("kneeL").GetComponent <Puppet2D_Bone>().Order    = -20;
                GameObject.Find("footL").GetComponent <Puppet2D_Bone>().Order    = -30;
                GameObject.Find("footLEnd").GetComponent <Puppet2D_Bone>().Order = -30;

                GameObject.Find("clavR").GetComponent <Puppet2D_Bone>().Order     = 20;
                GameObject.Find("shoulderR").GetComponent <Puppet2D_Bone>().Order = 50;
                GameObject.Find("elbowR").GetComponent <Puppet2D_Bone>().Order    = 40;
                GameObject.Find("handR").GetComponent <Puppet2D_Bone>().Order     = 30;
                GameObject.Find("handREnd").GetComponent <Puppet2D_Bone>().Order  = 30;

                GameObject.Find("thighR").GetComponent <Puppet2D_Bone>().Order   = 30;
                GameObject.Find("kneeR").GetComponent <Puppet2D_Bone>().Order    = 20;
                GameObject.Find("footR").GetComponent <Puppet2D_Bone>().Order    = 10;
                GameObject.Find("footREnd").GetComponent <Puppet2D_Bone>().Order = 10;

                Puppet2D_Bone[] bones   = FindObjectsOfType <Puppet2D_Bone>();
                List <Object>   objList = new List <Object>();

                for (int i = 0; i < bones.Length; i++)
                {
                    if (!bones[i].name.Contains("clav"))
                    {
                        objList.Add(bones[i].gameObject);
                    }
                }
                for (int i = 0; i < gos.Length; i++)
                {
                    string originalSpriteName = gos[i].name;
                    bounds[i] = gos[i].GetComponent <SpriteRenderer>().bounds;

                    Selection.activeGameObject = gos[i];
                    bool isSkinned = false;

                    foreach (Object obj in objList)
                    {
                        GameObject bone = obj as GameObject;
                        if (bounds[i].Contains(bone.transform.position))
                        {
                            if (Vector3.Distance(bounds[i].center, bone.transform.position) < .8f * (bounds[i].size.x / 2f) && Vector3.Distance(bounds[i].center, bone.transform.position) < .8f * (bounds[i].size.y / 2f))
                            {
                                isSkinned = true;
                                break;
                            }
                        }
                    }
                    if (isSkinned)
                    {
                        Puppet2D_Skinning.ConvertSpriteToMesh(5);
                        geoNames[i] = (originalSpriteName + "_GEO");

                        GameObject geo = GameObject.Find(geoNames[i]);
                        if (i == 0)
                        {
                            objList.Add(geo);
                        }
                        else
                        {
                            objList[objList.Count - 1] = geo;
                        }

                        Selection.objects = objList.ToArray();

                        Puppet2D_Skinning.BindSmoothSkin();

                        Selection.activeGameObject = geo;
                    }
                    else
                    {
                        if (i == 0)
                        {
                            objList.Add(gos[i]);
                        }
                        else
                        {
                            objList[objList.Count - 1] = gos[i];
                        }


                        Selection.objects = objList.ToArray();

                        Puppet2D_Skinning.BindRigidSkin();
                    }
                }

                Puppet2D_GlobalControl globalControl = FindObjectOfType <Puppet2D_GlobalControl>();
                Animator animator = globalControl.gameObject.AddComponent <Animator>();
                Puppet2d_AnimatorController P2dController = globalControl.gameObject.AddComponent <Puppet2d_AnimatorController>();
                P2dController.speed   = 2f * (_hipPoint.position.y - _footLPoint.position.y);
                P2dController.enabled = false;

                animator.runtimeAnimatorController = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(Puppet2D_Editor._puppet2DPath + "/Animation/AutoRig/P2D_AnimatorController.controller", typeof(UnityEditor.Animations.AnimatorController));
                //globalControl.BonesVisiblity = false;
                //globalControl.UpdateVisibility();
                foreach (Puppet2D_GlobalControl gc in globalControls)
                {
                    gc.gameObject.SetActive(true);
                }


                Undo.DestroyObjectImmediate(guide.gameObject);
            }
        }
 // Use this for initialization
 void Start()
 {
     _animator      = gameObject.GetComponent <Animator>();
     _globalControl = gameObject.GetComponent <Puppet2D_GlobalControl>();
 }