Export and import of Unity animation data

1. Use JSON to store and read animation data flow in Unity


1. Define the animation data class
2. Serialize the animation data into a JSON string. This can be achieved using the JsonUtility.ToJson() method.
3. Save the JSON string to a file, such as using File.WriteAllText() to write.
4. When reading, read the JSON string from the file and use JsonUtility.FromJson() to deserialize it back to the animation data class.
5. According to the deserialized animation data class, obtain the corresponding AnimationClip for playback and other operations

2. Animation data storage and reading method


API------AnimationUnitily
1. Obtain the float curve binding data through AnimationUtility.GetCurveBindings , and return an array such as EditorCurveBinding[] to store motion data.
2. Obtain the AnimationCurve object through the AnimationUtility.GetEditorCurve method and store the collection of key frames
 

Obtain all animation events related to the animation clip through the AnimationUtility.GetCurveBindings interface, and then obtain the float curve pointed to by the binding through the AnimationUtility.GetEditorCurve interface

binding.path is the path of the object node operated in the model; binding.propertyName is the object node property operated (such as Position, Rotation, Scale, BlendShape, etc.); curve is the keyframe curve; curve.length and curve.keys.Length Both are the number of key frames; curve[i].value in the last loop is the value in the key frame, and curve[i].time is the time in the key frame.

LitJson is used in the code

JsonMapper.ToJson(data) , JsonMapper.ToObject to serialize and deserialize

//AnimationUtility.
    //JsonUtility.ToJson   将动画数据序列化为JSON字符串
    //File.WriteAllText()    将JSON字符串保存到文件中
    int length;

    /// <summary>
    /// 动画数据类
    /// </summary>
    public class AnimationData
    {
        //动画名
        public string name;
        //动画时长
        public float time;
        //3个位置信息
        public Dictionary<string, List<float>> dicPos = new Dictionary<string, List<float>>();
        //事件信息
        public AnimationEvent animEvent;
    }


    private void Start()
    {
        AnimationClip clip = GetComponent<Animation>().clip;
        //实例化
        AnimationData data = new AnimationData();

        //读取信息
        data.name = clip.name;
        data.time = clip.length;

        foreach (var binding in AnimationUtility.GetCurveBindings(clip))
        {
            AnimationCurve curve = AnimationUtility.GetEditorCurve(clip, binding);
            //Debug.Log("/" + binding.propertyName + "\tKeys: " + curve.keys.Length);

            List<float> keyframeValues = new List<float>();
            data.dicPos.Add(binding.propertyName, keyframeValues);
            length = curve.length;
            for (int i = 0; i < curve.length; i++)
            {
                //Debug.Log("Keys" + i + ":" + curve[i].value);
                //添加元素
                keyframeValues.Add(curve[i].value);
            }
        }

        string filePath = Path.Combine(Application.persistentDataPath, "AnimationData");

        //序列化为JSON字符串
        //string json = JsonUtility.ToJson(data);
        string json = JsonMapper.ToJson(data);
        Debug.Log(json);

        //保存文件
        File.WriteAllText(filePath, json);

        //反序列化
        string json1 = File.ReadAllText(filePath);
        AnimationData data1 = JsonMapper.ToObject<AnimationData>(json1);

        
    }

Guess you like

Origin blog.csdn.net/weixin_53163894/article/details/131866832