// Called when the import process has completed and we have a prefab ready to go public void ImportFinished(string prefabPath) { // Get at the import behavour tied to this prefab and remove it from the scene string xmlAssetPath = GetXmlImportAssetPath(prefabPath); ImportBehaviour importBehaviour = ImportBehaviour.FindOrCreateImportBehaviour(xmlAssetPath); importBehaviour.DestroyImportBehaviour(); }
// We need to call this while the renderers on the model is having its material assigned to it // This is invoked for every submesh in the .obj wavefront mesh public Material FixMaterialForMeshRenderer(string objName, Renderer renderer) { string xmlPath = GetXmlImportAssetPath(objName); ImportBehaviour importBehavior = ImportBehaviour.FindOrCreateImportBehaviour(xmlPath); // The mesh to match string meshName = renderer.name; // Increment our progress bar importBehavior.IncrementProgressBar(String.Format("Assign material: {0}", meshName)); // Find an assignment that matches the mesh renderer var assignMaterials = importBehavior.XmlDocument.Root.Elements("AssignMaterial"); XElement match = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName); if (match == null) { // The names of our meshes in the AssignMaterials elements may be wrong // This happened before when Unity replaced whitespace with underscore in our named meshes // That case is handled now, but there may be others StringBuilder builder = new StringBuilder(); builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name); string choices = String.Join("\n ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray()); builder.AppendFormat("Choices are:\n {0}", choices); Debug.LogError(builder.ToString()); return(null); } string materialName = match.Attribute("material").Value + ".mat"; string materialPath = GetMaterialAssetPath(materialName); // Assign the material Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material; if (material == null) { Debug.LogError(String.Format("Could not find material: {0}", materialName)); } // Do we have an alpha color key? string htmlColor = ImportUtils.GetAttributeAsString(match, "alphaColorKey", ""); if (!String.IsNullOrEmpty(htmlColor)) { // Take for granted color is in the form '#RRGGBB' byte r = byte.Parse(htmlColor.Substring(1, 2), System.Globalization.NumberStyles.HexNumber); byte g = byte.Parse(htmlColor.Substring(3, 2), System.Globalization.NumberStyles.HexNumber); byte b = byte.Parse(htmlColor.Substring(5, 2), System.Globalization.NumberStyles.HexNumber); Color color = new Color32(r, g, b, 255); material.SetColor("_AlphaColorKey", color); } return(material); }
// Called when the import process has completed and we have a prefab ready to go public void ImportFinished(string prefabPath) { // String the prefab extension string prefabName = Path.GetFileNameWithoutExtension(prefabPath); // Get at the import behavour tied to this prefab and remove it from the scene string xmlAssetPath = GetXmlImportAssetPath(prefabName); ImportBehaviour importBehaviour = ImportBehaviour.FindOrCreateImportBehaviour(xmlAssetPath); importBehaviour.DestroyImportBehaviour(); }
// By the time this is called, our assets should be ready to create the map prefab public void MeshImported(string objPath) { string xmlPath = GetXmlImportAssetPath(objPath); ImportBehaviour importBehaviour = ImportBehaviour.FindOrCreateImportBehaviour(xmlPath); importBehaviour.IncrementProgressBar(String.Format("Create prefab: {0}", Path.GetFileNameWithoutExtension(GetPrefabAssetPath(objPath, false, null)))); foreach (var xmlPrefab in importBehaviour.XmlDocument.Root.Elements("Prefab")) { CreatePrefab(xmlPrefab, objPath); } }
// By the time this is called, our assets should be ready to create the map prefab public void MeshImported(string objPath) { // String the mesh type (.obj) from the path string objName = Path.GetFileNameWithoutExtension(objPath); // Get the XML file that this mesh came from string xmlPath = GetXmlImportAssetPath(objName); ImportBehaviour importBehaviour = ImportBehaviour.FindOrCreateImportBehaviour(xmlPath); importBehaviour.IncrementProgressBar(String.Format("Create prefab: {0}", Path.GetFileNameWithoutExtension(GetPrefabAssetPath(objName, false, null)))); foreach (var xmlPrefab in importBehaviour.XmlDocument.Root.Elements("Prefab")) { CreatePrefab(xmlPrefab, objPath); } }
// We need to call this while the renderers on the model is having its material assigned to it // This is invoked for every submesh in the .obj wavefront mesh public Material FixMaterialForMeshRenderer(string objName, Renderer renderer) { string xmlPath = GetXmlImportAssetPath(objName); ImportBehaviour importBehavior = ImportBehaviour.FindOrCreateImportBehaviour(xmlPath); // The mesh to match string meshName = renderer.name; // Increment our progress bar importBehavior.IncrementProgressBar(String.Format("Assign material: {0}", meshName)); // Find an assignment that matches the mesh renderer var assignMaterials = importBehavior.XmlDocument.Root.Elements("AssignMaterial"); XElement match = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName); if (match == null) { // The names of our meshes in the AssignMaterials elements may be wrong // This happened before when Unity replaced whitespace with underscore in our named meshes // That case is handled now, but there may be others StringBuilder builder = new StringBuilder(); builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name); string choices = String.Join("\n ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray()); builder.AppendFormat("Choices are:\n {0}", choices); Debug.LogError(builder.ToString()); return(null); } string materialName = match.Attribute("material").Value + ".mat"; string materialPath = GetMaterialAssetPath(materialName); // Assign the material Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material; if (material == null) { Debug.LogError(String.Format("Could not find material: {0}", materialName)); } return(material); }
// Called when Unity detects the *.tiled2unity.xml file needs to be (re)imported public void ImportBegin(string xmlPath) { // Normally, this is where we first create the XmlDocument for the whole import. ImportBehaviour importBehaviour = ImportBehaviour.FindOrCreateImportBehaviour(xmlPath); XDocument xml = importBehaviour.XmlDocument; if (xml == null) { Debug.LogError(String.Format("GameObject {0} not successfully initialized. Is it left over from a previous import. Try removing from scene are re-importing {1}.", importBehaviour.gameObject.name, xmlPath)); return; } CheckVersion(xmlPath, xml); // Import asset files. // (Note that textures should be imported before meshes) ImportTexturesFromXml(xml); CreateMaterialsFromInternalTextures(xml); ImportMeshesFromXml(xml); }