//protected SerializedObject serializedObject { get { return m_Inspector.serializedAssetImporter; } }
        //protected Avatar avatarAsset; // { get { return m_Inspector.avatar; } }

        // This list containt the mecanim's human bones id for each body part
        //protected int[][] m_BodyPartHumanBone =
        //{
        //    new int[] {-1},
        //    new int[] { (int)HumanBodyBones.Hips, (int)HumanBodyBones.Spine, (int)HumanBodyBones.Chest, (int)HumanBodyBones.UpperChest},
        //    new int[] { (int)HumanBodyBones.Neck, (int)HumanBodyBones.Head, (int)HumanBodyBones.LeftEye, (int)HumanBodyBones.RightEye, (int)HumanBodyBones.Jaw},
        //    new int[] { (int)HumanBodyBones.LeftShoulder, (int)HumanBodyBones.LeftUpperArm, (int)HumanBodyBones.LeftLowerArm, (int)HumanBodyBones.LeftHand},
        //    new int[]
        //    {
        //        (int)HumanBodyBones.LeftThumbProximal , (int)HumanBodyBones.LeftThumbIntermediate, (int)HumanBodyBones.LeftThumbDistal,
        //        (int)HumanBodyBones.LeftIndexProximal , (int)HumanBodyBones.LeftIndexIntermediate, (int)HumanBodyBones.LeftIndexDistal,
        //        (int)HumanBodyBones.LeftMiddleProximal , (int)HumanBodyBones.LeftMiddleIntermediate, (int)HumanBodyBones.LeftMiddleDistal,
        //        (int)HumanBodyBones.LeftRingProximal , (int)HumanBodyBones.LeftRingIntermediate, (int)HumanBodyBones.LeftRingDistal,
        //        (int)HumanBodyBones.LeftLittleProximal , (int)HumanBodyBones.LeftLittleIntermediate, (int)HumanBodyBones.LeftLittleDistal
        //    },
        //    new int[] { (int)HumanBodyBones.RightShoulder, (int)HumanBodyBones.RightUpperArm, (int)HumanBodyBones.RightLowerArm, (int)HumanBodyBones.RightHand},
        //    new int[]
        //    {
        //        (int)HumanBodyBones.RightThumbProximal , (int)HumanBodyBones.RightThumbIntermediate, (int)HumanBodyBones.RightThumbDistal,
        //        (int)HumanBodyBones.RightIndexProximal , (int)HumanBodyBones.RightIndexIntermediate, (int)HumanBodyBones.RightIndexDistal,
        //        (int)HumanBodyBones.RightMiddleProximal , (int)HumanBodyBones.RightMiddleIntermediate, (int)HumanBodyBones.RightMiddleDistal,
        //        (int)HumanBodyBones.RightRingProximal , (int)HumanBodyBones.RightRingIntermediate, (int)HumanBodyBones.RightRingDistal,
        //        (int)HumanBodyBones.RightLittleProximal , (int)HumanBodyBones.RightLittleIntermediate, (int)HumanBodyBones.RightLittleDistal
        //    },
        //    new int[] { (int)HumanBodyBones.LeftUpperLeg, (int)HumanBodyBones.LeftLowerLeg, (int)HumanBodyBones.LeftFoot, (int)HumanBodyBones.LeftToes},
        //    new int[] { (int)HumanBodyBones.RightUpperLeg, (int)HumanBodyBones.RightLowerLeg, (int)HumanBodyBones.RightFoot, (int)HumanBodyBones.RightToes}
        //};

        //public AvatarMappingEditor()
        //{
        //    m_BodyPartToggle = new bool[(int)BodyPart.Last];
        //    m_BodyPartFoldout = new bool[(int)BodyPart.Last];
        //    for (int i = 0; i < (int)BodyPart.Last; i++)
        //    {
        //        m_BodyPartToggle[i] = false;
        //        m_BodyPartFoldout[i] = true;
        //    }
        //}


        public void Init(GameObject go, SkeletonBone[] bones, HumanTemplate template)
        {
            if (go == null)
            {
                return;
            }
            gameObject = go;
            //serializedObject = so; // SerializedObject so = new SerializedObject(assetImporter);
            modelBones = MyAvatarSetupTool.GetModelBones(gameObject.transform, false, null);

            //m_IsBiped = AvatarBipedMapper.IsBiped(gameObject.transform, null);

            // Handle human bones
            if (m_Bones == null)
            {
                m_Bones = MyAvatarSetupTool.GetHumanBones(bones, modelBones, template);
            }
            //ValidateMapping();

            //m_CurrentTransformEditorFoldout = true;
            //m_HasSkinnedMesh = (gameObject.GetComponentInChildren<SkinnedMeshRenderer>() != null);

            // Handle pose
            //InitPose();

            //// Repaint
            //SceneView.RepaintAll();
        }
    private static HumanDescription ReadHumanDescription()
    {
        HumanDescription humanDescription = new HumanDescription();
        List <HumanBone> humanBones       = new List <HumanBone>();

        HumanTemplate template = Resources.Load(humanTemplateFile) as HumanTemplate;

        string[] boneNames = HumanTrait.BoneName;

        //List<string> mapping = new List<string>();
        foreach (string boneName in boneNames)
        {
            HumanBone newBone = new HumanBone();
            newBone.humanName = boneName;
            newBone.boneName  = template.Find(boneName);

            if (newBone.boneName != "")
            {
                HumanLimit limit = new HumanLimit();
                limit.useDefaultValues = true;
                newBone.limit          = limit;
                humanBones.Add(newBone);
            }
        }

        humanDescription.human             = humanBones.ToArray();
        humanDescription.upperArmTwist     = 0.5f;
        humanDescription.lowerArmTwist     = 0.5f;
        humanDescription.upperLegTwist     = 0.5f;
        humanDescription.lowerLegTwist     = 0.5f;
        humanDescription.armStretch        = 0.05f;
        humanDescription.legStretch        = 0.05f;
        humanDescription.feetSpacing       = 0.0f;
        humanDescription.hasTranslationDoF = true;

        //if (secondPass && _isAnimation)
        {
            //skeletonDescription = modelImporter.humanDescription.skeleton;
            List <SkeletonBone> skeletonBones = new List <SkeletonBone>();
            for (int i = 0; i < skeletonDescription.Length; i++)
            {
                SkeletonBone newSkeletonBone = new SkeletonBone();
                newSkeletonBone.name     = skeletonDescription[i].name;
                newSkeletonBone.position = skeletonDescription[i].position;
                newSkeletonBone.rotation = skeletonDescription[i].rotation;
                newSkeletonBone.scale    = skeletonDescription[i].scale;
                //newSkeletonBone.transformModified = skeletonDescription[i].transformModified;

                skeletonBones.Add(newSkeletonBone);
            }

            humanDescription.skeleton = skeletonBones.ToArray();
        }
        return(humanDescription);
    }
    void OnPostprocessModel(GameObject go)
    {
        if (!open)
        {
            return;
        }

        if (thisImporter != assetImporter)
        {
            Debug.Log("ignor importer");
            return;
        }
        if (!MyClass.IsWorking())
        {
            return;
        }
        Debug.Log("OnPostprocessModel:" + assetPath);


        //gameObject = go;
        //if (skeletonDescription == null)
        //{
        //    PrepareSkeletonDescription(modelImporter);
        //}
        //if (secondPass)
        //{
        //    serializedObject = new SerializedObject(assetImporter);
        //    MyAvatarMappingEditor editor = new MyAvatarMappingEditor();
        //    editor.Init(go, serializedObject);
        //    MyAvatarSetupTool.BoneWrapper[] bones = editor.MakePoseValid();
        //}


        if (!secondPass)
        {
            //serializedObject = new SerializedObject(assetImporter);
            //MyAvatarMappingEditor editor = new MyAvatarMappingEditor();
            //editor.Init(go, serializedObject);
            //editor.MakePoseValid();

            //Animator a = go.GetComponent<Animator>();
            //if (a)
            //{
            //    a.Rebind();
            //}
            go.transform.parent   = null;
            go.transform.position = Vector3.zero;
            go.transform.rotation = Quaternion.identity;

            List <SkeletonBone> skeletonBones = new List <SkeletonBone>();
            AddSkeletonBoneRecursive(go.transform, skeletonBones);
            skeletonDescription = skeletonBones.ToArray();
            Debug.Log(skeletonBones);

            HumanTemplate template = Resources.Load(humanTemplateFile) as HumanTemplate;

            // TODO modify bone
            serializedObject = new SerializedObject(assetImporter);
            MyAvatarMappingEditor editor = new MyAvatarMappingEditor();
            editor.Init(go, skeletonDescription, template);
            //editor.Init(go, serializedObject);
            MyAvatarSetupTool.BoneWrapper[] bones = editor.MakePoseValid();
            for (int i = 0; i < skeletonDescription.Length; i++)
            {
                //SkeletonBone skeleton = skeletonDescription[i];
                for (int j = 0; j < bones.Length; j++)
                {
                    Transform bone = bones[j].bone;
                    if (bone && bone.name == skeletonDescription[i].name)
                    {
                        skeletonDescription[i].position = bone.localPosition;
                        skeletonDescription[i].rotation = bone.localRotation;
                        skeletonDescription[i].scale    = bone.localScale;
                    }
                }
            }


            //ModelImporter modelImporter = assetImporter as ModelImporter;

            //HumanDescription humDesc = modelImporter.humanDescription;
            //humDesc.skeleton = skeletonBones.ToArray();



            AssetDatabase.ImportAsset(assetPath);
            secondPass = true;
        }
        else
        {
            secondPass = false;


            //Animator anim = go.GetComponent<Animator>();
            //Avatar avatar = anim.avatar;

            //HumanPose pose = new HumanPose();
            //HumanPoseHandler handler = new HumanPoseHandler(go.GetComponent<Animator>().avatar, go.transform);
            //handler.SetHumanPose(ref pose);

            //    AvatarMappingEditor
            //    AvatarSubEditor
            //    AvatarSetupTool.MakePoseValid(this.m_Bones);
            //AvatarSetupTool.TransferPoseToDescription(base.serializedObject, base.root);
        }
    }