GlobalClockDrawer.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Chronos;
  4. using Chronos.Controls.Editor;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityObject = UnityEngine.Object;
  8. [CustomPropertyDrawer(typeof(GlobalClockAttribute))]
  9. public class GlobalClockDrawer : PropertyDrawer
  10. {
  11. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  12. {
  13. EditorGUI.BeginProperty(position, label, property);
  14. List<PopupOption<string>> options = new List<PopupOption<string>>();
  15. if (UnityObject.FindObjectOfType<Timekeeper>() != null)
  16. {
  17. Timekeeper timekeeper = Timekeeper.instance;
  18. foreach (GlobalClock globalClock in timekeeper
  19. .GetComponents<GlobalClock>()
  20. .Where(gc => !string.IsNullOrEmpty(gc.key)))
  21. {
  22. options.Add(new PopupOption<string>(globalClock.key));
  23. }
  24. }
  25. PopupOption<string> selectedOption;
  26. if (options.Any(o => o.value == property.stringValue))
  27. {
  28. selectedOption = new PopupOption<string>(property.stringValue);
  29. }
  30. else if (!string.IsNullOrEmpty(property.stringValue))
  31. {
  32. selectedOption = new PopupOption<string>(property.stringValue, property.stringValue + " (Missing)");
  33. }
  34. else
  35. {
  36. selectedOption = null;
  37. }
  38. PopupOption<string> noneOption = new PopupOption<string>(null, "None");
  39. var currentProperty = property;
  40. position = EditorGUI.PrefixLabel(position, label);
  41. PopupGUI<string>.Render
  42. (
  43. position,
  44. gc => ChangeValue(currentProperty, gc),
  45. options,
  46. selectedOption,
  47. noneOption,
  48. property.hasMultipleDifferentValues
  49. );
  50. EditorGUI.EndProperty();
  51. }
  52. protected void ChangeValue(SerializedProperty property, string value)
  53. {
  54. // BUG: Multi-object editing and resetting the same property doesn't apply
  55. // That's probably because the "Modified" flag isn't triggered, even if one of the
  56. // objects has a different value.
  57. property.stringValue = value;
  58. property.serializedObject.ApplyModifiedProperties();
  59. }
  60. }