private void HandleTiledAttributes(GameObject gameObject, XElement goXml, Tiled2Unity.ImportBehaviour importComponent) { // Add the TiledMap component TiledMap map = gameObject.AddComponent <TiledMap>(); try { map.Orientation = ImportUtils.GetAttributeAsEnum <TiledMap.MapOrientation>(goXml, "orientation"); map.StaggerAxis = ImportUtils.GetAttributeAsEnum <TiledMap.MapStaggerAxis>(goXml, "staggerAxis"); map.StaggerIndex = ImportUtils.GetAttributeAsEnum <TiledMap.MapStaggerIndex>(goXml, "staggerIndex"); map.HexSideLength = ImportUtils.GetAttributeAsInt(goXml, "hexSideLength"); map.NumLayers = ImportUtils.GetAttributeAsInt(goXml, "numLayers"); map.NumTilesWide = ImportUtils.GetAttributeAsInt(goXml, "numTilesWide"); map.NumTilesHigh = ImportUtils.GetAttributeAsInt(goXml, "numTilesHigh"); map.TileWidth = ImportUtils.GetAttributeAsInt(goXml, "tileWidth"); map.TileHeight = ImportUtils.GetAttributeAsInt(goXml, "tileHeight"); map.ExportScale = ImportUtils.GetAttributeAsFloat(goXml, "exportScale"); map.MapWidthInPixels = ImportUtils.GetAttributeAsInt(goXml, "mapWidthInPixels"); map.MapHeightInPixels = ImportUtils.GetAttributeAsInt(goXml, "mapHeightInPixels"); map.BackgroundColor = ImportUtils.GetAttributeAsColor(goXml, "backgroundColor", Color.black); } catch { importComponent.RecordWarning("Couldn't add TiledMap component. Are you using an old version of Tiled2Unity in your Unity project?"); GameObject.DestroyImmediate(map); } }
private void ImportAllMeshes(Tiled2Unity.ImportBehaviour importComponent) { foreach (var xmlMesh in importComponent.XmlDocument.Root.Elements("ImportMesh")) { // We're going to create/write a file that contains our mesh data as a Wavefront Obj file // The actual mesh will be imported from this Obj file string file = ImportUtils.GetAttributeAsString(xmlMesh, "filename"); string data = xmlMesh.Value; // Keep track of mesh we're going to import if (!importComponent.ImportWait_Meshes.Contains(file)) { importComponent.ImportWait_Meshes.Add(file); } // The data is in base64 format. We need it as a raw string. string raw = ImportUtils.Base64ToString(data); // Save and import the asset string pathToMesh = GetMeshAssetPath(file); ImportUtils.ReadyToWrite(pathToMesh); File.WriteAllText(pathToMesh, raw, Encoding.UTF8); importComponent.ImportTiled2UnityAsset(pathToMesh); } // If we have no meshes to import then go to next stage if (importComponent.ImportWait_Meshes.Count() == 0) { ImportAllPrefabs(importComponent, null); } }
private void ImportAllPrefabs(Tiled2Unity.ImportBehaviour importComponent) { foreach (var xmlPrefab in importComponent.XmlDocument.Root.Elements("Prefab")) { CreatePrefab(xmlPrefab, importComponent); } }
private void ImportAllTextures(Tiled2Unity.ImportBehaviour importComponent) { // Textures need to be imported before we can create or import materials foreach (var xmlImportTexture in importComponent.XmlDocument.Root.Elements("ImportTexture")) { string filename = ImportUtils.GetAttributeAsString(xmlImportTexture, "filename"); string data = xmlImportTexture.Value; byte[] bytes = ImportUtils.Base64ToBytes(data); // Keep track that we are importing this texture if (!importComponent.ImportWait_Textures.Contains(filename, StringComparer.OrdinalIgnoreCase)) { importComponent.ImportWait_Textures.Add(filename); } // Start the import process for this texture string pathToSave = GetTextureAssetPath(filename); ImportUtils.ReadyToWrite(pathToSave); File.WriteAllBytes(pathToSave, bytes); importComponent.ImportTiled2UnityAsset(pathToSave); } // If we have no textures too import then go to next stage (materials) if (importComponent.ImportWait_Textures.Count() == 0) { ImportAllMaterials(importComponent); } }
private void CreatePrefab(XElement xmlPrefab, Tiled2Unity.ImportBehaviour importComponent) { var customImporters = GetCustomImporterInstances(importComponent); // Part 1: Create the prefab string prefabName = xmlPrefab.Attribute("name").Value; float prefabScale = ImportUtils.GetAttributeAsFloat(xmlPrefab, "scale", 1.0f); GameObject tempPrefab = new GameObject(prefabName); HandleTiledAttributes(tempPrefab, xmlPrefab, importComponent); HandleCustomProperties(tempPrefab, xmlPrefab, customImporters); // 한도영! { tempPrefab.tag = "Map"; tempPrefab.AddComponent <Map>(); } // Part 2: Build out the prefab // We may have an 'isTrigger' attribute that we want our children to obey bool isTrigger = ImportUtils.GetAttributeAsBoolean(xmlPrefab, "isTrigger", false); AddGameObjectsTo(tempPrefab, xmlPrefab, isTrigger, importComponent, customImporters); // Part 3: Allow for customization from other editor scripts to be made on the prefab // (These are generally for game-specific needs) CustomizePrefab(tempPrefab, customImporters); // Part 3.5: Apply the scale only after all children have been added tempPrefab.transform.localScale = new Vector3(prefabScale, prefabScale, prefabScale); // Part 4: Save the prefab, keeping references intact. string resourcePath = ImportUtils.GetAttributeAsString(xmlPrefab, "resourcePath", ""); bool isResource = !String.IsNullOrEmpty(resourcePath) || ImportUtils.GetAttributeAsBoolean(xmlPrefab, "resource", false); string prefabPath = GetPrefabAssetPath(prefabName, isResource, resourcePath); string prefabFile = Path.GetFileName(prefabPath); // Keep track of the prefab file being imported if (!importComponent.ImportWait_Prefabs.Contains(prefabFile, StringComparer.OrdinalIgnoreCase)) { importComponent.ImportWait_Prefabs.Add(prefabFile); importComponent.ImportingAssets.Add(prefabPath); } UnityEngine.Object finalPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)); if (finalPrefab == null) { // The prefab needs to be created ImportUtils.ReadyToWrite(prefabPath); finalPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath); } // Replace the prefab, keeping connections based on name. This imports the prefab asset as a side-effect. PrefabUtility.ReplacePrefab(tempPrefab, finalPrefab, ReplacePrefabOptions.ReplaceNameBased); // Destroy the instance from the current scene hiearchy. UnityEngine.Object.DestroyImmediate(tempPrefab); }
private void CheckSettings(Tiled2Unity.ImportBehaviour importComponent) { // Check anti-aliasing if (QualitySettings.antiAliasing != 0) { importComponent.RecordWarning("Anti-aliasing is enabled and may cause seams. See Edit->Project Settings->Quality to disable."); } }
private UnityEngine.Material CreateMaterialFromXml(XElement xml, Tiled2Unity.ImportBehaviour importComponent) { // Does this material support alpha color key? bool useColorKey = xml.Attribute("alphaColorKey") != null; bool usesDepthShader = ImportUtils.GetAttributeAsBoolean(xml, "usesDepthShaders", false); // Determine which shader we sould be using string shaderName = "Tiled2Unity/"; // Are we using depth shaders? if (usesDepthShader) { shaderName += "Depth"; } else { shaderName += "Default"; } // Are we using color key shaders? Color keyColor = Color.black; if (useColorKey) { keyColor = ImportUtils.GetAttributeAsColor(xml, "alphaColorKey"); shaderName += " Color Key"; } // Try creating the material with the right shader. Fall back to the built-in Sprites/Default shader if there's a problem. UnityEngine.Material material = null; try { material = new UnityEngine.Material(UnityEngine.Shader.Find(shaderName)); } catch (Exception e) { importComponent.RecordError("Error creating material with shader '{0}'. {1}", shaderName, e.Message); } if (material == null) { importComponent.RecordWarning("Using default sprite shader for material"); material = new UnityEngine.Material(UnityEngine.Shader.Find("Sprites/Default")); } if (useColorKey) { material.SetColor("_AlphaColorKey", keyColor); } return(material); }
private void CheckVersion(Tiled2Unity.ImportBehaviour importComponent, Tiled2Unity.ImportTiled2Unity importTiled2Unity) { try { // Get the version from our Tiled2Unity.export.txt library data file TextAsset textAsset = importTiled2Unity.GetTiled2UnityTextAsset(); XDocument xml = XDocument.Parse(textAsset.text); string importerTiled2UnityVersion = xml.Element("Tiled2UnityImporter").Element("Header").Attribute("version").Value; if (importComponent.ExporterTiled2UnityVersion != importerTiled2UnityVersion) { importComponent.RecordWarning("Imported Tiled2Unity file '{0}' was exported with version {1}. We are expecting version {2}", importComponent.Tiled2UnityXmlPath, importComponent.ExporterTiled2UnityVersion, importerTiled2UnityVersion); } } catch (Exception e) { importComponent.RecordError("Failed to read Tiled2Unity import version from '{0}': {1}", importComponent.Tiled2UnityXmlPath, e.Message); } }
private LinkedTilesets ConstructTilesets(XElement xml, Tiled2Unity.ImportBehaviour importComponent) { LinkedTilesets tilesets = new LinkedTilesets(); foreach (XElement tilesetXml in xml.Descendants("TilesetFirstGid")) { int firstGid = int.Parse(tilesetXml.Attribute("firstGid").Value); // unfortunately we lose directory info here string tilesetName = tilesetXml.Attribute("tilesetName").Value; string xmlDir = importComponent.Tiled2UnityXmlPath.Substring(0, importComponent.Tiled2UnityXmlPath.LastIndexOf('/')); string tilesetsDir = xmlDir.Substring(0, xmlDir.LastIndexOf('/')); string tilesetPath = tilesetsDir + "/Tilesets/" + tilesetName + ".asset"; Tileset tileset = AssetDatabase.LoadAssetAtPath <Tileset>(tilesetPath); tilesets[firstGid] = tileset; } return(tilesets); }
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); // "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 = 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); } }