Example #1
0
    void OnGUI()
    {
        m_Animator = (Animator)EditorGUILayout.ObjectField("Animator", m_Animator, typeof(Animator), true);
        m_AnimType = (ModelImporterAnimationType)EditorGUILayout.EnumPopup("AnimType", m_AnimType);
        if (m_AnimType != ModelImporterAnimationType.Legacy)
        {
            m_AnimType = ModelImporterAnimationType.Generic;
        }

        bool bSet = (m_Animator != null);

        EUtil.PushGUIEnable(bSet);
        if (EUtil.Button("Convert Animation!", bSet ? Color.green : Color.red))
        {
            if (!m_Animator.isHuman)
            {
                EUtil.ShowNotification("The model is not in Humanoid rig!");
                return;
            }

            // save xforms recursively
            m_SavedPose = EUtil.CacheXformData(m_Animator.transform);

            m_Animator.Update(0);
#if !U5
            var ainfos = m_Animator.GetCurrentAnimationClipState(0); //only effect after Update is called
#else
            var ainfos = m_Animator.GetCurrentAnimatorClipInfo(0);   //only effect after Update is called
#endif
            if (ainfos.Length == 0)
            {
                EUtil.ShowNotification("No clip in AnimationController!");
            }
            else
            {
                AnimationClip clip             = ainfos[0].clip;
                string        oldClipAssetPath = AssetDatabase.GetAssetPath(clip);
                string        newClipAssetPath = PathUtil.StripExtension(oldClipAssetPath) + NEW_CLIP_POSTFIX + m_AnimType + ".anim";

                string filePath = EditorUtility.SaveFilePanel("Select export file path",
                                                              Path.GetDirectoryName(newClipAssetPath),
                                                              Path.GetFileNameWithoutExtension(newClipAssetPath),
                                                              "anim");
                if (filePath.Length > 0)
                {
                    filePath = PathUtil.FullPath2ProjectPath(filePath);
                    _ConvertAnim(filePath);
                }
                else
                {
                    EUtil.ShowNotification("Conversion Cancelled...");
                }
            }

            //apply the original pose back
            EUtil.ApplyXformData(m_Animator.transform, m_SavedPose);
        }
        EUtil.PopGUIEnable();
    }