// 导出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)); }