public static void Execute(System.Action <T> functor) { if (_executing) { throw new System.InvalidOperationException("Can not globally broadcast a message currently executing."); } if (_receivers == null || _receivers.Count == 0) { return; } _executing = true; try { var e = _receivers.GetEnumerator(); while (e.MoveNext()) { if (e.Current is UnityEngine.Object && (e.Current as UnityEngine.Object) == null) { //skip & remove destroyed objects Remove(e.Current); } try { functor(e.Current); } catch (System.Exception ex) { Debug.LogException(ex); } } } finally { _executing = false; if (_toRemove != null) { var e = _toRemove.GetEnumerator(); while (e.MoveNext()) { _receivers.Remove(e.Current); } _toRemove.Dispose(); _toRemove = null; } if (_toAdd != null) { var e = _toAdd.GetEnumerator(); while (e.MoveNext()) { _receivers.Add(e.Current); } _toAdd.Dispose(); _toAdd = null; } } }
void IUpdateable.Update() { if (_currentToken == null) { GameLoopEntry.UpdatePump.Remove(this); return; } TempHashSet <AudibleSensor> activeSet = null; if (_activeSensors.Count > 0) { activeSet = TempCollection.GetSet(_activeSensors); } var pos = this.transform.position; float d, r; var e = AudibleSensor.Pool.GetEnumerator(); while (e.MoveNext()) { var sensor = e.Current; if (_activeSensors.Contains(sensor) || sensor.Ignores(this)) { continue; } d = (sensor.transform.position - pos).sqrMagnitude; r = (sensor.Range + _range); if (d < r * r) { _activeSensors.Add(sensor); sensor.SignalEnterSiren(this); } } if (activeSet != null) { var e2 = activeSet.GetEnumerator(); while (e2.MoveNext()) { var sensor = e.Current; d = (sensor.transform.position - pos).sqrMagnitude; r = (sensor.Range + _range); if (d < r * r) { sensor.SignalSirenStay(this); } else { _activeSensors.Remove(sensor); sensor.SignalExitSiren(this); } } activeSet.Dispose(); } }