UnityDateTime.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.ComponentModel;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. using UnityEditor.TerrainTools;
  6. #endif
  7. using UnityEngine;
  8. namespace TimelineViewer
  9. {
  10. // we have to use UDateTime instead of DateTime on our classes
  11. // we still typically need to either cast this to a DateTime or read the DateTime field directly
  12. [System.Serializable]
  13. public class UnityDateTime : ISerializationCallbackReceiver
  14. {
  15. [HideInInspector] public DateTime m_DateTime;
  16. // if you don't want to use the PropertyDrawer then remove HideInInspector here
  17. [HideInInspector][SerializeField] private string _dateTime;
  18. public static implicit operator DateTime(UnityDateTime udt)
  19. {
  20. return (udt.m_DateTime);
  21. }
  22. public static implicit operator UnityDateTime(DateTime dt)
  23. {
  24. return new UnityDateTime() { m_DateTime = dt };
  25. }
  26. public void OnAfterDeserialize()
  27. {
  28. DateTime.TryParse(_dateTime, out m_DateTime);
  29. }
  30. public void OnBeforeSerialize()
  31. {
  32. _dateTime = m_DateTime.ToString();
  33. }
  34. }
  35. // if we implement this PropertyDrawer then we keep the label next to the text field
  36. #if UNITY_EDITOR
  37. [CustomPropertyDrawer(typeof(UnityDateTime))]
  38. public class UnityDateTimeDrawer : PropertyDrawer
  39. {
  40. // Draw the property inside the given rect
  41. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  42. {
  43. // Using BeginProperty / EndProperty on the parent property means that
  44. // prefab override logic works on the entire property.
  45. EditorGUI.BeginProperty(position, label, property);
  46. // Draw label
  47. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  48. // Don't make child fields be indented
  49. var indent = EditorGUI.indentLevel;
  50. EditorGUI.indentLevel = 0;
  51. // Calculate rects
  52. Rect amountRect = new Rect(position.x, position.y, position.width, position.height);
  53. //GUI.enabled = false;
  54. // Draw fields - passs GUIContent.none to each so they are drawn without labels
  55. //EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("_dateTime"), GUIContent.none);
  56. //EditorGUI.LabelField(amountRect, label.text, property.FindPropertyRelative("_dateTime").stringValue);
  57. //GUI.enabled = true;
  58. var content = new GUIContent(property.FindPropertyRelative("_dateTime").stringValue);
  59. if (EditorGUI.DropdownButton(amountRect, content, FocusType.Keyboard))
  60. {
  61. if (UiCalendarWindow.IsDisplayed)
  62. {
  63. //WarningPopup.Init("Only one instance of UICalendar allowed", "Ok");
  64. Debug.Log("Only one instance of UICalendar allowed");
  65. return;
  66. }
  67. UiCalendarWindow.Init();
  68. UiCalendarWindow.OnDateSelected.AddListener((selecteDate) =>
  69. {
  70. property.FindPropertyRelative("_dateTime").stringValue = selecteDate.ToString();
  71. property.serializedObject.ApplyModifiedProperties();
  72. property.serializedObject.Update();
  73. });
  74. }
  75. // Set indent back to what it was
  76. EditorGUI.indentLevel = indent;
  77. EditorGUI.EndProperty();
  78. }
  79. }
  80. #endif
  81. }