/// <summary> /// Gets sprite animation of the provided tile ID /// </summary> /// <returns>Array containing animation frames and their times if any, null otherwize</returns> /// <param name="tileID">Id of the tile in the tmx map file</param> /// <param name="map">The tmx map deserializtion</param> private AnimationFrame[] GetAnimations(int tileID, TiledMap map) { for (int i = 0; i < map.tileSets.Length; i++) { TiledTileSetFile tSet = map.tileSets[i]; if (tSet.tiles != null) { foreach (TiledTile tile in tSet.tiles) { int id = tileID - map.tileSetEntries[i].firstGID; if (id == tile.id && tile.animation != null) { AnimationFrame[] result = new AnimationFrame[tile.animation.frames.Length]; for (int j = 0; j < result.Length; j++) { result[j].sprite = GetTile(tile.animation.frames[j].tileID + map.tileSetEntries[i].firstGID, map); result[j].duration = tile.animation.frames[j].duration; } return(result); } } } } return(null); }
/// <summary> /// TODO: Function under construction /// </summary> /// <param name="tileID">Tile I.</param> /// <param name="map">Map.</param> /// <param name="tileObject">Tile object.</param> private void AttachTileProperties(int tileID, TiledMap map, GameObject tileObject) { for (int i = 0; i < map.tileSets.Length; i++) { TiledTileSetFile tSet = map.tileSets[i]; if (tSet.image == null) { //for single images foreach (TiledTile tile in tSet.tiles) { if (tile.id + map.tileSetEntries[i].firstGID == tileID) { if (tile.customProperties != null && tile.customProperties.Length > 0) { Tile3D tileScript = tileObject.AddComponent <Tile3D>(); for (int j = 0; j < tile.customProperties.Length; j++) { string pName = tile.customProperties[j].name.ToLower(); string pValue = tile.customProperties[j].value; if (pName.Equals("generatecollider")) { bool gen; if (bool.TryParse(pValue, out gen)) { tileScript.generateCollider = gen; } } else if (pName.Equals("minrandomdisplacement") || pName.Equals("maxrandomdisplacement")) { /* * float x, y, z; * Vector3 displacement; * string[] vals = pValue.Split(','); * if (vals.Length == 3) * { * //TODO: continue from here * } */ } } } } } } } }
private TiledObjectGroup GetObjectsGroup(TiledMap map, int tileId) { for (int i = 0; i < map.tileSets.Length; i++) { TiledTileSetFile tsFile = map.tileSets[i]; if (tsFile.tiles != null) { foreach (TiledTile tile in tsFile.tiles) { if (tile.id + map.tileSetEntries[i].firstGID == tileId) { return(tile.objectsGroup); } } } } return(null); }
/// <summary> /// Loads the sprite of the provided tile ID from Resources folder /// </summary> /// <returns>Sprite of the tile if found, null otherwise</returns> /// <param name="tileID">Id of the tile in the tmx map file</param> /// <param name="map">The tmx map deserialization</param> private Sprite GetTile(int tileID, TiledMap map) { for (int i = 0; i < map.tileSets.Length; i++) { string tsxFolder = tsxOriginalLocations[i].Substring(0, tsxOriginalLocations[i].LastIndexOfAny(FILEPATH_SEPARATORS)); tsxFolder = tsxFolder.Substring(tsxFolder.IndexOf("Assets")); TiledTileSetFile tSet = map.tileSets[i]; if (tSet.image == null) { //for single images foreach (TiledTile tile in tSet.tiles) { if (tile.id + map.tileSetEntries[i].firstGID == tileID) { string path = FormatPath(tsxFolder + "/" + tile.image.source); Sprite result = null; int resIndex = path.ToLower().IndexOf("resources"); string fullPath = Path.Combine(Application.dataPath, path); fullPath = Path.GetFullPath((new Uri(fullPath)).LocalPath); path = fullPath.Substring(fullPath.IndexOf("Assets") + 7); if (resIndex > 0) { string resPath = path.Substring(resIndex + 10, path.Length - (resIndex + 10)); resPath = resPath.Substring(0, resPath.LastIndexOf(".")); result = Resources.Load <Sprite>(resPath); } else { result = AssetDatabase.LoadAssetAtPath <Sprite>(path); } if (result == null) { if (File.Exists(Application.dataPath.Replace("Assets", "") + path)) { EditorScriptsController.enableSpriteImporter2D = true; EditorScriptsController.importSpriteSheet = false; EditorScriptsController.targetMap = map; EditorScriptsController.textureFilterMode = spriteFilterMode; //File is there, but //import settings were off, give another chance to find the sprite AssetDatabase.ImportAsset(path); exceptionsToTolerate++; } throw new FileNotFoundException("'" + path + "': Tile not found"); } return(result); } } } else { //for spritesheet int id = tileID - map.tileSetEntries[i].firstGID; if (id >= 0 && id < tSet.tileCount) { string path = FormatPath(tsxFolder + "/" + tSet.image.source); UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetsAtPath(path); bool settingsChanged = false; if (EditorScriptsController.textureFilterMode != spriteFilterMode) { settingsChanged = true; } if (objs == null || objs.Length - 1 < tSet.tileCount || settingsChanged) { if (File.Exists(Application.dataPath.Replace("Assets", "") + path)) { EditorScriptsController.enableSpriteImporter2D = true; EditorScriptsController.importSpriteSheet = true; EditorScriptsController.textureFilterMode = spriteFilterMode; EditorScriptsController.targetMap = map; EditorScriptsController.targetTileset = i; AssetDatabase.ImportAsset(path); exceptionsToTolerate++; if (settingsChanged) { throw new System.Exception("Nothing serious, just to fire reimport"); } } } Sprite[] sprites = new Sprite[objs.Length - 1]; for (int j = 1; j < objs.Length; j++) { sprites[j - 1] = (Sprite)objs[j]; } return(sprites[id]); } } } return(null); }
private void OnPreprocessTexture() { if (!EditorScriptsController.enableSpriteImporter2D) { return; } TiledMap map = EditorScriptsController.targetMap; TextureImporter importer = (TextureImporter)assetImporter; //object[] args = new object[2] { 0, 0 }; //MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); //mi.Invoke(importer, args); //int width = (int)args[0]; //int height = (int)args[1]; TextureImporterSettings texSettings = new TextureImporterSettings(); importer.ReadTextureSettings(texSettings); texSettings.filterMode = EditorScriptsController.textureFilterMode; if (EditorScriptsController.importSpriteSheet) { texSettings.spriteMode = (int)SpriteImportMode.Multiple; TiledTileSetFile tSet = map.tileSets[EditorScriptsController.targetTileset]; SpriteMetaData[] tiles = new SpriteMetaData[tSet.tileCount]; int row = (tSet.tileCount / tSet.columns) - 1; int rows = row; int col = 0; for (int i = 0; i < tiles.Length; i++) { SpriteMetaData dat = new SpriteMetaData(); dat.pivot = Vector2.zero; Vector2 pos = Vector2.zero; pos.y += tSet.margin; pos.x += tSet.margin; pos.x += col * (map.tileWidth + tSet.spacing); pos.y += row * (map.tileHeight + tSet.spacing); Rect tileRect = new Rect(pos.x, pos.y, map.tileWidth, map.tileHeight); dat.rect = tileRect; dat.name = "Tile_" + i; col++; if (col == tSet.columns) { col = 0; row--; } tiles[i] = dat; } importer.spritesheet = tiles; } else { texSettings.spriteMode = (int)SpriteImportMode.Single; } importer.filterMode = EditorScriptsController.textureFilterMode; texSettings.spriteAlignment = (int)SpriteAlignment.Custom; importer.SetTextureSettings(texSettings); int ppu = map != null?Mathf.Min(map.tileWidth, map.tileHeight) : 100; importer.spritePixelsPerUnit = ppu; importer.textureType = TextureImporterType.Sprite; importer.spritePivot = Vector2.zero; }