public override void OnPostImport(string assetPath) { XmlDocument doc = new XmlDocument(); doc.Load(assetPath); XmlNode root = doc.DocumentElement; string prefabPath = root.SelectSingleNode("Path").InnerText; string fileName = Path.GetFileNameWithoutExtension(assetPath); string fullPath = Path.Combine(prefabPath, fileName + ".prefab"); // Load the prefab asset GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject; if (prefab == null) { SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath); return; } GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; // Load and set children SmallParserUtils.RecursiveParseTransformXml(root, prefabInstance); // Save prefab asset PrefabUtility.RecordPrefabInstancePropertyModifications(prefabInstance.GetComponent <Transform>()); PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction); // Clean up GameObject.DestroyImmediate(prefabInstance); // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor) AssetDatabase.ImportAsset(fullPath); }
public static Shader GetShaderFromName(string shaderName) { Shader shader = Shader.Find(shaderName); if (shader == null) { SmallLogger.LogWarning(SmallLogger.LogType.PreImport, "The shader \"" + shaderName + "\" does not exists."); } return(shader); }
public override void OnPostImport(string assetPath) { XmlDocument doc = new XmlDocument(); doc.Load(assetPath); XmlNode root = doc.DocumentElement; string prefabPath = root.SelectSingleNode("Path").InnerText; string fileName = Path.GetFileNameWithoutExtension(assetPath); string fullPath = Path.Combine(prefabPath, fileName + ".prefab"); // Load the prefab asset GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject; if (prefab == null) { SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath); return; } GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; Camera camera = prefabInstance.GetOrAddComponent <Camera>(); string projection = root.SelectSingleNode("Projection").InnerText; camera.orthographic = (projection != "PERSP"); float fov = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Fov").InnerText); camera.fieldOfView = (Mathf.Atan(Mathf.Tan(Mathf.Deg2Rad * fov / 2.0f) / camera.aspect) * 2.0f) * Mathf.Rad2Deg; float near = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Near").InnerText); camera.nearClipPlane = near; float far = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Far").InnerText); camera.farClipPlane = far; float size = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Size").InnerText); camera.orthographicSize = size * 0.28f; // Save prefab asset PrefabUtility.RecordPrefabInstancePropertyModifications(camera); PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction); // Clean up GameObject.DestroyImmediate(prefabInstance); // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor) AssetDatabase.ImportAsset(fullPath); }
public static Material CreateMaterialFromXml(string xmlPath) { XmlDocument xml = new XmlDocument(); xml.Load(xmlPath); XmlNode root = xml.DocumentElement; string fileName = Path.GetFileNameWithoutExtension(xmlPath); string path = Path.Combine(root.SelectSingleNode("Path").InnerText, fileName + ".mat"); // If the material does not exists, we create it bool isCreating = false; Material material = AssetDatabase.LoadAssetAtPath <Material>(path); if (material == null) { isCreating = true; material = new Material(Shader.Find("Standard")); AssetDatabase.CreateAsset(material, path); } // Try to set the shader XmlNode shaderNode = root.SelectSingleNode("Shader"); string shaderName = "Standard"; if (shaderNode != null) { shaderName = root.SelectSingleNode("Shader").InnerText; } else { SmallLogger.LogWarning(SmallLogger.LogType.PreImport, "Shader name not found in material '" + fileName + "'. The material is probably missing a group node."); } Shader shader = GetShaderFromName(shaderName); if (shader != null) { material.shader = shader; SmallLogger.Log(SmallLogger.LogType.PreImport, (isCreating ? "Creating" : "Loading") + " material '" + fileName + "' from xml '" + xmlPath + "' using shader '" + shaderName + "'"); } else { SmallLogger.Log(SmallLogger.LogType.PreImport, (isCreating ? "Creating" : "Loading") + " material '" + fileName + "'from xml '" + xmlPath + "'. Shader name is invalid '" + shaderName + "'"); } return(material); }
public override void OnPostImport(string assetPath) { XmlDocument doc = new XmlDocument(); doc.Load(assetPath); XmlNode root = doc.DocumentElement; string prefabPath = root.SelectSingleNode("Path").InnerText; string fileName = Path.GetFileNameWithoutExtension(assetPath); string fullPath = Path.Combine(prefabPath, fileName + ".prefab"); // Load the prefab asset GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject; if (prefab == null) { SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath); return; } GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; // Load and assign the mesh string meshPath = root.SelectSingleNode("Model").InnerText + ".fbx"; Mesh mesh = AssetDatabase.LoadAssetAtPath <Mesh>(meshPath); MeshFilter meshFilter = prefabInstance.GetOrAddComponent <MeshFilter>(); meshFilter.mesh = mesh; PrefabUtility.RecordPrefabInstancePropertyModifications(meshFilter); // Load and assign materials MeshRenderer renderer = prefabInstance.GetOrAddComponent <MeshRenderer>(); XmlNodeList materialsNode = root.SelectSingleNode("Materials").ChildNodes; if (materialsNode.Count != 0) { Material[] materials = new Material[materialsNode.Count]; for (int i = 0; i < materialsNode.Count; i++) { string path = materialsNode[i].SelectSingleNode("Path").InnerText; string name = materialsNode[i].SelectSingleNode("Name").InnerText; string materialPath = Path.Combine(path, name + ".mat"); materials[i] = AssetDatabase.LoadAssetAtPath <Material>(materialPath); } renderer.sharedMaterials = materials; PrefabUtility.RecordPrefabInstancePropertyModifications(renderer); } // Load and set children SmallParserUtils.RecursiveParseTransformXml(root, prefabInstance); // Save prefab asset PrefabUtility.RecordPrefabInstancePropertyModifications(prefabInstance.GetComponent <Transform>()); PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction); // Clean up GameObject.DestroyImmediate(prefabInstance); // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor) AssetDatabase.ImportAsset(fullPath); }
public override void OnPostImport(string assetPath) { XmlDocument doc = new XmlDocument(); doc.Load(assetPath); XmlNode root = doc.DocumentElement; string prefabPath = root.SelectSingleNode("Path").InnerText; string fileName = Path.GetFileNameWithoutExtension(assetPath); string fullPath = Path.Combine(prefabPath, fileName + ".prefab"); // Load the prefab asset GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject; if (prefab == null) { SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath); return; } GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; Light light = prefabInstance.GetOrAddComponent <Light>(); // Light type string type = root.SelectSingleNode("Type").InnerText; if (type == "POINT") { light.type = LightType.Point; light.range = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText); } else if (type == "SPOT") { light.type = LightType.Spot; light.spotAngle = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("SpotSize").InnerText); light.range = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText); } else if (type == "SUN") { light.type = LightType.Directional; } else if (type == "AREA") { string shape = root.SelectSingleNode("Shape").InnerText; if (shape == "RECTANGLE") { light.type = LightType.Rectangle; float width = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Width").InnerText); float height = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Height").InnerText); light.areaSize = new Vector2(width, height); } else if (shape == "DISC") { light.type = LightType.Disc; float radius = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText); light.areaSize = new Vector2(radius, radius); } } // Light color light.color = SmallParserUtils.ParseColorXml(root.SelectSingleNode("Color").InnerText); light.intensity = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Power").InnerText) / 100.0f; // Save prefab asset PrefabUtility.RecordPrefabInstancePropertyModifications(light); PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction); // Clean up GameObject.DestroyImmediate(prefabInstance); // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor) AssetDatabase.ImportAsset(fullPath); }