public ExportAnimation(AnimationClip clip) { Time = clip.length; FrameRate = clip.frameRate; AnimationClipCurveData[] data = AnimationUtility.GetAllCurves(clip, true); CurveData = new AnimCurveData[data.Length]; for (int i = 0; i < data.Length; i++) { CurveData[i] = ExprotCurve(data[i]); } }
public AnimCurveData GetAnimCurveData(string key) { AnimCurveData data = null; if (!m_list.TryGetValue(key, out data)) { TextAsset asset = PoolManager.Singleton.LoadWithoutInstantiate <TextAsset>(key); BinaryHelper helper = new BinaryHelper(asset.bytes); data = new AnimCurveData(); data.Read(helper); m_list.Add(key, data); } return(data); }
void Update() { if (!gameObject.activeSelf) { return; } AnimCurveData data = AnimCurveDataManager.Singleton.GetAnimCurveData("Anim/" + m_pathName + ".bytes"); int key = (int)((data.AnimCurveInfoList.Count - 1) * Percent); if (key <= m_lastKey) { return; } if (key >= data.AnimCurveInfoList.Count) { Debug.LogWarning("曲线数据出错,当前key是:" + key.ToString() + ", name is " + m_pathName); return; } m_lastKey = key; foreach (var item in m_objList) { Renderer[] renders = item.GetComponents <Renderer>(); foreach (Renderer r in renders) { KeyframeData info = data.AnimCurveInfoList[key]; foreach (var propItem in info.PropertyInfoList) { if (r.material.HasProperty(propItem.m_name)) { if (propItem.m_color != Color.white) { r.material.SetColor(propItem.m_name, propItem.m_color); } if (propItem.m_value != float.MinValue) { r.material.SetFloat(propItem.m_name, propItem.m_value); } } } } } }
AnimCurveData ExprotCurve(AnimationClipCurveData data) { AnimCurveData curve = new AnimCurveData(); curve.path = data.path; curve.propertyName = data.propertyName; Keyframe[] keys = data.curve.keys; curve.Alloc(keys.Length); for (int i = 0; i < keys.Length; i++) { curve.inTangent[i] = keys[i].inTangent; curve.outTangent[i] = keys[i].outTangent; curve.tangentMode[i] = keys[i].tangentMode; curve.time[i] = keys[i].time; curve.value[i] = keys[i].value; } return(curve); }
private void GetData(float time, AnimationClipCurveData[] array, AnimCurveData data) { KeyframeData info = new KeyframeData(); foreach (AnimationClipCurveData item in array) { float value = item.curve.Evaluate(time); string name = item.propertyName; name = name.Replace("material.", ""); if (name.Contains(".r") || name.Contains(".g") || name.Contains(".b") || name.Contains(".a")) { string key = name.Substring(0, name.Length - 2); KeyframeData.PropertyInfo propInfo = info.PropertyInfoList.Find(i => i.m_name == key); if (propInfo == null) { Color c = Color.white; if (name.Contains(".r")) { c.r = value; } else if (name.Contains(".g")) { c.g = value; } else if (name.Contains(".b")) { c.b = value; } else if (name.Contains(".a")) { c.a = value; } propInfo = new KeyframeData.PropertyInfo(key); propInfo.m_color = c; info.PropertyInfoList.Add(propInfo); } else { if (name.Contains(".r")) { propInfo.m_color.r = value; } else if (name.Contains(".g")) { propInfo.m_color.g = value; } else if (name.Contains(".b")) { propInfo.m_color.b = value; } else if (name.Contains(".a")) { propInfo.m_color.a = value; } } } else { KeyframeData.PropertyInfo propInfo = info.PropertyInfoList.Find(i => i.m_name == name); if (propInfo == null) { propInfo = new KeyframeData.PropertyInfo(name); propInfo.m_value = value; info.PropertyInfoList.Add(propInfo); } else { propInfo.m_value = value; } } } data.AnimCurveInfoList.Add(info); }
void OnGUI() { this.Repaint(); AnimationClip clip = Selection.activeObject as AnimationClip; if (clip == null) { return; } //animation: xxx.anim string animName = clip.name + ".anim"; GUIContent labelAnimName = new GUIContent(animName, ""); EditorGUILayout.LabelField(LABEL_NAME_ANIM, labelAnimName, GUILayout.Width(500f)); //save path: xx/xx/ string path = AssetDatabase.GetAssetPath(clip); if (path == "") { return; } path = Path.GetDirectoryName(path); path = path + "/"; GUIContent labelSavePath = new GUIContent(path, ""); EditorGUILayout.LabelField(LABEL_PATH_SAVE, labelSavePath, GUILayout.Width(500f)); //save name: Assets/Resources/Anim/xxx.bytes string saveName = "Assets/Resources/Anim/" + clip.name + "_bytes.bytes"; GUIContent labelSaveName = new GUIContent(saveName, ""); EditorGUILayout.LabelField(LABEL_NAME_SAVE, labelSaveName, GUILayout.Width(500f)); m_duration = EditorGUILayout.FloatField(LABEL_CURVE_DUR, m_duration); Rect rct = new Rect(80, 80, 80, 30); if (GUI.Button(rct, @"Save")) { AnimCurveData csData = new AnimCurveData(); AnimationClipCurveData[] array = AnimationUtility.GetAllCurves(clip); float time = 0; while (time < clip.length) { GetData(time, array, csData); time += m_duration; } GetData(clip.length, array, csData); string targetPath = saveName; if (File.Exists(targetPath)) { File.Delete(targetPath); } using (FileStream targetFile = new FileStream(targetPath, FileMode.Create)) { BinaryHelper helper = new BinaryHelper(); csData.m_interval = m_duration; csData.Save(helper); byte[] buff = helper.GetBytes(); targetFile.Write(buff, 0, buff.Length); } Debug.Log("save finished"); } }
void UpdateCameraLight() { switch (m_cameraLightType) { case ENCameraLightType.enL2D: { AnimCurveData data = AnimCurveDataManager.Singleton.GetAnimCurveData("Anim/" + m_cameraLightL2D + ".bytes"); float now = Time.time; if (now - m_cameraLightStartTime > data.m_interval || m_cameraLightIndex == -1) { m_cameraLightStartTime = now; ++m_cameraLightIndex; if (m_cameraLightIndex >= data.AnimCurveInfoList.Count) { m_cameraLightType = ENCameraLightType.enIng; m_cameraLightStartTime = now; m_cameraLightIndex = -1; return; } Renderer[] renders = m_darkerObj.GetComponentsInChildren <Renderer>(); foreach (Renderer r in renders) { KeyframeData info = data.AnimCurveInfoList[m_cameraLightIndex % data.AnimCurveInfoList.Count]; foreach (var propItem in info.PropertyInfoList) { if (r.material.HasProperty(propItem.m_name)) { //if (propItem.m_color != Color.white) //{ // r.material.SetColor(propItem.m_name, propItem.m_color); //} if (propItem.m_value != float.MinValue) { r.material.SetFloat(propItem.m_name, propItem.m_value); } } } } } } break; case ENCameraLightType.enIng: if (Time.time - m_cameraLightStartTime > m_cameraLightDuration) { m_cameraLightType = ENCameraLightType.enD2L; m_cameraLightStartTime = Time.time; m_cameraLightIndex = -1; } break; case ENCameraLightType.enD2L: { AnimCurveData data = AnimCurveDataManager.Singleton.GetAnimCurveData("Anim/" + m_cameraLightD2L + ".bytes"); float now = Time.time; if (now - m_cameraLightStartTime > data.m_interval || m_cameraLightIndex == -1) { m_cameraLightStartTime = now; ++m_cameraLightIndex; if (m_cameraLightIndex >= data.AnimCurveInfoList.Count) { m_cameraLightType = ENCameraLightType.enNone; return; } Renderer[] renders = m_darkerObj.GetComponentsInChildren <Renderer>(); foreach (Renderer r in renders) { KeyframeData info = data.AnimCurveInfoList[m_cameraLightIndex % data.AnimCurveInfoList.Count]; foreach (var propItem in info.PropertyInfoList) { if (r.material.HasProperty(propItem.m_name)) { //if (propItem.m_color != Color.white) //{ // r.material.SetColor(propItem.m_name, propItem.m_color); //} if (propItem.m_value != float.MinValue) { r.material.SetFloat(propItem.m_name, propItem.m_value); } } } } } } break; default: break; } }
AnimCurveData ExprotCurve(AnimationClipCurveData data) { AnimCurveData curve = new AnimCurveData(); curve.path = data.path; curve.propertyName = data.propertyName; Keyframe[] keys = data.curve.keys; curve.Alloc(keys.Length); for (int i = 0; i < keys.Length; i++) { curve.inTangent[i] = keys[i].inTangent; curve.outTangent[i] = keys[i].outTangent; curve.tangentMode[i] = keys[i].tangentMode; curve.time[i] = keys[i].time; curve.value[i] = keys[i].value; } return curve; }
public ExportAnimation(AnimationClip clip) { Time = clip.length; FrameRate = clip.frameRate; AnimationClipCurveData[] data = AnimationUtility.GetAllCurves(clip, true); CurveData = new AnimCurveData[data.Length]; for (int i = 0; i < data.Length; i++) { CurveData[i] = ExprotCurve(data[i]); } }