StudyManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. namespace SchoenLogger
  7. {
  8. public abstract class StudyManager<TCondition> : MonoBehaviour, IStudyManager where TCondition : Condition, new()
  9. {
  10. [Header("Study Settings")]
  11. public int ParticipantId = -1;
  12. [Header("Study Events")]
  13. public UnityEvent<TCondition, int> ChangeCondition;
  14. public UnityEvent<TCondition, int> StartCondition;
  15. [Header("Misc")]
  16. [SerializeField]
  17. protected bool EnableConsoleLogging = false;
  18. protected TCondition[] Conditions;
  19. protected int CurrentConditionIndex = -1;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. CreateConditions(ref Conditions);
  24. }
  25. /// <summary>
  26. /// Creates all possible Conditions
  27. /// </summary>
  28. /// <param name="conditions"></param>
  29. protected abstract void CreateConditions(ref TCondition[] conditions);
  30. public void RaiseNextCondition()
  31. {
  32. CurrentConditionIndex++;
  33. if (CurrentConditionIndex >= Conditions.Length)
  34. return;
  35. ChangeCondition?.Invoke(Conditions[CurrentConditionIndex], ParticipantId);
  36. if(EnableConsoleLogging)
  37. Debug.LogFormat("Changed Condition to {0}!", CurrentConditionIndex);
  38. }
  39. public void RaiseStartCondition()
  40. {
  41. StartCondition?.Invoke(Conditions[CurrentConditionIndex], ParticipantId);
  42. if(EnableConsoleLogging)
  43. Debug.LogFormat("Started Condition! {0}", Conditions[CurrentConditionIndex].ToCsv());
  44. }
  45. public string GetConditionCountString()
  46. {
  47. if (Conditions == null || !Application.isPlaying)
  48. return "Only available on play";
  49. return Conditions.Length.ToString();
  50. }
  51. public int GetCurrentConditionIndex()
  52. {
  53. return CurrentConditionIndex;
  54. }
  55. }
  56. public interface IStudyManager
  57. {
  58. void RaiseNextCondition();
  59. void RaiseStartCondition();
  60. string GetConditionCountString();
  61. int GetCurrentConditionIndex();
  62. }
  63. #if UNITY_EDITOR
  64. [CustomEditor(typeof(StudyManager<>), true)]
  65. public class StudyManagerEditor : Editor
  66. {
  67. public override void OnInspectorGUI()
  68. {
  69. DrawDefaultInspector();
  70. IStudyManager Target = (IStudyManager)target;
  71. //EditorGUILayout.Space(10);
  72. //EditorGUILayout.LabelField("Manage Conditions", EditorStyles.boldLabel);
  73. EditorGUILayout.LabelField("Defined Conditions: ", Target.GetConditionCountString());
  74. EditorGUILayout.LabelField("Current Condition: ", Target.GetCurrentConditionIndex().ToString());
  75. EditorGUILayout.Space(5);
  76. EditorGUILayout.LabelField("Controlls", EditorStyles.boldLabel);
  77. GUILayout.BeginHorizontal();
  78. if (GUILayout.Button("Setup next Condition"))
  79. {
  80. Target.RaiseNextCondition();
  81. }
  82. if (GUILayout.Button("Setup & start next Condition"))
  83. {
  84. Target.RaiseNextCondition();
  85. Target.RaiseStartCondition();
  86. }
  87. GUILayout.EndHorizontal();
  88. if (GUILayout.Button("StartCondition"))
  89. {
  90. Target.RaiseStartCondition();
  91. }
  92. }
  93. }
  94. #endif
  95. }