5using System.Diagnostics.CodeAnalysis;
8using Object = UnityEngine.Object;
12 public sealed partial class Asset
24 public partial class Path : IEquatable<Path>, IEquatable<String>
26 private const String DefaultExtension =
"asset";
28 private String m_RelativePath = String.Empty;
39 public GUID
Guid =>
GetGuid(
this, AssetPathToGUIDOptions.OnlyExistingAssets);
61#if UNITY_2023_2_OR_NEWER
62 return AssetDatabase.AssetPathExists(m_RelativePath);
64 return AssetDatabase.AssetPathToGUID(m_RelativePath, AssetPathToGUIDOptions.OnlyExistingAssets).Length > 0;
95 [ExcludeFromCodeCoverage]
107 [ExcludeFromCodeCoverage]
118 public String
Extension => System.IO.Path.GetExtension(m_RelativePath);
127 [ExcludeFromCodeCoverage]
128 public String
FileName => System.IO.Path.GetFileName(m_RelativePath);
137 [ExcludeFromCodeCoverage]
146 public String
FullPath => System.IO.Path.GetFullPath(m_RelativePath).ToForwardSlashes();
160 [ExcludeFromCodeCoverage]
176 var dirName = System.IO.Path.GetDirectoryName(m_RelativePath);
177 return String.IsNullOrEmpty(dirName) ? null : dirName;
194 [ExcludeFromCodeCoverage]
private Path() {}
223 public Path([NotNull] String fullOrRelativePath)
225 ThrowIf.NullOrWhitespace(fullOrRelativePath, nameof(fullOrRelativePath));
226 m_RelativePath = ToRelative(fullOrRelativePath.ToForwardSlashes());
251 public Path([NotNull] String folderPath, [NotNull] String fileName, [NotNull] String extension = DefaultExtension)
253 ThrowIf.NullOrWhitespace(folderPath, nameof(folderPath));
254 ThrowIf.NullOrWhitespace(fileName, nameof(fileName));
255 ThrowIf.NullOrWhitespace(extension, nameof(extension));
256 ThrowIf.ContainsPathSeparators(fileName, nameof(fileName));
258 var relativeDir = ToRelative(folderPath.ToForwardSlashes());
259 m_RelativePath = $
"{relativeDir}/{fileName}.{extension.TrimStart('.').ToLower()}";
270 public Path([NotNull] Object asset)
272 ThrowIf.ArgumentIsNull(asset, nameof(asset));
273 ThrowIf.NotInDatabase(asset);
275 m_RelativePath =
Get(asset);
286 if (ReferenceEquals(other,
null))
288 if (ReferenceEquals(
this, other))
291 return m_RelativePath.Equals(other.m_RelativePath);
301 public Boolean
Equals(String other) => m_RelativePath.Equals(
new Path(other).m_RelativePath);
308 public static implicit
operator String(
Path path) => path !=
null ? path.m_RelativePath :
null;
315 public static implicit
operator Path(String path) => path !=
null ?
new Path(path) : null;
325 if (ReferenceEquals(path1, path2))
327 if (ReferenceEquals(path1,
null))
329 if (ReferenceEquals(path2,
null))
332 return path1.
Equals(path2);
350 other is String str ? path1.Equals(str) : path1.Equals(other as
Path);
358 public static Boolean
operator !=(
Path path1, System.Object other) => !(path1 == other);
374 public static Boolean
operator !=(System.Object other,
Path path) => !(path == other);
382 [ExcludeFromCodeCoverage]
391 public void Rename(String newFileOrFolderName)
393 if (String.IsNullOrEmpty(newFileOrFolderName) ==
false)
394 m_RelativePath = $
"{FolderPath}/{System.IO.Path.GetFileName(newFileOrFolderName)}";
419 public override String
ToString() => m_RelativePath;
430 public override Boolean
Equals(System.Object obj)
432 if (obj is
Path path)
434 if (obj is String str)
444 public override Int32
GetHashCode() => m_RelativePath.GetHashCode();
Path MetaPath
Returns the path to the .meta file if the path is an asset file or folder.
override String ToString()
Returns the relative path as string.
String[] SubFolders
Returns the names of all folders in a path to a folder.
Path UniqueFilePath
Returns the path altered with a numbering if an asset already exists (and is imported) at the path.
static Boolean operator!=(Path path1, Path path2)
Tests two path instances for inequality.
Path([NotNull] String fullOrRelativePath)
Creates an asset path from either an absolute or relative 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.
String FileNameWithoutExtension
Returns the file name without extension.
override Boolean Equals(System.Object obj)
Tests path for equality with an object.
Path([NotNull] Object asset)
Creates an asset path from an asset instance.
String FileName
Returns the file name with extension.
Path AssetPath
Returns the path to the asset file if the path represents a .meta file.
Boolean ExistsInFileSystem
Returns true if the path exists in the file system.
GUID Guid
Returns the GUID for the path.
String Extension
Returns the extension of the path.
GUID CreateFolders()
Creates the folders in the path recursively.
Boolean Equals(String other)
Tests another path for equality.
Path FolderPath
Returns the relative path to the directory the file or folder is in.
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 Path Get([NotNull] Object asset)
Gets the relative path of an asset.
void Rename(String newFileOrFolderName)
Renames the last element of the CodeSmileEditor.Asset.Path instance.
Path([NotNull] String folderPath, [NotNull] String fileName, [NotNull] String extension=DefaultExtension)
Creates an asset path by combining folder path, file name and extension.
override Int32 GetHashCode()
Returns the internal path string's hash code.
static Boolean operator==(Path path1, Path path2)
Tests two path instances for equality.
void OpenExternal()
Opens the folder externally, for example File Explorer (Windows) or Finder (Mac).
Boolean Exists
Returns true if the path exists in the AssetDatabase.
static Boolean FileExists([NotNull] Path path)
Tests if the given file exists in the file system.
String FullPath
Returns the full (absolute) path with forward slashes as separators.
static Path ToMeta([NotNull] Path path)
Returns the .meta file path for an asset path.
Represents a relative path to an asset file or folder, typically under 'Assets' or 'Packages'.