static void CreateNavTilesFromSprites() { Object[] objects = Selection.objects; List <Sprite> sprites = new List <Sprite>(); for (int i = 0; i < objects.Length; i++) { // Only use sprites, no subclasses. if (objects[i].GetType() == typeof(Sprite)) { sprites.Add((Sprite)objects[i]); } } if (sprites.Count == 0) { Debug.LogWarning("No sprites selected to create NavTiles for."); return; } string path = EditorUtility.OpenFolderPanel("Choose a save location for the NavTiles", Application.dataPath, ""); path = path.Substring(path.IndexOf("Assets")); if (string.IsNullOrEmpty(path)) { return; } for (int i = 0; i < sprites.Count; i++) { EditorUtility.DisplayProgressBar("Creating NavTile(s)", $"Creating from {sprites[i].name}", (float)i / (sprites.Count - 1)); NavTile tile = ScriptableObject.CreateInstance <NavTile>(); tile.sprite = sprites[i]; if (AssetDatabase.LoadAssetAtPath($"{path}/{sprites[i].name}.asset", typeof(NavTile))) { // Create dialog to ask if User is sure they want to overwrite existing prefab. if (EditorUtility.DisplayDialog("Are you sure?", "A NavTile with the name '" + sprites[i].name + "' already exists. Do you want to overwrite it?", "Yes", "No")) // If the user presses the yes button, create the Prefab. { AssetDatabase.CreateAsset(tile, $"{path}/{sprites[i].name}.asset"); } } else { AssetDatabase.CreateAsset(tile, $"{path}/{sprites[i].name}.asset"); } } EditorUtility.ClearProgressBar(); }
static void ConvertTiles() { Object[] objects = Selection.objects; List <Tile> tiles = new List <Tile>(); for (int i = 0; i < objects.Length; i++) { // Only use tiles, no subclasses. if (objects[i].GetType() == typeof(Tile)) { tiles.Add((Tile)objects[i]); } } Object[] newTiles = new Object[tiles.Count]; for (int i = 0; i < tiles.Count; i++) { EditorUtility.DisplayProgressBar("Converting to NavTile(s)", $"Converting {tiles[i].name}", (float)i / (tiles.Count - 1)); NavTile nTile = ScriptableObject.CreateInstance <NavTile>(); nTile.CreateFromTile(tiles[i]); string path = AssetDatabase.GetAssetPath(tiles[i]); Tilemap[] maps = Resources.FindObjectsOfTypeAll <Tilemap>(); for (int x = 0; x < maps.Length; x++) { if (maps[x].ContainsTile(tiles[i])) { maps[x].SwapTile(tiles[i], nTile); } } AssetDatabase.CreateAsset(nTile, path); newTiles[i] = nTile; } EditorUtility.ClearProgressBar(); Selection.objects = newTiles; }