Пример #1
0
        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);

                byte[] metaDataBuffer         = resource.GetMetaData();
                SerializedFieldData fieldData = ProtocolBufferSerializer.DeserializeFieldData(metaDataBuffer);
                string assemblyName           = ProtocolBufferSerializer.GetAssemblyName(fieldData.assemblyType);
                string scriptTypeName         = fieldData.typeName;
                string fieldName = fieldData.fieldName;

                Type scriptType = null;

                //Qualify type check with assembly name, GetType only looks in current assembly otherwise.
                if (!string.IsNullOrEmpty(assemblyName))
                {
                    scriptType = Type.GetType(scriptTypeName + ", " + assemblyName);
                }
                else
                {
                    scriptType = Type.GetType(scriptTypeName);
                }

                if (scriptType != null)
                {
                    Gradient gradient = new Gradient();

                    PropertySetter colorKeySetter = new PropertySetter("colorKeys", (System.Object val) => { gradient.colorKeys = val as GradientColorKey[]; });
                    PropertySetter alphaKeySetter = new PropertySetter("alphaKeys", (System.Object val) => { gradient.alphaKeys = val as GradientAlphaKey[]; });

                    List <PropertySetter> customValueSetters = new List <PropertySetter>();
                    customValueSetters.Add(colorKeySetter);
                    customValueSetters.Add(alphaKeySetter);

                    FieldDeserializer fieldDeserializer = new FieldDeserializer(customValueSetters, gradient);
                    ResourceResponse  response          = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

                    for (int i = 0; i < this.ChildNodes.Count; i++)
                    {
                        UnityNodeBase child = this.ChildNodes[i];

                        child.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                    }

                    if (resourceResponse != null)
                    {
                        resourceResponse.GetFieldDeserializer.SetField(fieldName, gradient);
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #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();

            ComponentNode scriptNode = new ComponentNode(PCFResourceType.CLASS, referenceID, null, this.type.Name.ToString());

            //Parent top level scripts to ObjectNode.
            objNode.AddChildNode(scriptNode);

            FieldInfo[] fields = this.type.GetFields();

            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i].IsPublic && !fields[i].IsStatic)
                {
                    SerializeField(serializedAssets, serializeOptions, scriptNode, postSerializeActions, fields[i]);
                }
            }

            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.type, this.fieldName, this.arrayItem, this.type.Assembly);
            resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.CLASS, resource);
        }
        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);

                if (resource != null)
                {
                    byte[] metaDataBuffer = resource.GetMetaData();
                    SerializedCollectionData collectionData = ProtocolBufferSerializer.DeserializeCollectionData(metaDataBuffer);
                    string assemblyName   = ProtocolBufferSerializer.GetAssemblyName(collectionData.assemblyType);
                    string scriptTypeName = collectionData.typeName;
                    string fieldName      = collectionData.fieldName;
                    int    itemCount      = collectionData.count;
                    Type   scriptType     = null;

                    //Qualify type check with assembly name, GetType only looks in current assembly otherwise.
                    if (!string.IsNullOrEmpty(assemblyName))
                    {
                        scriptType = Type.GetType(scriptTypeName + ", " + assemblyName);
                    }
                    else
                    {
                        scriptType = Type.GetType(scriptTypeName);
                    }

                    if (scriptType != null)
                    {
                        Array array = Array.CreateInstance(scriptType, itemCount);

                        if (array != null)
                        {
                            FieldDeserializer arrayDeserializer = new FieldDeserializer(array);
                            ResourceResponse  response          = new ResourceResponse();
                            response.SetFieldDeserializer(arrayDeserializer);

                            foreach (UnityNodeBase node in this.ChildNodes)
                            {
                                node.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                                arrayDeserializer.IncrementIndex();
                            }
                        }

                        if (resourceResponse != null)
                        {
                            resourceResponse.GetFieldDeserializer.SetField(fieldName, array);
                        }
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #4
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 the datatype is not a primitive look for a class specific serializer to invoke.
            Assembly      assembly       = Assembly.GetAssembly(this.GetType());
            string        namespaceName  = this.GetType().Namespace;
            string        serializerName = namespaceName + "." + this.type.Name + "Serialization";
            Type          serializerType = assembly.GetType(serializerName, false);
            List <UInt32> collectionIDs  = new List <UInt32>();

            //See if there is a custom serializer defined for this type.
            if (serializerType != null)
            {
                ComponentNode componentNode = new ComponentNode(PCFResourceType.POINTERCOLLECTION, referenceID, null, this.type.Name.ToString());
                objNode.AddChildNode(componentNode);

                for (int i = 0; i < this.values.Length; i++)
                {
                    System.Object obj = this.values[i];

                    object     serializerClass = Activator.CreateInstance(serializerType, Convert.ChangeType(obj, this.type), "", true, this.rootNode);
                    MethodInfo serializeMethod = serializerType.GetMethod("Serialize");
                    MethodInfo pointerIDMethod = serializerType.GetMethod("GetPointerID");

                    object[] parameters = new object[4];
                    parameters[0] = serializedAssets;
                    parameters[1] = serializeOptions;
                    parameters[2] = componentNode;
                    parameters[3] = postSerializeActions;

                    serializeMethod.Invoke(serializerClass, parameters);

                    UInt32 refID = (UInt32)pointerIDMethod.Invoke(serializerClass, null);

                    collectionIDs.Add(refID);
                }

                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeCollectionData(this.type, this.fieldName, collectionIDs.Count, collectionIDs.ToArray(), this.type.Assembly);

                resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, null);

                serializedAssets.AddResource(referenceID, PCFResourceType.POINTERCOLLECTION, resource);
            }
        }
Пример #5
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();

            byte[] bytes = null;

            if (this.type == typeof(string))
            {
                if (this.value != null)
                {
                    bytes = System.Text.Encoding.UTF8.GetBytes((string)this.value);
                }
            }
            else if (this.type == typeof(int))
            {
                bytes = BitConverter.GetBytes((int)this.value);
            }
            else if (this.type == typeof(float))
            {
                bytes = BitConverter.GetBytes((float)this.value);
            }
            else if (this.type == typeof(double))
            {
                bytes = BitConverter.GetBytes((double)this.value);
            }
            else if (this.type == typeof(bool))
            {
                bytes = BitConverter.GetBytes((bool)this.value);
            }
            else if (this.type == typeof(byte[]))
            {
                bytes = (byte[])this.value;
            }

            if (bytes != null)
            {
                ComponentNode componentNode = new ComponentNode(PCFResourceType.PRIMITIVE, referenceID, null, this.type.Name.ToString());
                objNode.AddChildNode(componentNode);

                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.type, this.fieldName, this.arrayItem, this.type.Assembly);
                resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, bytes);

                serializedAssets.AddResource(referenceID, PCFResourceType.PRIMITIVE, resource);
            }
        }
Пример #6
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();

            ComponentNode scriptNode = new ComponentNode(PCFResourceType.GRADIENT, referenceID, null, typeof(UnityEngine.Gradient).Name.ToString());

            objNode.AddChildNode(scriptNode);

            GradientColorKey[] colorKeys          = this.gradient.colorKeys;
            System.Object[]    convertedColorKeys = new System.Object[colorKeys.Length];

            for (int i = 0; i < colorKeys.Length; i++)
            {
                GradientColorKey colorKey = colorKeys[i];
                convertedColorKeys[i] = colorKey as System.Object;
            }

            UnitySerializeCollection colorKeySerializer = new UnitySerializeCollection(convertedColorKeys, typeof(UnityEngine.GradientColorKey), null, "colorKeys", rootNode);

            colorKeySerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postserializeActions);


            GradientAlphaKey[] alphaKeys          = this.gradient.alphaKeys;
            System.Object[]    convertedAlphaKeys = new System.Object[alphaKeys.Length];

            for (int i = 0; i < alphaKeys.Length; i++)
            {
                GradientAlphaKey alphaKey = alphaKeys[i];
                convertedAlphaKeys[i] = alphaKey as System.Object;
            }

            UnitySerializeCollection alphaKeySerializer = new UnitySerializeCollection(convertedAlphaKeys, typeof(UnityEngine.GradientAlphaKey), null, "alphaKeys", rootNode);

            alphaKeySerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postserializeActions);

            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.gradient.GetType(), this.fieldName, this.arrayItem, this.gradient.GetType().Assembly);
            resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.GRADIENT, resource);
        }
        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;
            }
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postserializeActions)
        {
            this.referenceID = this.rootNode.GenerateID();

            ComponentNode componentNode = new ComponentNode(PCFResourceType.TRANSFORMPOINTER, referenceID, null, this.transform.GetType().Name.ToString());

            objNode.AddChildNode(componentNode);

            //Find the node this node points to in the tree.
            NodeBase transformNode = FindTransformInTree(rootNode, this.transform.name);
            UInt32   referencedID  = transformNode.GetReferenceID();

            AssetResource resource = new AssetResource(false);

            byte[] metaDataBuffer = ProtocolBufferSerializer.SerializeFieldData(this.transform.GetType(), this.fieldName, this.arrayItem, this.transform.GetType().Assembly);
            resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, BitConverter.GetBytes(referencedID));

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

            this.pointedID = transformNode.GetReferenceID();
        }
Пример #9
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[this.resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                byte[]             metaDataBuffer     = resource.GetMetaData();
                SerializedMaterial serializedMaterial = ProtocolBufferSerializer.DeserializeMaterialData(metaDataBuffer);

                string shaderName = serializedMaterial.ShaderName;
                this.material      = new UnityEngine.Material(Shader.Find(shaderName));
                this.material.name = serializedMaterial.DisplayName;

                ResourceResponse previousRequest = null;

                if (serializedMaterial.MaterialProperties != null)
                {
                    foreach (KeyValuePair <string, SerializedMaterialProperty> pair in serializedMaterial.MaterialProperties)
                    {
                        if (pair.Value != null)
                        {
                            MaterialPropertyType propertyType = (MaterialPropertyType)pair.Value.PropertyType;

                            if (propertyType == MaterialPropertyType.FloatType)
                            {
                                SerializedMaterialFloatProperty floatProperty = pair.Value as SerializedMaterialFloatProperty;
                                this.material.SetFloat(pair.Key, floatProperty.Val);
                            }
                            else if (propertyType == MaterialPropertyType.VectorType)
                            {
                                SerializedMaterialVectorProperty vectorProperty = pair.Value as SerializedMaterialVectorProperty;

                                Vector4 vector = new Vector4(vectorProperty.X, vectorProperty.Y, vectorProperty.Z, vectorProperty.W);
                                this.material.SetVector(pair.Key, vector);
                            }
                            else if (propertyType == MaterialPropertyType.ColorType)
                            {
                                SerializedMaterialColorProperty colorProperty = pair.Value as SerializedMaterialColorProperty;

                                Color color = new Color(colorProperty.R, colorProperty.G, colorProperty.B, colorProperty.A);
                                this.material.SetColor(pair.Key, color);
                            }
                            else if (propertyType == MaterialPropertyType.TextureType)
                            {
                                SerializedMaterialTextureProperty textureProperty = pair.Value as SerializedMaterialTextureProperty;
                                string propertyName = pair.Key;

                                ResourceResponse textureRequest = new ResourceResponse(textureProperty.ReferenceID, (ResourceResponse response) =>
                                {
                                    material.SetTexture(propertyName, response.GetTextureRequest);
                                });

                                if (previousRequest != null)
                                {
                                    textureRequest.SetPreviousResponse(previousRequest);
                                    previousRequest.SetNextResponse(textureRequest);
                                }

                                previousRequest = textureRequest;
                            }
                        }
                    }
                }

                if (previousRequest != null)
                {
                    //Get the first request created.
                    ResourceResponse firstRequest = previousRequest;
                    while (true)
                    {
                        ResourceResponse previous = firstRequest.GetPreviousResponse();

                        if (previous == null)
                        {
                            break;
                        }

                        firstRequest = previous;
                    }

                    //Have each request set by one of the child nodes.
                    for (int i = 0; i < this.ChildNodes.Count; i++)
                    {
                        UnityNodeBase child = this.ChildNodes[i];

                        child.Deserialize(dataBlocks, parentNode, firstRequest, postInstallActions, optimizedLoad);
                    }
                }

                //Finish up and set material to whatever field/object that owns this node.
                ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                if (request != null)
                {
                    request.HandleMaterialResponse(this.material);
                }

                watch.Stop();
                //Debug.Log("Time to deserialize material: " + watch.ElapsedMilliseconds);

                this.isDeserialized = true;
            }
        }
Пример #10
0
        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);

                byte[] metaDataBuffer         = resource.GetMetaData();
                SerializedFieldData fieldData = ProtocolBufferSerializer.DeserializeFieldData(metaDataBuffer);

                if (fieldData != null)
                {
                    byte[]        data = resource.GetResourceData();
                    System.Object obj  = null;

                    if (data != null)
                    {
                        if (fieldData.type == 1)
                        {
                            obj = System.Text.Encoding.UTF8.GetString(data);
                        }
                        else if (fieldData.type == 2)
                        {
                            obj = BitConverter.ToInt32(data, 0);
                        }
                        else if (fieldData.type == 3)
                        {
                            obj = BitConverter.ToUInt32(data, 0);
                        }
                        else if (fieldData.type == 4)
                        {
                            obj = BitConverter.ToSingle(data, 0);
                        }
                        else if (fieldData.type == 5)
                        {
                            obj = BitConverter.ToDouble(data, 0);
                        }
                        else if (fieldData.type == 6)
                        {
                            obj = BitConverter.ToBoolean(data, 0);
                        }
                        else if (fieldData.type == 7)
                        {
                            obj = data;
                        }

                        if (resourceResponse != null)
                        {
                            if (fieldData.arrayItem)
                            {
                                resourceResponse.GetFieldDeserializer.SetArrayItem(obj);
                            }
                            else
                            {
                                resourceResponse.GetFieldDeserializer.SetField(fieldData.fieldName, obj);
                            }
                        }
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #11
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
        }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions)
        {
                        #if UNITY_EDITOR
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            //Check if node hierarchy contains this material already.
            string   path         = AssetDatabase.GetAssetPath(this.material);
            NodeBase materialNode = CheckSharedMaterial(this.rootNode, path);

            //If material is already serialized (aka shared we simply serialize a pointer to it.
            if (materialNode != null)
            {
                //Debug.Log("Shared Material: " + path);

                UInt32 materialID = materialNode.GetReferenceID();

                ComponentNode componentNode = new ComponentNode(PCFResourceType.MATERIALPOINTER, referenceID, null);

                objNode.AddChildNode(componentNode);

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

                //Create serialized asset by converting data to a bytearray and give it to the constructor.
                AssetResource resource = new AssetResource(false);
                resource.Serialize(referenceID, MetaDataType.UNKOWN, null, bytes);

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

                //Make sure caller can get the actual material referenceID when serializing its metadata.
                this.pointedID = materialID;
            }
            else
            {
                ComponentNode componentNode = new ComponentNode(PCFResourceType.MATERIAL, referenceID, path);

                //Material nodes can and are most likely to be children of other component nodes.
                objNode.AddChildNode(componentNode);

                SerializedMaterial serializedMaterial = new SerializedMaterial();
                serializedMaterial.DisplayName = this.material.name;
                serializedMaterial.ShaderName  = this.material.shader.name;

                Dictionary <string, SerializedMaterialProperty> materialProperties = new Dictionary <string, SerializedMaterialProperty>();

                // Iterate through children and get the textures of the material.
                Shader shader = this.material.shader;

                int count = ShaderUtil.GetPropertyCount(shader);
                for (int i = 0; i < count; i++)
                {
                    ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetPropertyType(shader, i);
                    string propertyName = ShaderUtil.GetPropertyName(shader, i);

                    SerializedMaterialProperty property = null;

                    //Color property.
                    if (propertyType == ShaderUtil.ShaderPropertyType.Float || propertyType == ShaderUtil.ShaderPropertyType.Range)
                    {
                        SerializedMaterialFloatProperty prop = new SerializedMaterialFloatProperty();

                        prop.Val          = this.material.GetFloat(propertyName);
                        prop.PropertyType = (int)MaterialPropertyType.FloatType;

                        property = prop;
                    }
                    //Vector property.
                    if (propertyType == ShaderUtil.ShaderPropertyType.Vector)
                    {
                        SerializedMaterialVectorProperty prop = new SerializedMaterialVectorProperty();

                        Vector4 vector = this.material.GetVector(propertyName);
                        prop.X = vector.x;
                        prop.Y = vector.y;
                        prop.Z = vector.z;
                        prop.W = vector.w;

                        prop.PropertyType = (int)MaterialPropertyType.VectorType;

                        property = prop;
                    }
                    //Float property.
                    if (propertyType == ShaderUtil.ShaderPropertyType.Color)
                    {
                        SerializedMaterialColorProperty prop = new SerializedMaterialColorProperty();

                        Color color = this.material.GetColor(propertyName);

                        prop.R = color.r;
                        prop.G = color.g;
                        prop.B = color.b;
                        prop.A = color.a;

                        prop.PropertyType = (int)MaterialPropertyType.ColorType;

                        property = prop;
                    }

                    //Texture property.
                    if (propertyType == ShaderUtil.ShaderPropertyType.TexEnv)
                    {
                        Texture2D texture = (Texture2D)this.material.GetTexture(propertyName);

                        if (texture != null)
                        {
                            UnitySerializeTexture textureSerializer = new UnitySerializeTexture(texture, this.rootNode);
                            textureSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

                            SerializedMaterialTextureProperty prop = new SerializedMaterialTextureProperty();

                            prop.ReferenceID  = textureSerializer.GetReferenceID();
                            prop.PropertyType = (int)MaterialPropertyType.TextureType;

                            property = prop;
                        }
                    }

                    materialProperties.Add(propertyName, property);
                }

                serializedMaterial.MaterialProperties = materialProperties;

                //Create serialized asset by converting data to a bytearray and give it to the constructor.
                AssetResource resource = new AssetResource(false);

                byte[] metaDataBuffer = ProtocolBufferSerializer.SerializedMaterial(serializedMaterial);
                resource.Serialize(this.referenceID, MetaDataType.PROTOBUF, metaDataBuffer, null);

                serializedAssets.AddResource(referenceID, PCFResourceType.MATERIAL, 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
        }
Пример #13
0
        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);

                if (resource != null)
                {
                    byte[] metaDataBuffer         = resource.GetMetaData();
                    SerializedFieldData fieldData = ProtocolBufferSerializer.DeserializeFieldData(metaDataBuffer);

                    string assemblyName = ProtocolBufferSerializer.GetAssemblyName(fieldData.assemblyType);

                    if (assemblyName != null)
                    {
                        Type scriptType = null;

                        //Qualify type check with assembly name, GetType only looks in current assembly otherwise.
                        if (!string.IsNullOrEmpty(assemblyName))
                        {
                            scriptType = Type.GetType(fieldData.typeName + ", " + assemblyName);
                        }
                        else
                        {
                            scriptType = Type.GetType(fieldData.typeName);
                        }

                        if (scriptType != null)
                        {
                            System.Object objectInstance = Activator.CreateInstance(scriptType);

                            FieldDeserializer fieldDeserializer = new FieldDeserializer(scriptType.GetFields(), objectInstance);
                            ResourceResponse  response          = new ResourceResponse();
                            response.SetFieldDeserializer(fieldDeserializer);

                            foreach (UnityNodeBase node in this.ChildNodes)
                            {
                                node.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                            }

                            //if fieldname is empty its an array item.
                            if (resourceResponse != null)
                            {
                                if (fieldData.arrayItem)
                                {
                                    resourceResponse.GetFieldDeserializer.SetArrayItem(objectInstance);
                                }
                                else
                                {
                                    resourceResponse.GetFieldDeserializer.SetField(fieldData.fieldName, objectInstance);
                                }
                            }
                        }
                    }
                }

                this.isDeserialized = true;
            }
        }