void AddClip(AnimationClip clip) { JEAnimationClip aclip = new JEAnimationClip(); aclip.name = clip.name; aclip.length = clip.length; clips.Add(aclip); AnimationClipCurveData[] curveData = AnimationUtility.GetAllCurves(clip, true); // EditorCurveBinding[] curveData2 = AnimationUtility.GetCurveBindings(clip); // object references foreach (var binding in AnimationUtility.GetObjectReferenceCurveBindings(clip)) { ObjectReferenceKeyframe[] oKeyframes = AnimationUtility.GetObjectReferenceCurve(clip, binding); var boneName = binding.propertyName; if (binding.path != "") { boneName = binding.path + ":" + boneName; } var keyframes = new List <JEKeyframe>(); JEKeyframe keyframe; for (var a = 0; a < oKeyframes.Length; a++) { keyframe = new JEKeyframe(); keyframe.time = oKeyframes[a].time; if (oKeyframes[a].value is Sprite) { Sprite spr = (Sprite)oKeyframes[a].value; keyframe.name = spr.name; } keyframes.Add(keyframe); } // fill with temp image for last frame keyframe = new JEKeyframe(); keyframe.time = clip.length; if (oKeyframes[oKeyframes.Length - 1].value is Sprite) { Sprite spr = (Sprite)oKeyframes[oKeyframes.Length - 1].value; keyframe.name = spr.name; } keyframes.Add(keyframe); aclip.keyframes[boneName] = keyframes; } // curve data Dictionary <string, List <AnimationClipCurveData> > animdata = new Dictionary <string, List <AnimationClipCurveData> >(); for (int i = 0; i < curveData.Length; i++) { AnimationClipCurveData cd = curveData[i]; List <AnimationClipCurveData> nodedata; var name = cd.propertyName; if (cd.path != "") { name = cd.path + ":" + name; } if (!animdata.TryGetValue(name, out nodedata)) { nodedata = animdata[name] = new List <AnimationClipCurveData>(); } nodedata.Add(cd); } JEAnimationClip rclip = new JEAnimationClip(); List <string> rotationNames = new List <string>(); foreach (KeyValuePair <string, List <AnimationClipCurveData> > entry in animdata) { var boneName = entry.Key; bool valueChanged = false; var keyframes = new List <JEKeyframe>(); bool isOnOff = boneName.IndexOf("m_Enabled") != -1; isOnOff = boneName.IndexOf("m_IsActive") != -1; foreach (AnimationClipCurveData cd in entry.Value) { var curve = cd.curve; if (curve.keys.Length == 0) { continue; } for (var a = 0; a < curve.keys.Length; a++) { JEKeyframe keyframe = new JEKeyframe(); keyframe.time = curve.keys[a].time; keyframe.value = curve.keys[a].value; // keyframe.inTangent = curve.keys[a].inTangent; // keyframe.outTangent = curve.keys[a].outTangent; // keyframe.tangentMode = curve.keys[a].tangentMode; if (isOnOff) { keyframe.onOff = true; valueChanged = true; } if (a > 0) { if (curve.keys[a].value != curve.keys[0].value) { valueChanged = true; } } else { valueChanged = true; } keyframes.Add(keyframe); } } if (boneName.IndexOf("m_LocalRotation") != -1) { int indx = boneName.IndexOf("m_LocalRotation"); string name = boneName.Substring(0, indx) + "m_LocalRotation"; if (!rotationNames.Contains(name)) { rotationNames.Add(name); } rclip.keyframes[boneName] = keyframes; } else if (boneName.IndexOf("localEulerAnglesRaw") != -1) { int pos = boneName.IndexOf("localEulerAnglesRaw"); boneName = boneName.Substring(0, pos) + "m_LocalRotation"; aclip.keyframes[boneName] = keyframes; } else if (valueChanged) { aclip.keyframes[boneName] = keyframes; } } while (rotationNames.Count > 0) { string name = rotationNames[0]; rotationNames.RemoveAt(0); List <JEKeyframe> x = rclip.keyframes[name + ".x"]; List <JEKeyframe> y = rclip.keyframes[name + ".y"]; List <JEKeyframe> z = rclip.keyframes[name + ".z"]; List <JEKeyframe> w = rclip.keyframes[name + ".w"]; rclip.keyframes.Remove(name + ".x"); rclip.keyframes.Remove(name + ".y"); rclip.keyframes.Remove(name + ".z"); rclip.keyframes.Remove(name + ".w"); for (var a = 0; a < x.Count; a++) { Quaternion quat = new Quaternion(x[a].value, y[a].value, z[a].value, w[a].value); x[a].value = quat.eulerAngles.z; } aclip.keyframes[name] = x; } }
void AddClip(AnimationClip clip) { JEAnimationClip aclip = new JEAnimationClip(); aclip.name = clip.name; clips.Add(aclip); AnimationClipCurveData[] curveData = AnimationUtility.GetAllCurves(clip, true); Dictionary <string, List <AnimationClipCurveData> > animdata = new Dictionary <string, List <AnimationClipCurveData> > (); for (int i = 0; i < curveData.Length; i++) { AnimationClipCurveData cd = curveData[i]; List <AnimationClipCurveData> nodedata; if (!animdata.TryGetValue(cd.path, out nodedata)) { nodedata = animdata[cd.path] = new List <AnimationClipCurveData>(); } nodedata.Add(cd); } foreach (KeyValuePair <string, List <AnimationClipCurveData> > entry in animdata) { var boneName = entry.Key; var keyframes = aclip.keyframes[boneName] = new List <JEKeyframe>(); float maxTime = 0; foreach (AnimationClipCurveData cd in entry.Value) { var curve = cd.curve; if (curve.keys.Length == 0) { continue; } if (curve.keys[curve.keys.Length - 1].time > maxTime) { maxTime = curve.keys[curve.keys.Length - 1].time; } } Vector3 pos = new Vector3(0, 0, 0); Vector3 scale = new Vector3(1, 1, 1); Quaternion rot = Quaternion.identity; Vector3 lastpos = new Vector3(0, 0, 0); Vector3 lastscale = new Vector3(1, 1, 1); Quaternion lastrot = Quaternion.identity; for (float time = 0.0f; time <= maxTime;) { foreach (AnimationClipCurveData cd in entry.Value) { var curve = cd.curve; float value = curve.Evaluate(time); if (cd.propertyName == "m_LocalScale.x") { scale.x = value; } else if (cd.propertyName == "m_LocalScale.y") { scale.y = value; } else if (cd.propertyName == "m_LocalScale.z") { scale.z = value; } else if (cd.propertyName == "m_LocalPosition.x") { pos.x = value; } else if (cd.propertyName == "m_LocalPosition.y") { pos.y = value; } else if (cd.propertyName == "m_LocalPosition.z") { pos.z = value; } else if (cd.propertyName == "m_LocalRotation.x") { rot.x = value; } else if (cd.propertyName == "m_LocalRotation.y") { rot.y = value; } else if (cd.propertyName == "m_LocalRotation.z") { rot.z = value; } else if (cd.propertyName == "m_LocalRotation.w") { rot.w = value; } else { Debug.Log("Unknown Animtation: " + cd.propertyName); } } bool needFrame = false; float t; t = Math.Abs(lastpos.x - pos.x); if (t < .01f) { needFrame = true; } t = Math.Abs(lastpos.y - pos.y); if (t < .01f) { needFrame = true; } t = Math.Abs(lastpos.z - pos.z); if (t < .01f) { needFrame = true; } t = Math.Abs(lastscale.x - scale.x); if (t < .01f) { needFrame = true; } t = Math.Abs(lastscale.y - scale.y); if (t < .01f) { needFrame = true; } t = Math.Abs(lastscale.z - scale.z); if (t < .01f) { needFrame = true; } t = Math.Abs(lastrot.x - rot.x); if (t < .01f) { needFrame = true; } t = Math.Abs(lastrot.y - rot.y); if (t < .01f) { needFrame = true; } t = Math.Abs(lastrot.z - rot.z); if (t < .01f) { needFrame = true; } t = Math.Abs(lastrot.w - rot.w); if (t < .01f) { needFrame = true; } if (needFrame) { lastpos = pos; lastrot = rot; lastscale = scale; JEKeyframe keyframe = new JEKeyframe(); keyframe.pos = pos; keyframe.rot = rot; keyframe.scale = scale; keyframe.time = time; keyframes.Add(keyframe); } time += .025f; } } }