using System;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace AutoGroupGenerator
{
///
/// Represents an asset in the dependency graph via its GUID.
///
[Serializable]
public class AssetNode : IEquatable
{
#region Static Methods
///
/// Creates an from an asset path.
///
/// The project-relative asset path used to resolve a GUID.
/// The created node, or null if the path is invalid.
public static AssetNode FromAssetPath(string assetPath)
{
if (string.IsNullOrEmpty(assetPath))
{
return null;
}
var guidString = AssetDatabase.AssetPathToGUID(assetPath, AssetPathToGUIDOptions.OnlyExistingAssets);
return FromGuidString(guidString);
}
///
/// Creates an from a serialized string representation.
///
/// The serialized value containing a GUID string to parse.
/// The created node, or null if parsing fails.
public static AssetNode FromString(string value)
{
string[] parts = value.Split('|');
var guidString = parts[0];
return FromGuidString(guidString);
}
///
/// Creates an from a GUID string.
///
/// The GUID string representation used to create the node.
/// The created node, or null if parsing fails.
public static AssetNode FromGuidString(string guidString)
{
return GUID.TryParse(guidString, out var guid) ? new AssetNode(guid) : null;
}
#endregion
#region Fields
///
/// The GUID backing this node.
///
public readonly GUID Guid;
#endregion
#region Properties
///
/// Gets the asset path represented by this node.
///
public string AssetPath => AssetDatabase.GUIDToAssetPath(Guid);
///
/// Gets the asset file name without the directory path.
///
public string FileName => Path.GetFileName(AssetPath);
#endregion
#region Methods
///
/// Initializes a new instance of the class.
///
/// The GUID that uniquely identifies the asset in the project.
public AssetNode(GUID guid)
{
Guid = guid;
}
///
/// Returns the GUID string for this node.
///
/// The GUID string.
public override string ToString()
{
return Guid.ToString();
}
///
/// Determines whether another node is equal to this node.
///
/// The other node.
/// True when the GUIDs are equal.
public bool Equals(AssetNode other)
{
return other != null && Guid.Equals(other.Guid);
}
///
/// Determines whether another object is equal to this node.
///
/// The other object.
/// True when the object is an equal .
public override bool Equals(object obj)
{
return obj is AssetNode other && Equals(other);
}
///
/// Returns a hash code for this node.
///
/// The hash code.
public override int GetHashCode()
{
return Guid.GetHashCode();
}
#endregion
}
}