-
-
Save EntranceJew/f329f1c6a0c35ac51763455f76b5eb95 to your computer and use it in GitHub Desktop.
please god let me just use datetimes instead of unix timestamps
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
using System; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
using UnityEngine; | |
// we have to use UDateTime instead of DateTime on our classes | |
// we still typically need to either cast this to a DateTime or read the DateTime field directly | |
[System.Serializable] | |
public class UDateTime : ISerializationCallbackReceiver { | |
[HideInInspector] public DateTime dateTime; | |
// if you don't want to use the PropertyDrawer then remove HideInInspector here | |
[HideInInspector] [SerializeField] private string _dateTime; | |
public static implicit operator DateTime(UDateTime udt) { | |
return (udt.dateTime); | |
} | |
public static implicit operator UDateTime(DateTime dt) { | |
return new UDateTime() {dateTime = dt}; | |
} | |
public void OnAfterDeserialize() { | |
DateTime.TryParse(_dateTime, out dateTime); | |
} | |
public void OnBeforeSerialize() { | |
_dateTime = dateTime.ToString(); | |
} | |
} | |
// if we implement this PropertyDrawer then we keep the label next to the text field | |
#if UNITY_EDITOR | |
[CustomPropertyDrawer(typeof(UDateTime))] | |
public class UDateTimeDrawer : PropertyDrawer { | |
// Draw the property inside the given rect | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { | |
// Using BeginProperty / EndProperty on the parent property means that | |
// prefab override logic works on the entire property. | |
EditorGUI.BeginProperty(position, label, property); | |
// Draw label | |
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); | |
// Don't make child fields be indented | |
var indent = EditorGUI.indentLevel; | |
EditorGUI.indentLevel = 0; | |
// Calculate rects | |
Rect amountRect = new Rect(position.x, position.y, position.width, position.height); | |
// Draw fields - passs GUIContent.none to each so they are drawn without labels | |
EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("_dateTime"), GUIContent.none); | |
// Set indent back to what it was | |
EditorGUI.indentLevel = indent; | |
EditorGUI.EndProperty(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment