private GameObject[] GetValidTargets() { if (m_FlushPaintTargetCache) { m_CachedPaintTargets = null; if (activeBrushEditor != null) { m_CachedPaintTargets = activeBrushEditor.validTargets; } if (m_CachedPaintTargets == null || m_CachedPaintTargets.Length == 0) { scenePaintTarget = null; } else { var comparer = GridPaintActiveTargetsPreferences.GetTargetComparer(); if (comparer != null) { Array.Sort(m_CachedPaintTargets, comparer); } } m_FlushPaintTargetCache = false; } return(m_CachedPaintTargets); }
internal static SettingsProvider CreateSettingsProvider() { var settingsProvider = new SettingsProvider("Preferences/2D/Tile Palette", SettingsScope.User, SettingsProvider.GetSearchKeywordsFromGUIContentProperties <GridPaintPaletteWindowPreferences>()) { guiHandler = searchContext => { GridPaintPaletteWindow.PreferencesGUI(); GridPaintActiveTargetsPreferences.PreferencesGUI(); } }; return(settingsProvider); }
public static Dictionary <Vector2Int, TileBase> ConvertToTileSheet(Dictionary <Vector2Int, Object> sheet) { Dictionary <Vector2Int, TileBase> result = new Dictionary <Vector2Int, TileBase>(); string defaultPath = ProjectBrowser.s_LastInteractedProjectBrowser ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath() : "Assets"; // Early out if all objects are already tiles if (sheet.Values.ToList().FindAll(obj => obj is TileBase).Count == sheet.Values.Count) { foreach (KeyValuePair <Vector2Int, Object> item in sheet) { result.Add(item.Key, item.Value as TileBase); } return(result); } UserTileCreationMode userTileCreationMode = UserTileCreationMode.Overwrite; string path = ""; bool multipleTiles = sheet.Count > 1; if (multipleTiles) { bool userInterventionRequired = false; path = EditorUtility.SaveFolderPanel("Generate tiles into folder ", defaultPath, ""); path = FileUtil.GetProjectRelativePath(path); // Check if this will overwrite any existing assets foreach (var item in sheet.Values) { if (item is Sprite) { var tilePath = FileUtil.CombinePaths(path, String.Format("{0}.{1}", item.name, k_TileExtension)); if (File.Exists(tilePath)) { userInterventionRequired = true; break; } } } // There are existing tile assets in the folder with names matching the items to be created if (userInterventionRequired) { var option = EditorUtility.DisplayDialogComplex("Overwrite?", String.Format("Assets exist at {0}. Do you wish to overwrite existing assets?", path), "Overwrite", "Create New Copy", "Reuse"); switch (option) { case 0: // Overwrite { userTileCreationMode = UserTileCreationMode.Overwrite; } break; case 1: // Create New Copy { userTileCreationMode = UserTileCreationMode.CreateUnique; } break; case 2: // Reuse { userTileCreationMode = UserTileCreationMode.Reuse; } break; } } } else { // Do not check if this will overwrite new tile as user has explicitly selected the file to save to path = EditorUtility.SaveFilePanelInProject("Generate new tile", sheet.Values.First().name, k_TileExtension, "Generate new tile", defaultPath); } if (string.IsNullOrEmpty(path)) { return(result); } int i = 0; EditorUtility.DisplayProgressBar("Generating Tile Assets (" + i + "/" + sheet.Count + ")", "Generating tiles", 0f); try { MethodInfo createTileMethod = GridPaintActiveTargetsPreferences.GetCreateTileFromPaletteUsingPreferences(); if (createTileMethod == null) { return(null); } foreach (KeyValuePair <Vector2Int, Object> item in sheet) { TileBase tile; string tilePath = ""; if (item.Value is Sprite) { tile = createTileMethod.Invoke(null, new object[] { item.Value as Sprite }) as TileBase; if (tile == null) { continue; } tilePath = multipleTiles ? FileUtil.CombinePaths(path, String.Format("{0}.{1}", tile.name, k_TileExtension)) : path; switch (userTileCreationMode) { case UserTileCreationMode.CreateUnique: { if (File.Exists(tilePath)) { tilePath = AssetDatabase.GenerateUniqueAssetPath(tilePath); } AssetDatabase.CreateAsset(tile, tilePath); } break; case UserTileCreationMode.Overwrite: { AssetDatabase.CreateAsset(tile, tilePath); } break; case UserTileCreationMode.Reuse: { if (File.Exists(tilePath)) { tile = AssetDatabase.LoadAssetAtPath <TileBase>(tilePath); } else { AssetDatabase.CreateAsset(tile, tilePath); } } break; } } else { tile = item.Value as TileBase; } EditorUtility.DisplayProgressBar("Generating Tile Assets (" + i + "/" + sheet.Count + ")", "Generating " + tilePath, (float)i++ / sheet.Count); result.Add(item.Key, tile); } } finally { EditorUtility.ClearProgressBar(); } AssetDatabase.Refresh(); return(result); }
public static List <TileBase> ConvertToTileSheet(Dictionary <Vector2Int, TileDragAndDropHoverData> sheet) { List <TileBase> result = new List <TileBase>(); string defaultPath = TileDragAndDropManager.GetDefaultTileAssetPath(); // Early out if all objects are already tiles if (sheet.Values.ToList().FindAll(data => data.hoverObject is TileBase).Count == sheet.Values.Count) { foreach (var item in sheet.Values) { result.Add(item.hoverObject as TileBase); } return(result); } UserTileCreationMode userTileCreationMode = UserTileCreationMode.Overwrite; string path = ""; bool multipleTiles = sheet.Count > 1; int i = 0; HashSet <String> uniqueNames = new HashSet <string>(); if (multipleTiles) { bool userInterventionRequired = false; path = EditorUtility.SaveFolderPanel("Generate tiles into folder ", defaultPath, ""); path = FileUtil.GetProjectRelativePath(path); // Check if this will overwrite any existing assets foreach (var item in sheet.Values) { if (item.hoverObject is Sprite sprite) { var name = sprite.name; if (String.IsNullOrEmpty(name) || uniqueNames.Contains(name)) { name = GenerateUniqueNameForNamelessSprite(sprite, uniqueNames, ref i); } uniqueNames.Add(name); var tilePath = FileUtil.CombinePaths(path, String.Format("{0}.{1}", name, k_TileExtension)); if (File.Exists(tilePath)) { userInterventionRequired = true; break; } } } // There are existing tile assets in the folder with names matching the items to be created if (userInterventionRequired) { var option = EditorUtility.DisplayDialogComplex("Overwrite?", String.Format("Assets exist at {0}. Do you wish to overwrite existing assets?", path), "Overwrite", "Create New Copy", "Reuse"); switch (option) { case 0: // Overwrite { userTileCreationMode = UserTileCreationMode.Overwrite; } break; case 1: // Create New Copy { userTileCreationMode = UserTileCreationMode.CreateUnique; } break; case 2: // Reuse { userTileCreationMode = UserTileCreationMode.Reuse; } break; } } } else { // Do not check if this will overwrite new tile as user has explicitly selected the file to save to path = EditorUtility.SaveFilePanelInProject("Generate new tile", sheet.Values.First().hoverObject.name, k_TileExtension, "Generate new tile", defaultPath); } TileDragAndDropManager.SetUserTileAssetPath(path); if (string.IsNullOrEmpty(path)) { return(result); } i = 0; uniqueNames.Clear(); EditorUtility.DisplayProgressBar("Generating Tile Assets (" + i + "/" + sheet.Count + ")", "Generating tiles", 0f); try { MethodInfo createTileMethod = GridPaintActiveTargetsPreferences.GetCreateTileFromPaletteUsingPreferences(); if (createTileMethod == null) { return(null); } foreach (KeyValuePair <Vector2Int, TileDragAndDropHoverData> item in sheet) { TileBase tile; string tilePath = ""; if (item.Value.hoverObject is Sprite sprite) { tile = createTileMethod.Invoke(null, new object[] { sprite }) as TileBase; if (tile == null) { continue; } var name = tile.name; if (String.IsNullOrEmpty(name) || uniqueNames.Contains(name)) { name = GenerateUniqueNameForNamelessSprite(sprite, uniqueNames, ref i); } uniqueNames.Add(name); tilePath = multipleTiles ? FileUtil.CombinePaths(path, String.Format("{0}.{1}", name, k_TileExtension)) : path; // Case 1216101: Fix path slashes for Windows tilePath = FileUtil.NiceWinPath(tilePath); switch (userTileCreationMode) { case UserTileCreationMode.CreateUnique: { if (File.Exists(tilePath)) { tilePath = AssetDatabase.GenerateUniqueAssetPath(tilePath); } AssetDatabase.CreateAsset(tile, tilePath); } break; case UserTileCreationMode.Overwrite: { AssetDatabase.CreateAsset(tile, tilePath); } break; case UserTileCreationMode.Reuse: { if (File.Exists(tilePath)) { tile = AssetDatabase.LoadAssetAtPath <TileBase>(tilePath); } else { AssetDatabase.CreateAsset(tile, tilePath); } } break; } } else { tile = item.Value.hoverObject as TileBase; } EditorUtility.DisplayProgressBar("Generating Tile Assets (" + i + "/" + sheet.Count + ")", "Generating " + tilePath, (float)i++ / sheet.Count); result.Add(tile); } } finally { EditorUtility.ClearProgressBar(); } AssetDatabase.Refresh(); return(result); }