public UnitySerializeAnimation(GameObject parentGO, Animation animation, NodeBase rootNode)
 {
     this.animation = animation;
     this.rootNode  = rootNode;
 }
Ejemplo n.º 2
0
 public UnitySerializeLightProbes(GameObject parentGO, LightProbes lightProbes, NodeBase rootNode)
 {
     this.parentGO    = parentGO;
     this.lightProbes = lightProbes;
     this.rootNode    = rootNode;
 }
 public UnitySerializeMaterial(Material material, NodeBase rootNode)
 {
     this.material = material;
     this.rootNode = rootNode;
 }
Ejemplo n.º 4
0
 public UnitySerializeLight(GameObject parentGO, Light light, NodeBase rootNode)
 {
     this.light    = light;
     this.rootNode = rootNode;
 }
Ejemplo n.º 5
0
 public UnitySerializePointerCollection(System.Object[] values, Type type, System.Object script, string fieldName, NodeBase rootNode)
 {
     this.values    = values;
     this.type      = type;
     this.fieldName = fieldName;
     this.rootNode  = rootNode;
 }
Ejemplo n.º 6
0
 public AssetSerializer(GameObject selectedGameObject, string destinationDirectory)
 {
     this.destinationDirectory = destinationDirectory;
     this.selectedGameObject   = selectedGameObject;
     this.rootNode             = new RootNode("root");
 }
 public UnitySerializeTexture(Texture2D texture, NodeBase rootNode)
 {
     this.texture  = texture;
     this.rootNode = rootNode;
 }
Ejemplo n.º 8
0
 public UnitySerializeCamera(GameObject parentGO, Camera camera, NodeBase rootNode)
 {
     this.camera   = camera;
     this.rootNode = rootNode;
 }
Ejemplo n.º 9
0
 public ObjectNode(string name, NodeBase rootNode) : base()
 {
     this.name         = name;
     this.referenceID  = rootNode.GenerateID();
     this.resourceType = PCFResourceType.OBJECT;
 }
 public AudioClipSerialization(System.Object value, string fieldName, bool arrayItem, NodeBase rootNode)
 {
     this.audioClip = value as UnityEngine.AudioClip;
     this.fieldName = fieldName;
     this.arrayItem = arrayItem;
     this.rootNode  = rootNode;
 }
        public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postserializeActions)
        {
            if (this.audioClip == null)
            {
                Debug.Log("Missing audio clip! If this is intentional ignore this message.");
                return;
            }

                        #if UNITY_EDITOR
            //Make sure this is always 36 characters long.
            this.referenceID = this.rootNode.GenerateID();

            AudioSerializeOpts serializeOption = null;
            for (int i = 0; i < serializeOptions.Length; i++)
            {
                object opt = serializeOptions[i];

                if (opt is AudioSerializeOpts)
                {
                    serializeOption = opt as AudioSerializeOpts;
                    break;
                }
            }

            if (serializeOption == null)
            {
                return;
            }

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

            objNode.AddChildNode(scriptNode);

            bool   streamed  = true;
            byte[] audioData = serializeOption.PackageAudio(audioClip, serializedAssets, this.referenceID, ref streamed);

            JObject metaData = new JObject();
            metaData["fieldName"]  = this.fieldName;
            metaData["arrayItem"]  = this.arrayItem;
            metaData["name"]       = this.audioClip.name;
            metaData["sampleRate"] = this.audioClip.frequency;
            metaData["channels"]   = this.audioClip.channels;
            metaData["samples"]    = this.audioClip.samples * this.audioClip.channels;
            metaData["streamed"]   = streamed;

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

            AssetResource resource = new AssetResource(true);
            resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, audioData);

            serializedAssets.AddResource(referenceID, PCFResourceType.AUDIO, resource);
                        #endif
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
 public GradientSerialization(System.Object value, string fieldName, bool arrayItem, NodeBase rootNode)
 {
     this.gradient  = value as UnityEngine.Gradient;
     this.fieldName = fieldName;
     this.arrayItem = arrayItem;
     this.rootNode  = rootNode;
 }
        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 componentNode = new ComponentNode(PCFResourceType.ANIMATION, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            //Get fields using reflection
            PropertyInfo[] properties = animation.GetType().GetProperties();

            for (int i = 0; i < properties.Length; i++)
            {
                if (properties[i].CanWrite)
                {
                    PropertyInfo currentProperty = properties[i];
                    Type         valueType       = currentProperty.PropertyType;

                    //Find matching deserializer using reflection and field type as string or similar.
                    if (valueType.IsPrimitive || valueType == typeof(string))
                    {
                        UnitySerializerBase primitiveSerializer = new UnitySerializePrimitive(currentProperty.GetValue(animation, null), valueType, currentProperty.Name, false, this.rootNode);
                        primitiveSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);
                    }
                    else if (typeof(IList).IsAssignableFrom(properties[i].PropertyType))
                    {
                        //Convoluted way to make sure we have an array of System.Object.
                        IEnumerable          collection       = currentProperty.GetValue(animation, null) as IEnumerable;
                        List <System.Object> intermediateList = new List <System.Object>();

                        foreach (System.Object item in collection)
                        {
                            intermediateList.Add(item);
                        }

                        System.Object[] values = intermediateList.ToArray();

                        if (values != null && values.Length > 0)
                        {
                            UnitySerializerBase collectionSerializer = new UnitySerializeCollection(values, values[0].GetType(), animation, currentProperty.Name, rootNode);
                            collectionSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);
                        }
                    }
                    else if (valueType.IsClass || valueType.IsLayoutSequential)
                    {
                        //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 + "." + valueType.Name + "Serialization";
                        Type          serializerType = assembly.GetType(serializerName, false);
                        System.Object val            = properties[i].GetValue(this.animation, null);

                        //If we have implemented a custom serializer for this type we invoke it and let it serialize relevant data.
                        if (serializerType != null)
                        {
                            object     serializerClass = Activator.CreateInstance(serializerType, Convert.ChangeType(val, valueType), currentProperty.Name, false, this.rootNode);
                            MethodInfo serializeMethod = serializerType.GetMethod("Serialize");

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

                            serializeMethod.Invoke(serializerClass, parameters);
                        }
                        else
                        {
                            //Use generic serializer incase not found.
                            UnitySerializerBase classSerialize = new UnitySerializeClass(currentProperty.GetValue(this.animation, null), valueType, currentProperty.Name, false, this.rootNode);
                            classSerialize.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);
                        }
                    }
                }
            }

            //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, null);

            serializedAssets.AddResource(referenceID, PCFResourceType.ANIMATION, resource);
        }
 public UnitySerializeScript(GameObject parentGO, MonoBehaviour script, NodeBase rootNode)
 {
     this.script   = script;
     this.rootNode = rootNode;
 }
 public UnitySerializeSkinnedMesh(GameObject parentGO, SkinnedMeshRenderer skinnedMesh, NodeBase rootNode)
 {
     this.skinnedMesh = skinnedMesh;
     this.rootNode    = rootNode;
 }
        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.SCRIPT, referenceID, null, this.script.GetType().Name.ToString());

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

            JObject metaData = new JObject();

            metaData["scriptname"] = this.script.GetType().ToString();

            //Get fields using reflection
            FieldInfo[] fields = script.GetType().GetFields();

            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i].IsPublic && !fields[i].IsStatic)
                {
                    FieldInfo currentField = fields[i];
                    Type      valueType    = currentField.FieldType;

                    //Find matching deserializer using reflection and field type as string or similar.
                    if (valueType.IsPrimitive || valueType == typeof(string))
                    {
                        UnitySerializerBase primitiveSerializer = new UnitySerializePrimitive(currentField.GetValue(script), valueType, currentField.Name, false, this.rootNode);
                        primitiveSerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                    }
                    else if (typeof(IList).IsAssignableFrom(fields[i].FieldType))
                    {
                        //Convoluted way to make sure we have an array of System.Object.
                        IEnumerable          collection       = currentField.GetValue(script) as IEnumerable;
                        List <System.Object> intermediateList = new List <System.Object>();

                        foreach (System.Object item in collection)
                        {
                            intermediateList.Add(item);
                        }

                        System.Object[] values = intermediateList.ToArray();

                        if (values != null && values.Length > 0)
                        {
                            UnitySerializerBase collectionSerializer = new UnitySerializeCollection(values, values[0].GetType(), script, currentField.Name, rootNode);
                            collectionSerializer.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                        }
                    }
                    else if (valueType.IsClass || valueType.IsLayoutSequential)
                    {
                        //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 + "." + valueType.Name + "Serialization";
                        Type          serializerType = assembly.GetType(serializerName, false);
                        System.Object val            = fields[i].GetValue(this.script);

                        //If we have implemented a custom serializer for this type we invoke it and let it serialize relevant data.
                        if (serializerType != null)
                        {
                            object     serializerClass = Activator.CreateInstance(serializerType, Convert.ChangeType(val, valueType), currentField.Name, false, this.rootNode);
                            MethodInfo serializeMethod = serializerType.GetMethod("Serialize");

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

                            serializeMethod.Invoke(serializerClass, parameters);
                        }
                        else
                        {
                            //Use generic serializer incase not found.
                            UnitySerializerBase classSerialize = new UnitySerializeClass(currentField.GetValue(this.script), valueType, currentField.Name, false, this.rootNode);
                            classSerialize.Serialize(serializedAssets, serializeOptions, scriptNode, postSerializeActions);
                        }
                    }
                }
            }

            AssetResource resource = new AssetResource(false);

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

            serializedAssets.AddResource(referenceID, PCFResourceType.SCRIPT, resource);
        }
        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 componentNode = new ComponentNode(PCFResourceType.SKINNEDMESH, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            byte[] bytes = MeshSerializeUtilities.WriteMesh(this.skinnedMesh.sharedMesh, true, true);

            //Serialize Material
            UnitySerializeMaterial materialSerializer = new UnitySerializeMaterial(this.skinnedMesh.sharedMaterial, this.rootNode);

            materialSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

            //Make sure mesh knows that material it needs.
            JObject metaData = new JObject();

            metaData["materialID"] = materialSerializer.GetPointerID();
            metaData["rootBone"]   = skinnedMesh.rootBone.name;

            if (skinnedMesh.probeAnchor != null)
            {
                metaData["probeAnchor"] = skinnedMesh.probeAnchor.name;
            }

            metaData["quality"] = (int)skinnedMesh.quality;

            JArray bones = new JArray();

            for (int i = 0; i < skinnedMesh.bones.Length; i++)
            {
                bones.Add(skinnedMesh.bones[i].name);
            }
            metaData["bones"] = bones;

            JArray blendShapeWeights = new JArray();

            for (int i = 0; i < skinnedMesh.sharedMesh.blendShapeCount; i++)
            {
                blendShapeWeights.Add(skinnedMesh.GetBlendShapeWeight(i));
            }
            metaData["blendShapeWeights"] = blendShapeWeights;

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            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.SKINNEDMESH, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
Ejemplo n.º 19
0
        void SerializeRecursivly(GameObject go, SerializedAssets serializedAssets, object[] serializeOptions, NodeBase node, List <Action <NodeBase> > postSerializeActions)
        {
            //Serialize an abstract representation of the node tree.
            NodeBase objNode = new ObjectNode(go.name, this.rootNode);

            //Parent objNode to ParentNode, ParentNode is always an ObjectNode. (Gameobject if you will)
            node.AddChildNode(objNode);

            Component[] objectComponents = go.GetComponents(typeof(Component));

            //Some resources will serialize subresources such as materials, lightprobes and textures, they will become children of their NodeComponents.
            for (int i = 0; i < objectComponents.Length; i++)
            {
                if (objectComponents[i] is SkinnedMeshRenderer)
                {
                    UnitySerializeSkinnedMesh skinnedMeshSerializer = new UnitySerializeSkinnedMesh(go, objectComponents[i] as SkinnedMeshRenderer, this.rootNode);
                    skinnedMeshSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is MeshRenderer)
                {
                    UnitySerializeMesh meshSerializer = new UnitySerializeMesh(go, objectComponents[i] as MeshRenderer, this.rootNode);
                    meshSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is MonoBehaviour)
                {
                    UnitySerializeScript scriptSerializer = new UnitySerializeScript(go, objectComponents[i] as MonoBehaviour, this.rootNode);
                    scriptSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is Transform)
                {
                    UnitySerializeTransform transformSerializer = new UnitySerializeTransform(go, objectComponents[i] as Transform, this.rootNode);
                    transformSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is Animator)
                {
                    UnitySerializeAnimator avtarSerializer = new UnitySerializeAnimator(go, objectComponents[i] as Animator, this.rootNode);
                    avtarSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is Animation)
                {
                    UnitySerializeAnimation animationSerializer = new UnitySerializeAnimation(go, objectComponents[i] as Animation, this.rootNode);
                    animationSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is Camera)
                {
                    UnitySerializeCamera cameraSerializer = new UnitySerializeCamera(go, objectComponents[i] as Camera, this.rootNode);
                    cameraSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is LightProbeGroup)
                {
                    UnitySerializeLightProbes lightprobeSerializer = new UnitySerializeLightProbes(go, LightmapSettings.lightProbes, this.rootNode);
                    lightprobeSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is Collider)
                {
                    UnitySerializeCollider colliderSerializer = new UnitySerializeCollider(go, objectComponents[i] as Collider, this.rootNode);
                    colliderSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
                else if (objectComponents[i] is Light)
                {
                    UnitySerializeLight lightSerializer = new UnitySerializeLight(go, objectComponents[i] as Light, this.rootNode);
                    lightSerializer.Serialize(serializedAssets, serializeOptions, objNode, postSerializeActions);
                }
            }

            for (int i = 0; i < go.transform.childCount; i++)
            {
                GameObject child = go.transform.GetChild(i).gameObject;
                SerializeRecursivly(child, serializedAssets, serializeOptions, objNode, postSerializeActions);
            }
        }
Ejemplo n.º 20
0
 public UnitySerializePrimitive(System.Object value, Type type, string fieldName, bool arrayItem, NodeBase rootNode)
 {
     this.value     = value;
     this.type      = type;
     this.fieldName = fieldName;
     this.arrayItem = arrayItem;
     this.rootNode  = rootNode;
 }
        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();

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

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            if (this.texture != null)
            {
                TextureSerializeOpts serializeOption = null;
                for (int i = 0; i < serializeOptions.Length; i++)
                {
                    object opt = serializeOptions[i];

                    if (opt is TextureSerializeOpts)
                    {
                        serializeOption = opt as TextureSerializeOpts;
                        break;
                    }
                }

                if (serializeOption == null)
                {
                    return;
                }

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

                TextureDataFormat format      = TextureDataFormat.Empty;
                byte[]            textureData = serializeOption.PackageTexture(this.texture, serializedAssets, referenceID, ref format);

                JObject metaData = new JObject();
                metaData["width"]         = this.texture.width;
                metaData["height"]        = this.texture.height;
                metaData["textureFormat"] = (int)format;

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

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

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

                //Nodes store their resource when serializing
                componentNode.SetSerializer(this);
            }
                        #endif
        }
Ejemplo n.º 22
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);
            }
        }
Ejemplo n.º 23
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 componentNode = new ComponentNode(PCFResourceType.LIGHT, referenceID, null);

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

            byte[] bytes = new byte[24];

            float[] data = new float[6];

            data[0] = this.light.color.r;
            data[1] = this.light.color.g;
            data[2] = this.light.color.b;
            data[3] = this.light.color.a;
            data[4] = (float)this.light.type;
            data[5] = this.light.intensity;

            for (int i = 0; i < data.Length; i++)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(data[i]), 0, bytes, 4 * i, 4);
            }

            //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.LIGHT, resource);

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
        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 componentNode = new ComponentNode(PCFResourceType.COLLIDER, referenceID, null);

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

            //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, null);

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

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
Ejemplo n.º 25
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);
            }
        }
 public UnitySerializeCollider(GameObject parentGO, Collider collider, NodeBase rootNode)
 {
     this.rootNode = rootNode;
 }
Ejemplo n.º 27
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();

            LightprobeSerializeOpts serializeOption = null;

            for (int i = 0; i < serializeOptions.Length; i++)
            {
                object opt = serializeOptions[i];

                if (opt is LightprobeSerializeOpts)
                {
                    serializeOption = opt as LightprobeSerializeOpts;
                    break;
                }
            }

            if (serializeOption == null)
            {
                return;
            }

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

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

            //Manually serialize SH components,.
            //However unity does not currently expose any way to generate the probes at runtime.
            //So we cannot set them directly unless the probes exist. (Fix it unity :D)
            byte[] serializedProbes = SerializeLightProbes(this.lightProbes);

            Dictionary <string, FileInfo> internalBundles = serializeOption.ListInternalBundles();

            JArray bundleReferences = new JArray();

            foreach (KeyValuePair <string, FileInfo> pair in internalBundles)
            {
                UnitySerializeInternalBundle bundleSerializer = new UnitySerializeInternalBundle(pair.Key, pair.Value.FullName, string.Empty, this.rootNode);
                bundleSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

                JObject item = new JObject();
                item["platform"]    = pair.Key;
                item["referenceID"] = bundleSerializer.GetReferenceID();

                bundleReferences.Add(item);
            }

            JObject metaData = new JObject();

            metaData["numberOfProbes"]   = this.lightProbes != null ? this.lightProbes.bakedProbes.Length : 0;
            metaData["bundleReferences"] = bundleReferences;

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

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

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

            //Nodes store their resource when serializing
            componentNode.SetSerializer(this);
        }
Ejemplo n.º 28
0
 public Texture2DSerialization(System.Object value, string fieldName, bool arrayItem, NodeBase rootNode)
 {
     this.texture   = value as UnityEngine.Texture2D;
     this.fieldName = fieldName;
     this.arrayItem = arrayItem;
     this.rootNode  = rootNode;
 }
        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
        }
Ejemplo n.º 30
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 componentNode = new ComponentNode(PCFResourceType.MESH, referenceID, null);

            //Component nodes must always be parented to objNodes.
            objNode.AddChildNode(componentNode);

            //Serialize mesh into a byte array.
            byte[] bytes = MeshSerializeUtilities.WriteMesh(this.meshFilter.sharedMesh, true, false);

            //Serialize Material
            UnitySerializeMaterial materialSerializer = new UnitySerializeMaterial(this.meshRenderer.sharedMaterial, this.rootNode);

            materialSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions);

            //Make sure mesh knows that material it needs.
            JObject metaData = new JObject();

            metaData["materialID"] = materialSerializer.GetPointerID();

            //Create serialized asset by converting data to a bytearray and give it to the constructor.
            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.MESH, resource);

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