5using System.Collections.Generic;
6using System.Diagnostics.CodeAnalysis;
11using Object = UnityEngine.Object;
15 public sealed partial class Asset
17 public partial class Path
20 private static readonly String[] s_AllowedAssetSubfolders =
21 {
"assets",
"library",
"logs",
"packages",
"projectsettings",
"temp",
"usersettings" };
39 [ExcludeFromCodeCoverage]
45 [ExcludeFromCodeCoverage]
51 [ExcludeFromCodeCoverage]
57 [ExcludeFromCodeCoverage]
63 [ExcludeFromCodeCoverage]
80 public static Path Get([NotNull] Object asset)
82 var path = AssetDatabase.GetAssetPath(asset);
83 return String.IsNullOrEmpty(path) ? null : (
Path)path;
98 var path = AssetDatabase.GUIDToAssetPath(guid);
99 return String.IsNullOrEmpty(path) ? null : (
Path)path;
114 public static String[]
Get([NotNull] Object[] assets)
116 ThrowIf.ArgumentIsNull(assets, nameof(assets));
118 var objectCount = assets.Length;
119 var paths =
new String[objectCount];
120 for (var i = 0; i < objectCount; i++)
121 paths[i] =
Get(assets[i]);
140 AssetPathToGUIDOptions options = AssetPathToGUIDOptions.IncludeRecentlyDeletedAssets) =>
141 new(AssetDatabase.AssetPathToGUID(path, options));
153 [ExcludeFromCodeCoverage]
154 public static Path ToMeta([NotNull]
Path path) => AssetDatabase.GetTextMetaFilePathFromAssetPath(path);
166 [ExcludeFromCodeCoverage]
167 public static Path FromMeta([NotNull]
Path path) => AssetDatabase.GetAssetPathFromTextMetaFilePath(path);
178 public static Path GetScene([NotNull] Object instanceOrAsset) => AssetDatabase.GetAssetOrScenePath(instanceOrAsset);
194 public static Boolean
IsValid([NotNull] String path)
201 var fileName = System.IO.Path.GetFileName(path);
202 var folderName = System.IO.Path.GetDirectoryName(path);
205 var testIllegalChars =
new Func<Char, Boolean>(c => c ==
'*' || c ==
'?' || c ==
':');
206 isValid = folderName.Any(testIllegalChars) ==
false;
211 fileName = path.Substring(folderName.Length, path.Length - folderName.Length);
212 isValid = fileName.Any(testIllegalChars) ==
false;
217 SetLastErrorMessage($
"{ex.Message} => \"{path}\"");
232 public static Boolean
FileExists([NotNull]
Path path) => System.IO.File.Exists(path.m_RelativePath);
242 public static Boolean
FolderExists([NotNull]
Path path) => Directory.Exists(path.m_RelativePath);
256 ThrowIf.ArgumentIsNull(path, nameof(path));
257 ThrowIf.PathIsNotValid(path);
260 return path.FolderPath.Guid;
263 var isPresumablyFilePath = String.IsNullOrEmpty(path.Extension) ==
false;
264 var folderPath = isPresumablyFilePath ? path.FolderPath : path;
266 return folderPath.Guid;
268 var folderNames = ((String)folderPath).Split(
new[] {
'/' });
269 var folderGuid = GuidForExistingPath(folderNames[0]);
270 var partialPath = folderNames[0];
271 for (var i = 1; i < folderNames.Length; i++)
273 partialPath += $
"/{folderNames[i]}";
276 folderGuid = GuidForExistingPath(partialPath);
280 folderGuid = CreateSubFolder(
Get(folderGuid), folderNames[i]);
295 [ExcludeFromCodeCoverage]
316 var uniquePath = AssetDatabase.GenerateUniqueAssetPath(path);
317 return String.IsNullOrEmpty(uniquePath) ? path : uniquePath;
325 public static String[]
ToStrings([NotNull] IEnumerable<Path> paths) => paths.Select(path => (String)path).ToArray();
327 internal static Path UniquifyAsNeeded([NotNull]
Path path, Boolean overwriteExisting) =>
330 private static GUID CreateSubFolder([NotNull]
Path parentFolder, [NotNull] String subFolderName) =>
331 new(AssetDatabase.CreateFolder(parentFolder, subFolderName));
333 private static String ToRelative([NotNull] String fullOrRelativePath)
335 var relativePath = fullOrRelativePath;
336 if (IsRelative(relativePath) ==
false)
338 ThrowIf.NotAProjectPath(fullOrRelativePath);
339 relativePath = MakeRelative(fullOrRelativePath);
342 relativePath = relativePath.Trim(
'/');
344 ThrowIf.PathIsNotValid(relativePath);
348 private static Boolean IsRelative([NotNull] String path)
350 path = path.TrimStart(
'/').ToLower();
354 foreach (var allowedSubfolder
in s_AllowedAssetSubfolders)
356 var doesStartsWith = path.StartsWith(allowedSubfolder);
357 var subfolderLength = allowedSubfolder.Length;
358 var lengthMatches = path.Length == subfolderLength;
359 if (doesStartsWith && (lengthMatches || path[subfolderLength].
Equals(
'/')))
366 private static String MakeRelative([NotNull] String fullOrRelativePath) =>
369 private static GUID GuidForExistingPath([NotNull] String path) =>
370 new(AssetDatabase.AssetPathToGUID(path, AssetPathToGUIDOptions.OnlyExistingAssets));
static String FullLibraryPath
Returns the absolute path to the project's Library subfolder.
Path UniqueFilePath
Returns the path altered with a numbering if an asset already exists (and is imported) at the path.
Boolean Equals(Path other)
Tests another path for equality.
static Path FromMeta([NotNull] Path path)
Returns the asset's file path from a .meta file path.
static Boolean FolderExists([NotNull] Path path)
Tests if the given folder exists in the file system.
static String[] GetSubFolders([NotNull] Path path)
Returns the names of all subfolders in the path.
static String FullUserSettingsPath
Returns the absolute path to the project's UserSettings subfolder.
static String FullProjectPath
Returns the absolute path to the project's root folder.
static String FullLogsPath
Returns the absolute path to the project's Logs subfolder.
static String[] ToStrings([NotNull] IEnumerable< Path > paths)
Converts an IEnumerable collection of Path instances to a string array.
static String FullProjectTempPath
Returns the absolute path to the project's Temp subfolder.
static String[] Get([NotNull] Object[] assets)
Converts an array of asset instances to their asset paths.
static Path UniquifyFileName([NotNull] Path path)
Returns the path altered with a numbering if an asset already exists (and is imported) at the path.
static GUID GetGuid([NotNull] Path path, AssetPathToGUIDOptions options=AssetPathToGUIDOptions.IncludeRecentlyDeletedAssets)
Returns the GUID for an asset path.
static GUID CreateFolders([NotNull] Path path)
Creates any missing folders in the path.
static Path Get([NotNull] Object asset)
Gets the relative path of an asset.
static String FullAssetsPath
Returns the absolute path to the project's Assets subfolder.
static Path GetScene([NotNull] Object instanceOrAsset)
Returns the scene's path if the object is instantiated in a scene, otherwise returns the object's pat...
static String FullProjectSettingsPath
Returns the absolute path to the project's ProjectSettings subfolder.
static String FullPackagesPath
Returns the absolute path to the project's Packages subfolder.
static Boolean FileExists([NotNull] Path path)
Tests if the given file exists in the file system.
static Path ToMeta([NotNull] Path path)
Returns the .meta file path for an asset path.
static Path Get(GUID guid)
Gets the relative path of an asset.
static Boolean IsValid([NotNull] String path)
Returns true if the provided path is valid.
Represents a relative path to an asset file or folder, typically under 'Assets' or 'Packages'.