Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
PausableManager.cs
Go to the documentation of this file.
1 using System;
3 using UnityEngine;
4 
5 namespace droid.Runtime.Managers {
9  [AddComponentMenu("Neodroid/Managers/NeodroidManager")]
11  #region Fields
12 
13  #if UNITY_EDITOR
14  [Header("Warning! Will block editor requiring a restart, if not terminated while receiving.",
17  order = 120)]
18  [SerializeField]
19  bool _allow_in_editor_blockage = false;
20  #endif
21 
22  #endregion
23 
24  #region UnityCallbacks
25 
28  protected new void Awake() {
29  base.Awake();
30  if (this.Configuration.SimulationType == SimulationType.Frame_dependent_) {
31  this.EarlyUpdateEvent += this.PauseSimulation;
32  this.UpdateEvent += this.MaybeResume;
33  } else if (this.Configuration.SimulationType == SimulationType.Physics_dependent_) {
34  #if UNITY_EDITOR
35  if (this._allow_in_editor_blockage) {
36  this.EarlyFixedUpdateEvent +=
37  this.Receive; // Receive blocks the main thread and therefore also the unity editor.
38  } else {
39  Debug.LogWarning("Physics dependent blocking in editor is not enabled and is therefore not receiving or sending anything.");
40  }
41  #else
42  this.EarlyFixedUpdateEvent += this.Receive;
43  #endif
44  }
45  }
46 
47  #endregion
48 
49  #region PrivateMethods
50 
53  void MaybeResume() {
54  if (this.Stepping) {
55  #if NEODROID_DEBUG
56  if (this.Debugging) {
57  Debug.Log("Resuming simulation because of stepping");
58  }
59  #endif
60 
61  this.ResumeSimulation(this.Configuration.TimeScale);
62  } else if (this.TestActuators) {
63  #if NEODROID_DEBUG
64  if (this.Debugging) {
65  Debug.Log("Resuming simulation because of TestActuators");
66  }
67  #endif
68 
69  this.ResumeSimulation(this.Configuration.TimeScale);
70  } else {
71  #if NEODROID_DEBUG
72  if (this.Debugging) {
73  Debug.Log("Not resuming simulation because of stepping");
74  }
75  #endif
76  }
77  }
78 
81  public bool IsSimulationPaused { get { return !(this.SimulationTimeScale > 0); } }
82 
85  void PauseSimulation() {
86  #if NEODROID_DEBUG
87  if (this.Debugging) {
88  Debug.Log("Pausing simulation");
89  }
90  #endif
91  this.SimulationTimeScale = 0;
92  }
93 
97  void ResumeSimulation(float simulation_time_scale) {
98  #if NEODROID_DEBUG
99  if (this.Debugging) {
100  Debug.Log("Resuming simulation");
101  }
102  #endif
103  this.SimulationTimeScale = simulation_time_scale > 0 ? simulation_time_scale : 1;
104  }
105 
106  void SetAnimationSpeeds(float speed) {
107  var animators = FindObjectsOfType<Animator>();
108  foreach (var animator in animators) {
109  animator.speed = speed;
110  }
111  }
112 
115  void Receive() {
116  var reaction = this._Message_Server.Receive(TimeSpan.Zero);
117  this.SetReactionsFromExternalSource(reaction);
118  }
119 
120  #endregion
121  }
122 }
SimulationType
Determines the discrete timesteps of the simulation environment.