private void ImportTexturesFromXml(XDocument xml) { var texData = xml.Root.Elements("ImportTexture"); foreach (var tex in texData) { string name = tex.Attribute("filename").Value; string data = tex.Value; // The data is gzip compressed base64 string. We need the raw bytes. //byte[] bytes = ImportUtils.GzipBase64ToBytes(data); byte[] bytes = ImportUtils.Base64ToBytes(data); // Save and import the texture asset { string pathToSave = GetTextureAssetPath(name); ImportUtils.ReadyToWrite(pathToSave); File.WriteAllBytes(pathToSave, bytes); AssetDatabase.ImportAsset(pathToSave, ImportAssetOptions.ForceSynchronousImport); } // Create a material in prepartion for the texture being successfully imported { // We need to recreate the material every time because the Tiled Map may have changed string materialPath = GetMaterialAssetPath(name); Material material = CreateMaterialFromXml(tex); ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); } } }
private void CreateMaterialsFromInternalTextures(XDocument xml) { var texData = xml.Root.Elements("InternalTexture"); foreach (var tex in texData) { string texAssetPath = tex.Attribute("assetPath").Value; string materialPath = GetMaterialAssetPath(texAssetPath); // Create our material Material material = CreateMaterialFromXml(tex); // Assign to it the texture that is already internal to our Unity project Texture2D texture2d = AssetDatabase.LoadAssetAtPath(texAssetPath, typeof(Texture2D)) as Texture2D; material.SetTexture("_MainTex", texture2d); // Write the material to our asset database ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); } }
private void ImportAllMaterials(Tiled2Unity.ImportBehaviour importComponent) { // Create a material for each texture that has been imported foreach (var xmlTexture in importComponent.XmlDocument.Root.Elements("ImportTexture")) { bool isResource = ImportUtils.GetAttributeAsBoolean(xmlTexture, "isResource", false); string textureFile = ImportUtils.GetAttributeAsString(xmlTexture, "filename"); string materialPath = MakeMaterialAssetPath(textureFile, isResource); string materialFile = System.IO.Path.GetFileName(materialPath); // Keep track that we importing this material if (!importComponent.ImportWait_Materials.Contains(materialFile, StringComparer.OrdinalIgnoreCase)) { importComponent.ImportWait_Materials.Add(materialFile); } // Create the material UnityEngine.Material material = CreateMaterialFromXml(xmlTexture, importComponent); // Assign the texture to the material { string textureAsset = GetTextureAssetPath(textureFile); AssignTextureAssetToMaterial(material, materialFile, textureAsset, importComponent); } ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); importComponent.ImportTiled2UnityAsset(materialPath); } // Create a material for each internal texture foreach (var xmlInternal in importComponent.XmlDocument.Root.Elements("InternalTexture")) { bool isResource = ImportUtils.GetAttributeAsBoolean(xmlInternal, "isResource", false); string textureAsset = ImportUtils.GetAttributeAsString(xmlInternal, "assetPath"); string textureFile = System.IO.Path.GetFileName(textureAsset); string materialPath = MakeMaterialAssetPath(textureFile, isResource); // "Internal textures" may have a unique material name that goes with it string uniqueMaterialName = ImportUtils.GetAttributeAsString(xmlInternal, "materialName", ""); if (!String.IsNullOrEmpty(uniqueMaterialName)) { materialPath = String.Format("{0}/{1}{2}", Path.GetDirectoryName(materialPath), uniqueMaterialName, Path.GetExtension(materialPath)); materialPath = materialPath.Replace(System.IO.Path.DirectorySeparatorChar, '/'); } string materialFile = System.IO.Path.GetFileName(materialPath); // Keep track that we are importing this material if (!importComponent.ImportWait_Materials.Contains(materialFile, StringComparer.OrdinalIgnoreCase)) { importComponent.ImportWait_Materials.Add(materialFile); } // Create the material and assign the texture UnityEngine.Material material = CreateMaterialFromXml(xmlInternal, importComponent); AssignTextureAssetToMaterial(material, materialFile, textureAsset, importComponent); ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); importComponent.ImportTiled2UnityAsset(materialPath); } // If we have no materials to import then go to next stage (meshes) if (importComponent.ImportWait_Materials.Count() == 0) { ImportAllMeshes(importComponent); } }
private void ImportAllMaterials(Tiled2Unity.ImportBehaviour importComponent) { // Create a material for each texture that has been imported foreach (var xmlTexture in importComponent.XmlDocument.Root.Elements("ImportTexture")) { bool isResource = ImportUtils.GetAttributeAsBoolean(xmlTexture, "isResource", false); string textureFile = ImportUtils.GetAttributeAsString(xmlTexture, "filename"); string materialPath = MakeMaterialAssetPath(textureFile, isResource); string materialFile = Path.GetFileName(materialPath); // Keep track that we importing this material importComponent.ImportWait_Materials.Add(materialFile); // Create the material UnityEngine.Material material = CreateMaterialFromXml(xmlTexture); // Assign the texture to the material { string textureAsset = GetTextureAssetPath(textureFile); Texture2D texture2d = AssetDatabase.LoadAssetAtPath(textureAsset, typeof(Texture2D)) as Texture2D; material.SetTexture("_MainTex", texture2d); } ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); importComponent.ImportTiled2UnityAsset(materialPath); } // Create a material for each internal texture foreach (var xmlInternal in importComponent.XmlDocument.Root.Elements("InternalTexture")) { bool isResource = ImportUtils.GetAttributeAsBoolean(xmlInternal, "isResource", false); string textureAsset = ImportUtils.GetAttributeAsString(xmlInternal, "assetPath"); string textureFile = Path.GetFileName(textureAsset); string materialPath = MakeMaterialAssetPath(textureFile, isResource); string materialFile = Path.GetFileName(materialPath); // Keep track that we importing this material importComponent.ImportWait_Materials.Add(materialFile); // Create the material UnityEngine.Material material = CreateMaterialFromXml(xmlInternal); // Assign the texture to the material { Texture2D texture2d = AssetDatabase.LoadAssetAtPath(textureAsset, typeof(Texture2D)) as Texture2D; material.SetTexture("_MainTex", texture2d); } ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); importComponent.ImportTiled2UnityAsset(materialPath); } // If we have no materials to import then go to next stage (meshes) if (importComponent.ImportWait_Materials.Count() == 0) { ImportAllMeshes(importComponent); } }
private void ImportAllMaterials(Tiled2Unity.ImportBehaviour importComponent) { // Create a material for each texture that has been imported foreach (var xmlTexture in importComponent.XmlDocument.Root.Elements("ImportTexture")) { bool isResource = ImportUtils.GetAttributeAsBoolean(xmlTexture, "isResource", false); string textureFile = ImportUtils.GetAttributeAsString(xmlTexture, "filename"); string materialPath = MakeMaterialAssetPath(textureFile, isResource); string materialFile = System.IO.Path.GetFileName(materialPath); // Keep track that we importing this material if (!importComponent.ImportWait_Materials.Contains(materialFile, StringComparer.OrdinalIgnoreCase)) { importComponent.ImportWait_Materials.Add(materialFile); } // Create the material UnityEngine.Material material = CreateMaterialFromXml(xmlTexture, importComponent); // Assign the texture to the material { string textureAsset = GetTextureAssetPath(textureFile); AssignTextureAssetToMaterial(material, materialFile, textureAsset, importComponent); } ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); importComponent.ImportTiled2UnityAsset(materialPath); } // Create a material for each internal texture foreach (var xmlInternal in importComponent.XmlDocument.Root.Elements("InternalTexture")) { bool isResource = ImportUtils.GetAttributeAsBoolean(xmlInternal, "isResource", false); string textureAsset = ImportUtils.GetAttributeAsString(xmlInternal, "assetPath"); string textureFile = System.IO.Path.GetFileName(textureAsset); string materialPath = MakeMaterialAssetPath(textureFile, isResource); string materialFile = System.IO.Path.GetFileName(materialPath); // Keep track that we importing this material if (!importComponent.ImportWait_Materials.Contains(materialFile, StringComparer.OrdinalIgnoreCase)) { importComponent.ImportWait_Materials.Add(materialFile); } // Create the material and assign the texture UnityEngine.Material material = CreateMaterialFromXml(xmlInternal, importComponent); AssignTextureAssetToMaterial(material, materialFile, textureAsset, importComponent); ImportUtils.ReadyToWrite(materialPath); ImportUtils.CreateOrReplaceAsset(material, materialPath); importComponent.ImportTiled2UnityAsset(materialPath); } // If we have no materials to import then go to next stage (meshes) if (importComponent.ImportWait_Materials.Count() == 0) { ImportAllMeshes(importComponent); } }