Files
Cielonos/Packages/com.lunawolfstudios.scriptablesheets/Editor/Modules/Shared/Utilities/EnumUtility.cs
SoulliesOfficial 7ee2894a63 整合SLSUtilities
2026-01-17 11:35:49 -05:00

21 lines
627 B
C#

using System;
using System.Linq;
namespace LunaWolfStudiosEditor.ScriptableSheets.Shared
{
public static class EnumUtility
{
/// <summary>
/// Returns the first set flag from a valid flagged enum. Returns the default value if no flags are set.
/// </summary>
public static T FirstFlagOrDefault<T>(this T flaggedEnum) where T : Enum
{
if (!Attribute.IsDefined(typeof(T), typeof(FlagsAttribute)))
{
throw new ArgumentException($"{typeof(T).FullName} is not a flagged enum.");
}
return Enum.GetValues(typeof(T)).Cast<T>().FirstOrDefault(f => (int) (object) f != 0 && flaggedEnum.HasFlag(f));
}
}
}