/// <summary> /// Get the value of the existing child or the default value if the child is not present. /// </summary> public T GetChild <T> (string name, T defaultValue) { DataNode node = GetChild(name); if (node == null) { return(defaultValue); } return(node.Get <T>()); }
/// <summary> /// Deserialize a previously serialized renderer. /// </summary> static public void Deserialize(this Renderer ren, DataNode data) { #if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 ren.castShadows = data.GetChild <bool>("castShadows", ren.castShadows); #else DataNode cs = data.GetChild("castShadows"); if (cs != null) { ren.shadowCastingMode = cs.Get <bool>() ? UnityEngine.Rendering.ShadowCastingMode.On : UnityEngine.Rendering.ShadowCastingMode.Off; } else { ren.shadowCastingMode = data.GetChild <UnityEngine.Rendering.ShadowCastingMode>("shadowCastingMode", ren.shadowCastingMode); } ren.reflectionProbeUsage = data.GetChild <UnityEngine.Rendering.ReflectionProbeUsage>("reflectionProbes", ren.reflectionProbeUsage); #endif ren.receiveShadows = data.GetChild <bool>("receiveShadows", ren.receiveShadows); #if UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3 ren.useLightProbes = data.GetChild <bool>("useLightProbes", ren.useLightProbes); #else var lpu = data.GetChild("lightProbeUsage"); if (lpu != null) { ren.lightProbeUsage = (UnityEngine.Rendering.LightProbeUsage)lpu.Get <byte>((byte)ren.lightProbeUsage); } else { // Pre-Unity 5.4 format ren.lightProbeUsage = data.GetChild <bool>("useLightProbes", ren.lightProbeUsage != UnityEngine.Rendering.LightProbeUsage.Off) ? UnityEngine.Rendering.LightProbeUsage.BlendProbes : UnityEngine.Rendering.LightProbeUsage.Off; } #endif DataNode matRoot = data.GetChild("Materials"); if (matRoot != null && matRoot.children.size > 0) { Material[] mats = new Material[matRoot.children.size]; for (int i = 0; i < matRoot.children.size; ++i) { DataNode matNode = matRoot.children[i]; mats[i] = matNode.DeserializeMaterial(); } ren.sharedMaterials = mats; } }
/// <summary> /// Deserialize a previously serialized renderer. /// </summary> static public void Deserialize(this Renderer ren, DataNode data) { #if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 ren.castShadows = data.GetChild <bool>("castShadows", ren.castShadows); #else DataNode cs = data.GetChild("castShadows"); if (cs != null) { ren.shadowCastingMode = cs.Get <bool>() ? UnityEngine.Rendering.ShadowCastingMode.On : UnityEngine.Rendering.ShadowCastingMode.Off; } else { ren.shadowCastingMode = data.GetChild <UnityEngine.Rendering.ShadowCastingMode>("shadowCastingMode", ren.shadowCastingMode); } ren.reflectionProbeUsage = data.GetChild <UnityEngine.Rendering.ReflectionProbeUsage>("reflectionProbes", ren.reflectionProbeUsage); #endif ren.receiveShadows = data.GetChild <bool>("receiveShadows", ren.receiveShadows); ren.useLightProbes = data.GetChild <bool>("useLightProbes", ren.useLightProbes); DataNode matRoot = data.GetChild("Materials"); if (matRoot != null && matRoot.children.size > 0) { Material[] mats = new Material[matRoot.children.size]; for (int i = 0; i < matRoot.children.size; ++i) { DataNode matNode = matRoot.children[i]; mats[i] = matNode.DeserializeMaterial(); } ren.sharedMaterials = mats; } }
/// <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); }