Skip to content

Commit

Permalink
feat: Added EditorWindow.MoveOutOfScreen and AssetDatabaseHelper.Make…
Browse files Browse the repository at this point in the history
…SureFolderExists
  • Loading branch information
SolidAlloy committed Oct 8, 2020
1 parent 298491a commit d5ff96e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Editor/EditorWindows/AssetCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void OnGUI()

_calledOnGuiOnce = true;
ProjectWindowUtil.CreateAsset(_asset, _assetName);
position = new Rect(Screen.currentResolution.width + 10f, Screen.currentResolution.height + 10f, 0f, 0f);
this.MoveOutOfScreen();
}

private void OnDestroy()
Expand Down
7 changes: 7 additions & 0 deletions Editor/Extensions/EditorWindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public static void Resize(this EditorWindow window, float width = -1f, float hei
window.position = positionToAdjust;
}

/// <summary>Moves the window out of screen to hide but not close it.</summary>
/// <param name="window">The window to hide.</param>
public static void MoveOutOfScreen(this EditorWindow window)
{
window.position = new Rect(Screen.currentResolution.width + 10f, Screen.currentResolution.height + 10f, 0f, 0f);
}

private static void EnsureTheValueIsNotNegative(string valueName, float value)
{
if (value < 0f && value != -1f)
Expand Down
45 changes: 45 additions & 0 deletions Editor/Helpers/AssetDatabaseHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace SolidUtilities.Editor.Helpers
{
using System;
using System.Linq;
using UnityEditor;

/// <summary>Various methods that simplify <see cref="AssetDatabase"/> usage.</summary>
public static class AssetDatabaseHelper
{
/// <summary>
/// Creates folders in the path if they do not exist. The parent folder of the path is considered the Assets
/// folder. The path does not have to start with the Assets folder.
/// </summary>
/// <param name="folderPath">
/// The path to a folder. The parent folder of the path is considered the Assets folder.
/// </param>
/// <exception cref="ArgumentException">If the path contains a file.</exception>
/// <example><code>
/// string fullAssetPath = $"{Application.dataPath}/Scripts/GenericScriptableObjects/Generic_{className}.cs";
/// AssetDatabaseHelper.MakeSureFolderExists("Scripts/GenericScriptableObjects");
/// File.WriteAllText(fullAssetPath, template);
/// AssetDatabase.Refresh();
/// </code></example>
public static void MakeSureFolderExists(string folderPath)
{
if (folderPath.Contains("."))
{
throw new ArgumentException($"The path {folderPath} contains a file. The method can only " +
"create folders, files are not allowed.");
}

folderPath = folderPath.Trim('/');
var folders = folderPath.Split('/');

for (int i = 0; i < folders.Length; i++)
{
string folderToCreate = folders[i];
string parentFolders = $"Assets/{string.Join("/", folders.Take(i))}".TrimEnd('/');

if (!AssetDatabase.IsValidFolder($"{parentFolders}/{folderToCreate}"))
AssetDatabase.CreateFolder(parentFolders, folderToCreate);
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/Helpers/AssetDatabaseHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5ff96e

Please sign in to comment.