public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.animator = parentNode.GetGameObject().AddComponent <UnityEngine.Animator>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                this.animator.applyRootMotion = metaData["applyRootMotion"].ToObject <bool>();
                UInt32 avatarID = metaData.Value <UInt32>("avatarReferenceID");

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

                    ResourceResponse avatarResponse = new ResourceResponse(avatarID, (ResourceResponse response) =>
                    {
                        this.animator.avatar = response.GetAvatarRequest;
                    });

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

                this.isDeserialized = true;
            }
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.light = parentNode.GetGameObject().AddComponent <Light>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int     count = (bytes.Length / 4);
                float[] data  = new float[count];

                for (int i = 0; i < count; i++)
                {
                    data[i] = BitConverter.ToSingle(bytes, (i * 4));
                }

                light.color     = new Color(data[0], data[1], data[2], data[3]);
                light.type      = (LightType)Enum.ToObject(typeof(LightType), (int)data[4]);
                light.intensity = data[5];

                this.isDeserialized = true;
            }
        }
示例#3
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                Type scriptType = typeof(Animation);

                if (scriptType != null)
                {
                    Animation animation = parentNode.GetGameObject().AddComponent(scriptType) as Animation;

                    PropertySetter clipSetter = new PropertySetter("clip", (System.Object val) => { animation.clip = val as AnimationClip; });

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

                    FieldDeserializer fieldDeserializer = new FieldDeserializer(customValueSetters, animation);

                    ResourceResponse response = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

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

                this.isDeserialized = true;
            }
        }
        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;
            }
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            //Deserialize pointed node id.
            ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
            AssetResource resource  = dataBlock.GetResource(this.referenceID);

            UInt32 pointedNodeID = BitConverter.ToUInt32(resource.GetResourceData(), 0);

            UnityNodeBase currentParent = parentNode;

            while (currentParent.ParentNode != null)
            {
                currentParent = currentParent.ParentNode;
            }

            UnityNodeBase referencedNode = FindNodeWithID(currentParent, pointedNodeID);

            if (referencedNode is UnityMaterialNode)
            {
                UnityMaterialNode materialNode = referencedNode as UnityMaterialNode;

                Material mat = materialNode.GetMaterial();

                ResourceResponse request = resourceResponse.CanHandle(pointedNodeID);
                if (request != null)
                {
                    request.HandleMaterialResponse(mat);
                }
            }
        }
示例#6
0
        public T Recreate <T>(UnityNodeBase parentNode, PCFResourceType loadFlags) where T : UnityNodeBase
        {
            UnityNodeBase recreatedNode = null;

            //Allows us to mask nodes to load, makes it easier to do quick lookup of specific things. (like reading script data, no need to load textures etc)
            if ((this.resourceType & loadFlags) != 0)
            {
                //Create implementation specific node based on typetree info.
                recreatedNode = nodeFactory.CreateNodeImplementation(this.nodeName, this.resourceType, this.referenceID);
            }

            //If we are not the root node we will have a parent.
            if (parentNode != null && recreatedNode != null)
            {
                parentNode.AddChildNode(recreatedNode);
            }

            if (this.childNodes != null)
            {
                for (int i = 0; i < this.childNodes.Count; i++)
                {
                    this.childNodes[i].Recreate <T>(recreatedNode, loadFlags);
                }
            }

            //Only true for root node.
            if (parentNode == null)
            {
                return(recreatedNode as T);
            }

            return(null);
        }
示例#7
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.camera = parentNode.GetGameObject().AddComponent <Camera>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int     count = (bytes.Length / 4);
                float[] data  = new float[count];

                for (int i = 0; i < count; i++)
                {
                    data[i] = BitConverter.ToSingle(bytes, (i * 4));
                }

                camera.backgroundColor = new Color(data[0], data[1], data[2], data[3]);
                camera.fieldOfView     = data[4];
                camera.aspect          = data[5];

                this.isDeserialized = true;
            }
        }
 public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks, UnityNodeBase parentNode, ResourceResponse resourceRequest, List <Action <UnityNodeBase> > postInstallActions, bool optimizedLoad)
 {
     for (int i = 0; i < this.ChildNodes.Count; i++)
     {
         UnityNodeBase child = this.ChildNodes[i];
         child.Deserialize(dataBlocks, this, null, postInstallActions, optimizedLoad);
     }
 }
 public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                  UnityNodeBase parentNode,
                                  ResourceResponse resourceResponse,
                                  List <Action <UnityNodeBase> > postInstallActions,
                                  bool optimizedLoad)
 {
     throw new NotImplementedException();
 }
示例#10
0
 public override void Destroy()
 {
     for (int i = 0; i < this.ChildNodes.Count; i++)
     {
         UnityNodeBase child = this.ChildNodes[i];
         child.Destroy();
     }
 }
        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;
            }
        }
示例#12
0
        public override void Destroy()
        {
            for (int i = 0; i < this.ChildNodes.Count; i++)
            {
                UnityNodeBase child = this.ChildNodes[i];
                child.Destroy();
            }

            //Destroy gameobject after all children have destroyed themselves.
            UnityEngine.Object.Destroy(this.gameobject);
        }
示例#13
0
        public override Transform GetTransform()
        {
            UnityNodeBase transformNode = GetChildNodeByType(PCFResourceType.TRANSFORM);

            if (transformNode != null)
            {
                return(transformNode.GetTransform());
            }

            return(this.transform);
        }
        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[] bytes = resource.GetResourceData();

                int       vectorCount = (bytes.Length / 12);
                Vector3[] vectors     = new Vector3[vectorCount];

                for (int i = 0; i < vectorCount; i++)
                {
                    vectors[i] = new Vector3(
                        BitConverter.ToSingle(bytes, (i * 12)),
                        BitConverter.ToSingle(bytes, (i * 12) + 4),
                        BitConverter.ToSingle(bytes, (i * 12) + 8)
                        );
                }

                GameObject parentGO = parentNode.GetGameObject();

                //Gameobject creates a transform itself when its created, we only need to get the component.
                if (parentGO != null)
                {
                    this.transform = parentGO.GetComponent <Transform>();
                }

                if (this.transform != null)
                {
                    //Get the node above the parentNode and parent to it.
                    Transform parentTransform = parentNode.ParentNode.GetTransform();

                    //Parent this transform to the object above it, if one exists.
                    if (parentTransform != null)
                    {
                        this.transform.SetParent(parentTransform);
                    }

                    //Set the attributes after we have parented the transform.
                    this.transform.localPosition = vectors[0];
                    this.transform.localRotation = Quaternion.Euler(vectors[1]);
                    this.transform.localScale    = vectors[2];
                }

                this.isDeserialized = true;
            }
        }
示例#15
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.collider = parentNode.GetGameObject().AddComponent <BoxCollider>();

                this.isDeserialized = true;
            }
        }
示例#16
0
        public static UnityRootNode CreateNodeTree(Dictionary <PCFResourceType, DataBlockBase> dataBlocks, IFileHandle filePath, NodeFactory nodeFactory)
        {
            NodeBlock nodeBlock = dataBlocks[PCFResourceType.NODE] as NodeBlock;

            //Create and hook up node graph, but do no actual deserialization.
            UnityNodeBase node = nodeBlock.RecreateNodeGraph <UnityNodeBase>(nodeFactory);

            UnityRootNode rootNode = node as UnityRootNode;

            //Give rootnode a reference to the file we operate on, some nodes stream their data from this file.
            rootNode.SetFile(filePath);

            return(rootNode);
        }
示例#17
0
        public override GameObject GetGameObject()
        {
            if (this.ChildNodes != null)
            {
                UnityNodeBase objectNode = GetChildNodeByType(PCFResourceType.OBJECT);

                if (objectNode != null)
                {
                    return(objectNode.GetGameObject());
                }
            }

            return(null);
        }
示例#18
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            this.gameobject = new GameObject(this.name);
            this.gameobject.SetActive(false);

            for (int i = 0; i < this.ChildNodes.Count; i++)
            {
                UnityNodeBase child = this.ChildNodes[i];
                child.Deserialize(dataBlocks, this, null, postInstallActions, optimizedLoad);
            }
        }
示例#19
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                GameObject parentGameObject = parentNode.GetGameObject();

                this.meshFilter   = parentGameObject.AddComponent <MeshFilter>();
                this.meshRenderer = parentGameObject.AddComponent <MeshRenderer>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));
                UInt32  materialID     = metaData.Value <UInt32>("materialID");

                this.mesh = MeshSerializeUtilities.ReadMesh(resource.GetResourceData(), false);
                this.meshFilter.sharedMesh = this.mesh;

                if (optimizedLoad)
                {
                    //Free up cached ram memory for this mesh, disables access to mesh components like verts etc.
                    this.mesh.UploadMeshData(true);
                }

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

                    //Create a request to be send down the chain of children, see if any child will handle it.
                    ResourceResponse materialRequest = new ResourceResponse(materialID, (ResourceResponse response) => {
                        meshRenderer.material = response.GetMaterialRequest;
                    });

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

                this.isDeserialized = true;
            }
        }
示例#20
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();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                string scriptName = metaData["scriptname"].ToString();
                Type   scriptType = null;

                if (scriptMask == null)
                {
                    scriptType = Type.GetType(scriptName);
                }
                else if (this.scriptMask != null && this.scriptMask.Contains(scriptName))
                {
                    scriptType = Type.GetType(scriptName);
                }

                if (scriptType != null)
                {
                    this.script = parentNode.GetGameObject().AddComponent(scriptType) as MonoBehaviour;
                    FieldDeserializer fieldDeserializer = new FieldDeserializer(scriptType.GetFields(), this.script);

                    ResourceResponse response = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

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

                this.isDeserialized = true;
            }
        }
示例#21
0
        UnityNodeBase FindNodeWithID(UnityNodeBase node, UInt32 referenceID)
        {
            if (node.GetReferenceID() == referenceID)
            {
                return(node);
            }

            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                UnityNodeBase foundChild = FindNodeWithID(node.ChildNodes[i], referenceID);

                if (foundChild != null)
                {
                    return(foundChild);
                }
            }

            return(null);
        }
        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;
            }
        }
示例#23
0
        private void FindTransformsInHierarchy(UnityNodeBase node, Dictionary <string, Transform> mapping)
        {
            if (node.GetResourceType() == PCFResourceType.TRANSFORM)
            {
                Transform trans = node.GetTransform();
                mapping.Add(node.GetName(), trans);
            }

            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                UnityNodeBase   child = node.ChildNodes[i];
                PCFResourceType type  = child.GetResourceType();

                //Optimize search by not going into subtress that do not contain bones.
                if (type == PCFResourceType.ROOT || type == PCFResourceType.OBJECT || type == PCFResourceType.TRANSFORM)
                {
                    FindTransformsInHierarchy(child, mapping);
                }
            }
        }
示例#24
0
        void PopulateReferencedNodes(Dictionary <UInt32, UnityNodeBase> referencedNodes, UnityNodeBase node)
        {
            if (referencedNodes.ContainsKey(node.GetReferenceID()))
            {
                referencedNodes[node.GetReferenceID()] = node;
            }

            List <UnityNodeBase> children = node.ChildNodes;

            for (int i = 0; i < children.Count; i++)
            {
                UnityNodeBase   child = children[i];
                PCFResourceType type  = child.GetResourceType();

                if (type == PCFResourceType.ROOT || type == PCFResourceType.OBJECT || type == PCFResourceType.TRANSFORM)
                {
                    PopulateReferencedNodes(referencedNodes, children[i]);
                }
            }
        }
        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);

                ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                if (request != null)
                {
                    byte[] bundleBytes = resource.GetResourceData();
                    this.internalBundle = AssetBundle.LoadFromMemory(bundleBytes);

                    request.HandleAssetBundleResponse(this.internalBundle);
                }

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

                byte[] bytes = resource.GetResourceData();

                UInt32 referencedNodeID = BitConverter.ToUInt32(bytes, 0);
                //JObject metaData = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                //UInt32 referencedNodeID = metaData.Value<UInt32>("targetReferenceID");

                Dictionary <UInt32, UnityNodeBase> referencedNodes = resourceResponse.GetReferencedNodes;

                if (referencedNodes.ContainsKey(referencedNodeID))
                {
                    UnityNodeBase referencedNode = referencedNodes[referencedNodeID];

                    Transform transform = referencedNode.GetTransform();

                    if (transform != null)
                    {
                        resourceResponse.GetFieldDeserializer.SetArrayItem(transform);
                    }
                }

                this.isDeserialized = true;
            }
        }
示例#27
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);

                UInt32 pointedNodeID = BitConverter.ToUInt32(resource.GetResourceData(), 0);

                postInstallActions.Add((UnityNodeBase rootNode) => {
                    UnityNodeBase referencedNode = FindNodeWithID(rootNode, pointedNodeID);

                    if (referencedNode is UnityAnimationClipNode)
                    {
                        UnityAnimationClipNode animationClip = referencedNode as UnityAnimationClipNode;

                        AnimationClip animation = animationClip.GetAnimationClip();

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

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

                this.isDeserialized = true;
            }
        }
        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;
            }
        }
示例#29
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;
            }
        }
示例#30
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            GameObject parentGameObject = parentNode.GetGameObject();

            this.skinnedMesh = parentGameObject.AddComponent <SkinnedMeshRenderer>();

            ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
            AssetResource resource  = dataBlock.GetResource(this.referenceID);

            byte[]  metaDataBuffer = resource.GetMetaData();
            JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

            UInt32 materialID    = metaData.Value <UInt32>("materialID");
            string rootBoneName  = metaData.Value <string>("rootBone");
            string probeBoneName = metaData.Value <string>("probeAnchor");
            int    quality       = metaData.Value <int>("quality");

            string[] boneNames         = metaData.Value <JArray>("bones").ToObject <string[]>();
            float[]  blendShapeWeights = metaData.Value <JArray>("blendShapeWeights").ToObject <float[]>();

            // Add a post install action so the bones will have time to be created.
            postInstallActions.Add((UnityNodeBase node) => {
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                Transform[] bones = new Transform[boneNames.Length];

                //Mapp all bone transform in the hierarchy.
                Dictionary <string, Transform> bonesMapping = new Dictionary <string, Transform>();
                FindTransformsInHierarchy(node, bonesMapping);

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

                    if (bonesMapping.ContainsKey(name))
                    {
                        bones[i] = bonesMapping[name];
                    }
                    else
                    {
                        bones[i] = null;
                    }
                }

                this.mesh = MeshSerializeUtilities.ReadMesh(resource.GetResourceData(), true);

                if (optimizedLoad)
                {
                    //Free up mono memory for this mesh, now owned by the GPU.
                    this.mesh.UploadMeshData(true);
                }

                this.skinnedMesh.sharedMesh = this.mesh;
                this.skinnedMesh.bones      = bones;
                this.skinnedMesh.quality    = (SkinQuality)Enum.ToObject(typeof(SkinQuality), quality);

                for (int i = 0; i < blendShapeWeights.Length; i++)
                {
                    this.skinnedMesh.SetBlendShapeWeight(i, blendShapeWeights[i]);
                }

                if (bonesMapping.ContainsKey(rootBoneName))
                {
                    this.skinnedMesh.rootBone = bonesMapping[rootBoneName];
                }
                if (probeBoneName != null)
                {
                    this.skinnedMesh.probeAnchor = bonesMapping[probeBoneName];
                }

                watch.Stop();
                //Debug.Log("time to deserialize skinned mesh: " + watch.ElapsedMilliseconds);
            });

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

                //Create a request to be send down the chain of children, see if any child will handle it.
                ResourceResponse materialRequest = new ResourceResponse(materialID, (ResourceResponse response) => {
                    skinnedMesh.material = response.GetMaterialRequest;
                });

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