// We have many independent requests on the ImportBehaviour so we can't take for granted it has been created yet. // However, if it has been created then use it. public static ImportBehaviour FindOrCreateImportBehaviour(string xmlPath) { string importName = ImportBehaviour.GetFilenameWithoutTiled2UnityExtension(xmlPath); // Try to find foreach (ImportBehaviour status in UnityEngine.Object.FindObjectsOfType <ImportBehaviour>()) { if (String.Compare(status.ImportName, importName, true) == 0) { return(status); } } // Couldn't find, so create. GameObject gameObject = new GameObject("__temp_tiled2unity_import"); #if !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_2 && !UNITY_4_3 gameObject.transform.SetAsFirstSibling(); #endif ImportBehaviour importStatus = gameObject.AddComponent <ImportBehaviour>(); importStatus.ImportName = importName; // Opening the XDocument itself can be expensive so start the progress bar just before we start importStatus.StartProgressBar(xmlPath); importStatus.XmlDocument = XDocument.Load(xmlPath); importStatus.numberOfElements = importStatus.XmlDocument.Element("Tiled2Unity").Elements().Count(); importStatus.IncrementProgressBar(xmlPath); return(importStatus); }
// 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); }
// 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); }