// 导出AnimationTrack
        private int ExportAnimationTrack(AnimationTrack animationTrack, JSONObject trackListArr, JSONObject clipListArr)
        {
            JSONObject trackJSON = GenerateBaseTrack(animationTrack, PlaybaleTrackTypeMap["AnimationTrack"]);

            trackJSON.AddField("applyAvatarMask", animationTrack.applyAvatarMask);
            JSONObject infiniteClipOffsetPositionArr = new JSONObject(JSONObject.Type.ARRAY);
            JSONObject infiniteClipOffsetRotationArr = new JSONObject(JSONObject.Type.ARRAY);

            trackJSON.AddField("infiniteClipOffsetPosition", infiniteClipOffsetPositionArr);
            trackJSON.AddField("infiniteClipOffsetRotation", infiniteClipOffsetRotationArr);
#if UNITY_2018_3_OR_NEWER
            trackJSON.AddField("trackOffset", TrackOffsetMap[animationTrack.trackOffset]);
#else
            if (animationTrack.applyOffsets)
            {
                trackJSON.AddField("trackOffset", TrackOffsetMap["ApplyTransformOffsets"]);
            }
            else
            {
                trackJSON.AddField("trackOffset", TrackOffsetMap["Auto"]);
            }
#endif
#if UNITY_2019_1_OR_NEWER
            infiniteClipOffsetPositionArr.Add(-animationTrack.infiniteClipOffsetPosition.x);
            infiniteClipOffsetPositionArr.Add(animationTrack.infiniteClipOffsetPosition.y);
            infiniteClipOffsetPositionArr.Add(animationTrack.infiniteClipOffsetPosition.z);
            infiniteClipOffsetRotationArr.Add(-animationTrack.infiniteClipOffsetRotation.x);
            infiniteClipOffsetRotationArr.Add(animationTrack.infiniteClipOffsetRotation.y);
            infiniteClipOffsetRotationArr.Add(animationTrack.infiniteClipOffsetRotation.z);
            infiniteClipOffsetRotationArr.Add(-animationTrack.infiniteClipOffsetRotation.w);
#else
            infiniteClipOffsetPositionArr.Add(-animationTrack.openClipOffsetPosition.x);
            infiniteClipOffsetPositionArr.Add(animationTrack.openClipOffsetPosition.y);
            infiniteClipOffsetPositionArr.Add(animationTrack.openClipOffsetPosition.z);
            infiniteClipOffsetRotationArr.Add(-animationTrack.openClipOffsetRotation.x);
            infiniteClipOffsetRotationArr.Add(animationTrack.openClipOffsetRotation.y);
            infiniteClipOffsetRotationArr.Add(animationTrack.openClipOffsetRotation.z);
            infiniteClipOffsetRotationArr.Add(-animationTrack.openClipOffsetRotation.w);
#endif
            if (animationTrack.avatarMask != null)
            {
                WXAvatarMask mask = new WXAvatarMask(animationTrack.avatarMask);
                string       uid  = AddDependencies(mask);
                trackJSON.AddField("avatarMask", uid);
            }
            else
            {
                trackJSON.AddField("avatarMask", new JSONObject(JSONObject.Type.NULL));
            }

            JSONObject positionArr = new JSONObject(JSONObject.Type.ARRAY);
            positionArr.Add(-animationTrack.position.x);
            positionArr.Add(animationTrack.position.y);
            positionArr.Add(animationTrack.position.z);
            trackJSON.AddField("position", positionArr);

            JSONObject rotationArr = new JSONObject(JSONObject.Type.ARRAY);
            rotationArr.Add(-animationTrack.rotation.x);
            rotationArr.Add(animationTrack.rotation.y);
            rotationArr.Add(animationTrack.rotation.z);
            rotationArr.Add(-animationTrack.rotation.w);
            trackJSON.AddField("rotation", rotationArr);

            JSONObject matchTargetFieldsJSON = new JSONObject(JSONObject.Type.OBJECT);
            trackJSON.AddField("matchTargetFields", matchTargetFieldsJSON);
            matchTargetFieldsJSON.AddField("PositionX", (animationTrack.matchTargetFields & MatchTargetFields.PositionX) == MatchTargetFields.PositionX);
            matchTargetFieldsJSON.AddField("PositionY", (animationTrack.matchTargetFields & MatchTargetFields.PositionY) == MatchTargetFields.PositionY);
            matchTargetFieldsJSON.AddField("PositionZ", (animationTrack.matchTargetFields & MatchTargetFields.PositionZ) == MatchTargetFields.PositionZ);
            matchTargetFieldsJSON.AddField("RotationX", (animationTrack.matchTargetFields & MatchTargetFields.RotationX) == MatchTargetFields.RotationX);
            matchTargetFieldsJSON.AddField("RotationY", (animationTrack.matchTargetFields & MatchTargetFields.RotationY) == MatchTargetFields.RotationY);
            matchTargetFieldsJSON.AddField("RotationZ", (animationTrack.matchTargetFields & MatchTargetFields.RotationZ) == MatchTargetFields.RotationZ);

            UnityEditor.SerializedObject   serializedObject = new UnityEditor.SerializedObject(animationTrack);
            UnityEditor.SerializedProperty serializedClip   = serializedObject.FindProperty("m_Clips");

            JSONObject clipsIndexArr = trackJSON.GetField("clips");
            if (animationTrack.inClipMode) // 普通clip
            // 貌似有时候序列化的m_Clips顺序跟 timelineClipList 的顺序对不上,但是很难复现。没有找到顺序可以必定对上的方法,先这样吧
            {
                IEnumerable <TimelineClip> timelineClipList = animationTrack.GetClips();
                int num = 0;
                foreach (TimelineClip timelineClip in timelineClipList)
                {
                    JSONObject clipJSON = GenerateBaseTimelineClip(timelineClip, PlaybaleClipTypeMap["Animation"]);
                    JSONObject clipData = new JSONObject(JSONObject.Type.OBJECT);
                    float      m_PostExtrapolationTime = (float)serializedClip.FindPropertyRelative("Array.data[" + num + "].m_PostExtrapolationTime").doubleValue;
                    float      m_PreExtrapolationTime  = (float)serializedClip.FindPropertyRelative("Array.data[" + num + "].m_PreExtrapolationTime").doubleValue;
                    clipJSON.SetField("postExtrapolationTime", m_PostExtrapolationTime);
                    clipJSON.SetField("preExtrapolationTime", m_PreExtrapolationTime);
                    clipJSON.AddField("data", clipData);

                    bool m_Recordable = serializedClip.FindPropertyRelative("Array.data[" + num + "].m_Recordable").boolValue;
                    clipData.AddField("recordable", m_Recordable);

                    string clipPath = ExportAnimationClip(timelineClip.animationClip);
                    if (string.IsNullOrEmpty(clipPath))
                    {
                        clipData.AddField("clip", JSONObject.nullJO);
                    }
                    else
                    {
                        clipData.AddField("clip", clipPath);
                    }


                    AnimationPlayableAsset asset = (AnimationPlayableAsset)timelineClip.asset;
                    // clipData.AddField("clipCaps", ClipCapsMap.ContainsKey(timelineClip.clipCaps) ? ClipCapsMap[timelineClip.clipCaps] : ClipCapsMap[ClipCaps.None]);
                    // clipData.AddField("duration", (float)asset.duration);
#if UNITY_2018_3_OR_NEWER
                    // 2018_3才开始支持
                    clipData.AddField("applyFootIK", asset.applyFootIK);
#else
                    clipData.AddField("applyFootIK", false);
#endif

#if UNITY_2019_1_OR_NEWER
                    // 2019_1才开始支持
                    clipData.AddField("loop", LoopModeMap[asset.loop]);
#else
                    clipData.AddField("loop", LoopModeMap["UseSourceAsset"]);
#endif

                    clipData.AddField("useTrackMatchFields", asset.useTrackMatchFields);

                    JSONObject clipMatchTargetFieldsJSON = new JSONObject(JSONObject.Type.OBJECT);
                    clipData.AddField("matchTargetFields", clipMatchTargetFieldsJSON);
                    clipMatchTargetFieldsJSON.AddField("PositionX", (asset.matchTargetFields & MatchTargetFields.PositionX) == MatchTargetFields.PositionX);
                    clipMatchTargetFieldsJSON.AddField("PositionY", (asset.matchTargetFields & MatchTargetFields.PositionY) == MatchTargetFields.PositionY);
                    clipMatchTargetFieldsJSON.AddField("PositionZ", (asset.matchTargetFields & MatchTargetFields.PositionZ) == MatchTargetFields.PositionZ);
                    clipMatchTargetFieldsJSON.AddField("RotationX", (asset.matchTargetFields & MatchTargetFields.RotationX) == MatchTargetFields.RotationX);
                    clipMatchTargetFieldsJSON.AddField("RotationY", (asset.matchTargetFields & MatchTargetFields.RotationY) == MatchTargetFields.RotationY);
                    clipMatchTargetFieldsJSON.AddField("RotationZ", (asset.matchTargetFields & MatchTargetFields.RotationZ) == MatchTargetFields.RotationZ);

                    JSONObject clipPositionArr = new JSONObject(JSONObject.Type.ARRAY);
                    clipPositionArr.Add(-asset.position.x);
                    clipPositionArr.Add(asset.position.y);
                    clipPositionArr.Add(asset.position.z);
                    clipData.AddField("position", clipPositionArr);

                    JSONObject clipRotationArr = new JSONObject(JSONObject.Type.ARRAY);
                    clipRotationArr.Add(-asset.rotation.x);
                    clipRotationArr.Add(asset.rotation.y);
                    clipRotationArr.Add(asset.rotation.z);
                    clipRotationArr.Add(-asset.rotation.w);
                    clipData.AddField("rotation", clipRotationArr);

                    clipsIndexArr.Add(ExportTimelineClip(clipJSON, clipListArr));
                    num++;
                }
            }
            else // infiniteClip
            {
#if UNITY_2019_1_OR_NEWER
                // 2019_1才开始支持
                AnimationClip infiniteClip = animationTrack.infiniteClip;
#else
                // 序列化取出私有变量
                UnityEditor.SerializedObject   trackSerializedObject = new UnityEditor.SerializedObject(animationTrack);
                UnityEditor.SerializedProperty animClipSerialize     = trackSerializedObject.FindProperty("m_AnimClip");
                AnimationClip infiniteClip = animClipSerialize.objectReferenceValue as AnimationClip;
#endif
                string infinityClipPath = ExportAnimationClip(infiniteClip);
                if (string.IsNullOrEmpty(infinityClipPath))
                {
                    trackJSON.SetField("infinityClip", JSONObject.nullJO);
                }
                else
                {
                    trackJSON.SetField("infinityClip", infinityClipPath);
                }
            }
            // 导出子track
            JSONObject childrenJSON = trackJSON.GetField("children");
            IEnumerable <TrackAsset> childTrackAssetList = animationTrack.GetChildTracks();
            List <int> indexList = ExportTrackList(childTrackAssetList, trackListArr, clipListArr);
            foreach (int index in indexList)
            {
                childrenJSON.Add(index);
            }
            return(ExportTrack(trackJSON, trackListArr));
        }
Example #2
0
        protected override JSONObject ExportResource(ExportPreset preset)
        {
            if ((UnityEngine.Object)controller == (UnityEngine.Object)null)
            {
                Debug.LogWarning("Warning(Code:1002) : " + gameObject.name + "'s Animator Compoment must have a Controller!");
                return(null);
            }
            else
            {
                JSONObject controllerJson = new JSONObject(JSONObject.Type.OBJECT);
                // get parameter info
                JSONObject parameter = new JSONObject(JSONObject.Type.ARRAY);
                AnimatorControllerParameter[] PArray = controller.parameters;
                for (int i = 0; i < PArray.Length; i++)
                {
                    AnimatorControllerParameter p = PArray[i];
                    bool alreadyExit = false;
                    var  singleP     = controllerInfo.parameters.AddObject(p.name, out alreadyExit);
                    if (!alreadyExit)
                    {
                        singleP.Value.AddField("name", p.name);
                        int type;
                        ParameterType.TryGetValue(p.type, out type);
                        singleP.Value.AddField("type", type);
                        if (p.type == AnimatorControllerParameterType.Bool)
                        {
                            singleP.Value.AddField("default", animator.GetBool(p.nameHash));
                        }
                        if (p.type == AnimatorControllerParameterType.Float)
                        {
                            singleP.Value.AddField("default", animator.GetFloat(p.nameHash));
                        }
                        if (p.type == AnimatorControllerParameterType.Int)
                        {
                            singleP.Value.AddField("default", animator.GetInteger(p.nameHash));
                        }
                        if (p.type == AnimatorControllerParameterType.Trigger)
                        {
                            singleP.Value.AddField("default", false);
                        }
                    }
                }

                AnimatorControllerLayer[] layers = controller.layers;
                int num = layers.Length;
                for (int i = 0; i < num; i++)
                {
                    AnimatorControllerLayer layer = layers[i];
                    JSONObject layerJSON          = new JSONObject(JSONObject.Type.OBJECT);

                    AnimatorStateMachine machine = layer.stateMachine;
                    TraverseStateMachine(ref machine);
                    layerJSON.AddField("name", layer.name);
                    bool isExist = false;
                    var  keyPair = controllerInfo.machines.AddObject(machine, out isExist);
                    int  id      = keyPair.Key;
                    layerJSON.AddField("stateMachine", id);
                    if (layer.blendingMode == AnimatorLayerBlendingMode.Override)
                    {
                        layerJSON.AddField("blending", 0);
                    }
                    if (layer.blendingMode == AnimatorLayerBlendingMode.Additive)
                    {
                        layerJSON.AddField("blending", 1);
                    }
                    // layerJSON.AddField("IKPass", layer.iKPass);
                    if (i == 0)
                    {
                        layerJSON.AddField("weight", 1);
                    }
                    else
                    {
                        layerJSON.AddField("weight", layer.defaultWeight);
                    }
                    if (layer.avatarMask != null)
                    {
                        WXAvatarMask mask = new WXAvatarMask(layer.avatarMask);
                        string       uid  = AddDependencies(mask);
                        layerJSON.AddField("mask", uid);
                    }
                    else
                    {
                        layerJSON.AddField("mask", new JSONObject(JSONObject.Type.NULL));
                    }
                    controllerInfo.layers.Add(layerJSON);
                }


                HandleStatesPositionInfo(layers);

                JSONObject editorInfoJson = new JSONObject(JSONObject.Type.OBJECT);
                editorInfoJson.AddField("stateMachines", controllerInfo.stateMachinesPositionInfo.ToJSON());
                editorInfoJson.AddField("states", controllerInfo.statesPositionInfo);

                if (gameObject.GetComponent <Animator>().runtimeAnimatorController.GetType() == typeof(AnimatorOverrideController))
                {
                    controllerJson.AddField("name", ((AnimatorOverrideController)gameObject.GetComponent <Animator>().runtimeAnimatorController).name);
                }
                else if (gameObject.GetComponent <Animator>().runtimeAnimatorController.GetType() == typeof(AnimatorController))
                {
                    controllerJson.AddField("name", ((AnimatorController)gameObject.GetComponent <Animator>().runtimeAnimatorController).name);
                }
                controllerJson.AddField("layers", controllerInfo.layers);
                controllerJson.AddField("stateMachines", controllerInfo.machines.ToJSON());
                controllerJson.AddField("states", controllerInfo.states.ToJSON());
                controllerJson.AddField("transitions", controllerInfo.transitions);
                controllerJson.AddField("blendTrees", controllerInfo.blendTrees.ToJSON());
                controllerJson.AddField("parameters", controllerInfo.parameters.ToJSON());
                controllerJson.AddField("editorInfo", editorInfoJson);
                return(controllerJson);
            }
        }