public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                string  jsonString = System.Text.Encoding.UTF8.GetString(resource.GetMetaData());
                JObject jsonObject = JObject.Parse(jsonString);

                animationClip           = new AnimationClip();
                animationClip.name      = jsonObject.Value <string>("name");
                animationClip.frameRate = jsonObject.Value <float>("frameRate");
                animationClip.wrapMode  = (UnityEngine.WrapMode)jsonObject.Value <int>("wrapMode");
                animationClip.legacy    = true;

                SerializedAnimationClip serializedAnimationClip = ProtocolBufferSerializer.DeserializeAnimationClipData(resource.GetResourceData());

                foreach (int key in serializedAnimationClip.AnimationChannels.Keys)
                {
                    SerializedAnimationChannelName channel = (SerializedAnimationChannelName)key;

                    SerializedAnimationKeyFrame[] keyFrames = serializedAnimationClip.GetChannel(channel);

                    AnimationCurve curve = CreateAnimationCurve(serializedAnimationClip.PostWrapMode, serializedAnimationClip.PreWrapMode, keyFrames);

                    animationClip.SetCurve("", typeof(Transform), AnimationClipUtils.GetAnimationClipChannelName(channel), curve);
                }

                string fieldName = jsonObject.Value <string>("fieldName");
                if (resourceResponse != null)
                {
                    resourceResponse.GetFieldDeserializer.SetField(fieldName, animationClip);
                }

                this.isDeserialized = true;
            }
        }
Example #2
0
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

                        #if UNITY_EDITOR
            string   assetPath     = AssetDatabase.GetAssetPath(animationClip);
            NodeBase animationNode = CheckSharedAnimationClip(this.rootNode, assetPath);

            if (animationNode != null)
            {
                UInt32 animationID = animationNode.GetReferenceID();

                ComponentNode componentNode = new ComponentNode(PCFResourceType.ANIMATIONCLIPREFERENCE, referenceID, null, typeof(UnityEngine.AnimationClip).Name.ToString());

                objNode.AddChildNode(componentNode);

                byte[] bytes = BitConverter.GetBytes(animationID);

                JObject metaData = new JObject();
                metaData["fieldName"] = this.fieldName;

                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));

                resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, bytes);

                serializedAssets.AddResource(referenceID, PCFResourceType.ANIMATIONCLIPREFERENCE, resource);

                this.pointedID = animationID;
            }
            else
            {
                ComponentNode componentNode = new ComponentNode(PCFResourceType.ANIMATIONCLIP, referenceID, assetPath, typeof(UnityEngine.AnimationClip).Name.ToString());

                objNode.AddChildNode(componentNode);

                EditorCurveBinding[] curveBindings = AnimationUtility.GetCurveBindings(animationClip);

                AnimationCurve          initialCurve            = AnimationUtility.GetEditorCurve(animationClip, curveBindings[0]);
                SerializedAnimationClip serializedAnimationClip = new SerializedAnimationClip();

                serializedAnimationClip.PostWrapMode = (int)initialCurve.postWrapMode;
                serializedAnimationClip.PreWrapMode  = (int)initialCurve.preWrapMode;

                for (int i = 0; i < curveBindings.Length; i++)
                {
                    string propertyName = curveBindings[i].propertyName;

                    AnimationCurve animCurve = AnimationUtility.GetEditorCurve(animationClip, curveBindings[i]);

                    SerializedAnimationKeyFrame[] serializedKeyFrames = new SerializedAnimationKeyFrame[animCurve.keys.Length];
                    for (int j = 0; j < animCurve.keys.Length; j++)
                    {
                        serializedKeyFrames[j] = new SerializedAnimationKeyFrame();

                        serializedKeyFrames[j].InTagent   = animCurve.keys[j].inTangent;
                        serializedKeyFrames[j].OutTangent = animCurve.keys[j].outTangent;
                        serializedKeyFrames[j].Time       = animCurve.keys[j].time;
                        serializedKeyFrames[j].Value      = animCurve.keys[j].value;
                    }

                    serializedAnimationClip.AddChannel(AnimationClipUtils.GetAnimationClipChannelName(propertyName), serializedKeyFrames);
                }

                JObject metaData = new JObject();
                metaData["name"]      = animationClip.name;
                metaData["frameRate"] = animationClip.frameRate;
                metaData["wrapMode"]  = (int)animationClip.wrapMode;
                metaData["legacy"]    = animationClip.legacy;
                metaData["fieldName"] = this.fieldName;

                byte[] data = ProtocolBufferSerializer.SerializeAnimationClipData(serializedAnimationClip);

                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None));
                resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, data);

                serializedAssets.AddResource(referenceID, PCFResourceType.ANIMATIONCLIP, resource);

                //Nodes store their resource when serializing
                componentNode.SetSerializer(this);

                //Make sure caller can get the actual material referenceID when serializing its metadata.
                this.pointedID = this.referenceID;
            }
                        #endif
        }