CodeSmile AssetDatabase 1.9
Unity's AssetDatabase in enjoyable, consistent, concise, convenient, comprehensible, safe, documented form.
Loading...
Searching...
No Matches
Asset.Path.cs
1// Copyright (C) 2021-2023 Steffen Itterheim
2// Refer to included LICENSE file for terms and conditions.
3
4using System;
5using System.Diagnostics.CodeAnalysis;
6using UnityEditor;
7using UnityEngine;
8using Object = UnityEngine.Object;
9
10namespace CodeSmileEditor
11{
12 public sealed partial class Asset
13 {
24 public partial class Path : IEquatable<Path>, IEquatable<String>
25 {
26 private const String DefaultExtension = "asset";
27
28 private String m_RelativePath = String.Empty;
29
39 public GUID Guid => GetGuid(this, AssetPathToGUIDOptions.OnlyExistingAssets);
40
57 public Boolean Exists
58 {
59 get
60 {
61#if UNITY_2023_2_OR_NEWER
62 return AssetDatabase.AssetPathExists(m_RelativePath);
63#else
64 return AssetDatabase.AssetPathToGUID(m_RelativePath, AssetPathToGUIDOptions.OnlyExistingAssets).Length > 0;
65#endif
66 }
67 }
68
84 public Boolean ExistsInFileSystem => FileExists(this) || FolderExists(this);
85
95 [ExcludeFromCodeCoverage] // simple relay
96 public Path MetaPath => ToMeta(this);
97
107 [ExcludeFromCodeCoverage] // simple relay
108 public Path AssetPath => FromMeta(this);
109
118 public String Extension => System.IO.Path.GetExtension(m_RelativePath);
119
127 [ExcludeFromCodeCoverage] // simple relay
128 public String FileName => System.IO.Path.GetFileName(m_RelativePath);
129
137 [ExcludeFromCodeCoverage] // simple relay
138 public String FileNameWithoutExtension => System.IO.Path.GetFileNameWithoutExtension(m_RelativePath);
139
146 public String FullPath => System.IO.Path.GetFullPath(m_RelativePath).ToForwardSlashes();
147
160 [ExcludeFromCodeCoverage] // simple relay
161 public String[] SubFolders => GetSubFolders(this);
162
173 {
174 get
175 {
176 var dirName = System.IO.Path.GetDirectoryName(m_RelativePath);
177 return String.IsNullOrEmpty(dirName) ? null : dirName;
178 }
179 }
180
193
194 [ExcludeFromCodeCoverage] private Path() {} // disallowed parameterless ctor
195
223 public Path([NotNull] String fullOrRelativePath)
224 {
225 ThrowIf.NullOrWhitespace(fullOrRelativePath, nameof(fullOrRelativePath));
226 m_RelativePath = ToRelative(fullOrRelativePath.ToForwardSlashes());
227 }
228
251 public Path([NotNull] String folderPath, [NotNull] String fileName, [NotNull] String extension = DefaultExtension)
252 {
253 ThrowIf.NullOrWhitespace(folderPath, nameof(folderPath));
254 ThrowIf.NullOrWhitespace(fileName, nameof(fileName));
255 ThrowIf.NullOrWhitespace(extension, nameof(extension));
256 ThrowIf.ContainsPathSeparators(fileName, nameof(fileName));
257
258 var relativeDir = ToRelative(folderPath.ToForwardSlashes());
259 m_RelativePath = $"{relativeDir}/{fileName}.{extension.TrimStart('.').ToLower()}";
260 }
261
270 public Path([NotNull] Object asset)
271 {
272 ThrowIf.ArgumentIsNull(asset, nameof(asset));
273 ThrowIf.NotInDatabase(asset);
274
275 m_RelativePath = Get(asset);
276 }
277
284 public Boolean Equals(Path other)
285 {
286 if (ReferenceEquals(other, null))
287 return false;
288 if (ReferenceEquals(this, other))
289 return true;
290
291 return m_RelativePath.Equals(other.m_RelativePath);
292 }
293
301 public Boolean Equals(String other) => m_RelativePath.Equals(new Path(other).m_RelativePath);
302
308 public static implicit operator String(Path path) => path != null ? path.m_RelativePath : null;
309
315 public static implicit operator Path(String path) => path != null ? new Path(path) : null;
316
323 public static Boolean operator ==(Path path1, Path path2)
324 {
325 if (ReferenceEquals(path1, path2))
326 return true;
327 if (ReferenceEquals(path1, null))
328 return false;
329 if (ReferenceEquals(path2, null))
330 return false;
331
332 return path1.Equals(path2);
333 }
334
341 public static Boolean operator !=(Path path1, Path path2) => !(path1 == path2);
342
349 public static Boolean operator ==(Path path1, System.Object other) =>
350 other is String str ? path1.Equals(str) : path1.Equals(other as Path);
351
358 public static Boolean operator !=(Path path1, System.Object other) => !(path1 == other);
359
366 public static Boolean operator ==(System.Object other, Path path) => path == other;
367
374 public static Boolean operator !=(System.Object other, Path path) => !(path == other);
375
382 [ExcludeFromCodeCoverage] // cannot be tested
383 public void OpenExternal() => Application.OpenURL(System.IO.Path.GetFullPath(FolderPath));
384
391 public void Rename(String newFileOrFolderName)
392 {
393 if (String.IsNullOrEmpty(newFileOrFolderName) == false)
394 m_RelativePath = $"{FolderPath}/{System.IO.Path.GetFileName(newFileOrFolderName)}";
395 }
396
412 public GUID CreateFolders() => CreateFolders(this);
413
419 public override String ToString() => m_RelativePath;
420
430 public override Boolean Equals(System.Object obj)
431 {
432 if (obj is Path path)
433 return Equals(path);
434 if (obj is String str)
435 return Equals(str);
436
437 return false;
438 }
439
444 public override Int32 GetHashCode() => m_RelativePath.GetHashCode();
445 }
446 }
447}
Path MetaPath
Returns the path to the .meta file if the path is an asset file or folder.
Definition Asset.Path.cs:96
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.
Definition Asset.Path.cs:84
GUID Guid
Returns the GUID for the path.
Definition Asset.Path.cs:39
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.
Definition Asset.Path.cs:58
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'.
Definition Asset.Path.cs:25