TimekeeperEditor.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Chronos
  6. {
  7. [CustomEditor(typeof(Timekeeper))]
  8. public class TimekeeperEditor : Editor
  9. {
  10. protected SerializedProperty debug;
  11. protected SerializedProperty maxParticleLoops;
  12. public virtual void OnEnable()
  13. {
  14. debug = serializedObject.FindProperty("_debug");
  15. maxParticleLoops = serializedObject.FindProperty("_maxParticleLoops");
  16. }
  17. public override void OnInspectorGUI()
  18. {
  19. serializedObject.Update();
  20. Timekeeper timekeeper = (Timekeeper)serializedObject.targetObject;
  21. EditorGUILayout.PropertyField(debug, new GUIContent("Debug Mode"));
  22. EditorGUILayout.PropertyField(maxParticleLoops, new GUIContent("Max Particle Loops"));
  23. EditorGUILayout.HelpBox("Add global clocks to this object to configure each clock individually.", MessageType.Info);
  24. string[] duplicates = timekeeper.GetComponents<GlobalClock>()
  25. .Select(gc => gc.key)
  26. .Where(k => !string.IsNullOrEmpty(k))
  27. .GroupBy(k => k)
  28. .Where(g => g.Count() > 1)
  29. .Select(y => y.Key)
  30. .ToArray();
  31. if (duplicates.Length > 0)
  32. {
  33. EditorGUILayout.HelpBox("The following global clocks have identical keys:\n" + string.Join("\n", duplicates.Select(d => " - " + d).ToArray()), MessageType.Error);
  34. }
  35. serializedObject.ApplyModifiedProperties();
  36. }
  37. [MenuItem("GameObject/Timekeeper", false, 12)]
  38. private static void MenuCommand(MenuCommand menuCommand)
  39. {
  40. if (GameObject.FindObjectOfType<Timekeeper>() != null)
  41. {
  42. EditorUtility.DisplayDialog("Chronos", "The scene already contains a timekeeper.", "OK");
  43. return;
  44. }
  45. GameObject go = new GameObject("Timekeeper");
  46. GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
  47. Timekeeper timekeeper = go.AddComponent<Timekeeper>();
  48. timekeeper.AddClock("Root");
  49. Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
  50. Selection.activeObject = go;
  51. }
  52. }
  53. }