/// <summary> /// Deserialize a previously serialized game object. /// </summary> static void DeserializeHierarchy(this GameObject go, DataNode root) { SerializationEntry ent = new SerializationEntry(); ent.go = go; ent.node = root; mSerList.Add(ent); Transform trans = go.transform; trans.localPosition = root.GetChild <Vector3>("position", trans.localPosition); trans.localEulerAngles = root.GetChild <Vector3>("rotation", trans.localEulerAngles); trans.localScale = root.GetChild <Vector3>("scale", trans.localScale); go.layer = root.GetChild <int>("layer", go.layer); DataNode childNode = root.GetChild("Children"); if (childNode != null && childNode.children.size > 0) { for (int i = 0; i < childNode.children.size; ++i) { DataNode node = childNode.children[i]; GameObject child = null; GameObject prefab = UnityTools.Load <GameObject>(node.GetChild <string>("prefab")); if (prefab != null) { child = GameObject.Instantiate(prefab) as GameObject; } if (child == null) { child = new GameObject(); } child.name = node.name; Transform t = child.transform; t.parent = trans; t.localPosition = Vector3.zero; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; child.DeserializeHierarchy(node); } } }
/// <summary> /// Deserialize the texture that was previously serialized into the DataNode format. /// </summary> static public Texture DeserializeTexture(this DataNode node) { // First try the cache Texture tex = null; int id = node.Get <int>(); if (id != 0 && mTextures.TryGetValue(id, out tex) && tex != null) { return(tex); } // If the texture's ID is unknown, make a dummy one and try going through cache again string name = node.GetChild <string>("name", "Unnamed"); string path = node.GetChild <string>("path"); if (id == 0) { id = (path + name).GetHashCode(); if (mTextures.TryGetValue(id, out tex) && tex != null) { return(tex); } } // Next try to load the texture if (!string.IsNullOrEmpty(path)) { tex = UnityTools.Load <Texture>(path); if (tex != null) { mTextures[id] = tex; return(tex); } } // Lastly, create a new texture Texture2D t2 = new Texture2D(2, 2); t2.name = name; // Try to load the texture's data byte[] bytes = node.GetChild <byte[]>("bytes"); if (bytes != null) { t2.LoadImage(bytes); t2.filterMode = (FilterMode)node.GetChild <int>("filter", (int)t2.filterMode); t2.wrapMode = (TextureWrapMode)node.GetChild <int>("wrap", (int)t2.wrapMode); t2.anisoLevel = node.GetChild <int>("af", t2.anisoLevel); t2.Apply(); } else { #if UNITY_EDITOR Debug.LogWarning("Creating a dummy texture: " + t2.name, t2); #endif t2.SetPixels(new Color[] { Color.clear, Color.clear, Color.clear, Color.clear }); t2.Apply(); } // Add it to cache tex = t2; mTextures[id] = tex; return(tex); }
/// <summary> /// Set the mesh from the specified DataNode. /// </summary> static public Mesh DeserializeMesh(this DataNode node) { Mesh mesh = null; int id = node.Get <int>(); if (id != 0 && mCachedMeshes.TryGetValue(id, out mesh) && mesh != null) { return(mesh); } string name = node.GetChild <string>("name"); string path = node.GetChild <string>("path"); if (id == 0) { id = (path + name).GetHashCode(); if (mCachedMeshes.TryGetValue(id, out mesh) && mesh != null) { return(mesh); } } if (!string.IsNullOrEmpty(path)) { mesh = UnityTools.Load <Mesh>(path, name); #if UNITY_EDITOR if (mesh == null) { Debug.LogWarning("Unable to find mesh '" + name + "' in " + path); } #endif } else { mesh = new Mesh(); mesh.name = name; Vector3[] verts = node.GetChild <Vector3[]>("vertices"); if (verts != null) { mesh.vertices = verts; } Vector3[] normals = node.GetChild <Vector3[]>("normals"); if (normals != null) { mesh.normals = normals; } Vector2[] uv1 = node.GetChild <Vector2[]>("uv1"); if (uv1 != null) { mesh.uv = uv1; } Vector2[] uv2 = node.GetChild <Vector2[]>("uv2"); if (uv2 != null) { mesh.uv2 = uv2; } Vector4[] tangents = node.GetChild <Vector4[]>("tangents"); if (tangents != null) { mesh.tangents = tangents; } Color32[] colors = node.GetChild <Color32[]>("colors"); if (colors != null) { mesh.colors32 = colors; } BoneWeight[] weights = node.GetChild <BoneWeight[]>("weights"); if (weights != null) { mesh.boneWeights = weights; } Matrix4x4[] poses = node.GetChild <Matrix4x4[]>("poses"); if (poses != null) { mesh.bindposes = poses; } int[] triangles = node.GetChild <int[]>("triangles"); if (triangles != null) { mesh.triangles = triangles; } mesh.RecalculateBounds(); } mCachedMeshes[id] = mesh; return(mesh); }
/// <summary> /// Deserialize a previously serialized material. /// </summary> static public Material DeserializeMaterial(this DataNode matNode) { Material mat = null; int id = matNode.Get <int>(); if (mMaterials.TryGetValue(id, out mat) && mat != null) { return(mat); } // Try to load this material string name = matNode.GetChild <string>("name", "Unnamed"); string path = matNode.GetChild <string>("path"); if (id == 0) { id = (path + name).GetHashCode(); if (mMaterials.TryGetValue(id, out mat) && mat != null) { return(mat); } } if (!string.IsNullOrEmpty(path)) { mat = UnityTools.Load <Material>(path); if (mat != null) { mMaterials[id] = mat; return(mat); } } // Material can only be created if there is a shader to work with string shaderName = matNode.GetChild <string>("shader"); Shader shader = Shader.Find(shaderName); if (shader == null) { Debug.LogWarning("Shader '" + shaderName + "' was not found"); shader = Shader.Find("Diffuse"); } // Create a new material mat = new Material(shader); mat.name = name; mMaterials[id] = mat; // Restore material properties for (int b = 0; b < matNode.children.size; ++b) { DataNode prop = matNode.children[b]; if (prop.name == "shader") { continue; } if (prop.children.size != 0) { Texture tex = prop.DeserializeTexture(); if (tex != null) { mat.SetTexture(prop.name, tex); mat.SetTextureOffset(prop.name, prop.GetChild <Vector2>("offset")); mat.SetTextureScale(prop.name, prop.GetChild <Vector2>("scale", Vector2.one)); } } else if (prop.value is Vector4) { mat.SetVector(prop.name, prop.Get <Vector4>()); } else if (prop.value is Color) { mat.SetColor(prop.name, prop.Get <Color>()); } else if (prop.value is float || prop.value is int) { mat.SetFloat(prop.name, prop.Get <float>()); } } return(mat); }