Created
September 12, 2022 01:43
-
-
Save cathei/30c0ea58bfe5f8e8f8c02901b3ffdaff to your computer and use it in GitHub Desktop.
Unity String Enum Serialization (Supports from Unity 2020.1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// StringEnum, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022 | |
using System; | |
using UnityEngine; | |
namespace StringEnum | |
{ | |
[Serializable] | |
public struct StringEnum<T> : IEquatable<T>, IEquatable<StringEnum<T>>, ISerializationCallbackReceiver | |
where T : struct, Enum | |
{ | |
public StringEnum(T value) : this() | |
{ | |
_value = value; | |
} | |
[SerializeField] | |
private string _stringValue; | |
private T _value; | |
public T Value | |
{ | |
get { return _value; } | |
set { _value = value; } | |
} | |
public void OnBeforeSerialize() | |
{ | |
_stringValue = _value.ToString(); | |
} | |
public void OnAfterDeserialize() | |
{ | |
if (Enum.TryParse(_stringValue, out T value)) | |
_value = value; | |
} | |
public static implicit operator T(StringEnum<T> obj) | |
{ | |
return obj._value; | |
} | |
public static implicit operator StringEnum<T>(T value) | |
{ | |
return new StringEnum<T>(value); | |
} | |
public bool Equals(T other) | |
{ | |
return _value.Equals(other); | |
} | |
public bool Equals(StringEnum<T> other) | |
{ | |
return _value.Equals(other._value); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (obj is StringEnum<T> other) | |
return Equals(other); | |
if (obj is T otherEnum) | |
return Equals(otherEnum); | |
return false; | |
} | |
public override int GetHashCode() | |
{ | |
return _value.GetHashCode(); | |
} | |
public override string ToString() | |
{ | |
return _value.ToString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// StringEnum, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022 | |
using System; | |
using UnityEngine; | |
using UnityEditor; | |
namespace StringEnum.Editor | |
{ | |
[CustomPropertyDrawer(typeof(StringEnum<>), true)] | |
public class StringEnumPropertyDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
EditorGUI.BeginChangeCheck(); | |
var stringProp = property.FindPropertyRelative("_stringValue"); | |
var enumType = fieldInfo.FieldType.GenericTypeArguments[0]; | |
Enum enumValue = default; | |
try | |
{ | |
enumValue = (Enum)Enum.Parse(enumType, stringProp.stringValue); | |
} | |
catch (ArgumentException) { } | |
if (enumType.IsDefined(typeof(FlagsAttribute), false)) | |
{ | |
enumValue = EditorGUI.EnumFlagsField(position, label, enumValue); | |
} | |
else | |
{ | |
enumValue = EditorGUI.EnumPopup(position, label, enumValue); | |
} | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
stringProp.stringValue = enumValue.ToString(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment