using System; using System.Collections.Generic; using Unity.Entities; using UnityEngine.Perception.Randomization.Parameters; using UnityEngine.Perception.Randomization.Scenarios; namespace UnityEngine.Perception.Randomization.Randomizers { /// /// Derive Randomizer to implement systems that randomize GameObjects and/or simulation properties. /// /// /// Known issue: /// https://issuetracker.unity3d.com/issues/serializereference-non-serialized-initialized-fields-lose-their-values-when-entering-play-mode /// [Serializable] public abstract class Randomizer { [SerializeField, HideInInspector] bool m_Enabled = true; [SerializeField, HideInInspector] internal bool collapsed; /// /// Enabled Randomizers are updated, disabled Randomizers are not. /// public bool enabled { get => m_Enabled; set { m_Enabled = value; if (value) OnEnable(); else OnDisable(); } } /// /// Returns the scenario containing this Randomizer /// public ScenarioBase scenario => ScenarioBase.activeScenario; /// /// Retrieves the RandomizerTagManager of the scenario containing this Randomizer /// public RandomizerTagManager tagManager => RandomizerTagManager.singleton; internal IEnumerable parameters { get { var fields = GetType().GetFields(); foreach (var field in fields) { if (!field.IsPublic || !field.FieldType.IsSubclassOf(typeof(Parameter))) continue; var parameter = (Parameter)field.GetValue(this); if (parameter == null) { parameter = (Parameter)Activator.CreateInstance(field.FieldType); field.SetValue(this, parameter); } yield return parameter; } } } /// /// OnCreate is called when the Randomizer is added or loaded to a scenario /// [Obsolete("Method OnCreate has been deprecated. Use OnAwake instead (UnityUpgradable)", true)] protected virtual void OnCreate() => throw new NotSupportedException("OnCreate method has been deprecated"); /// /// OnAwake is called when the Randomizer is added or loaded to a scenario /// protected virtual void OnAwake() { } /// /// OnEnabled is called when the Randomizer becomes enabled and active /// protected virtual void OnEnable() { } /// /// OnDisable is called when the Randomizer becomes disabled /// protected virtual void OnDisable() { } /// /// OnScenarioStart is called on the frame the scenario begins iterating /// protected virtual void OnScenarioStart() { } /// /// OnScenarioComplete is called the after the entire Scenario has completed /// protected virtual void OnScenarioComplete() { } /// /// OnIterationStart is called at the start of a new Scenario iteration /// protected virtual void OnIterationStart() { } /// /// OnIterationEnd is called the after a Scenario iteration has completed /// protected virtual void OnIterationEnd() { } /// /// OnStartRunning is called on the first frame a Randomizer is enabled /// [Obsolete("Method OnStartRunning has been deprecated. Use OnEnabled instead (UnityUpgradable)", true)] protected virtual void OnStartRunning() => throw new NotSupportedException("OnStartRunning method has been deprecated"); /// /// OnStartRunning is called on the first frame a disabled Randomizer is updated /// [Obsolete("Method OnStopRunning has been deprecated. Use OnDisable instead (UnityUpgradable)", true)] protected virtual void OnStopRunning() => throw new NotSupportedException("OnStopRunning method has been deprecated"); /// /// OnUpdate is executed every frame for enabled Randomizers /// protected virtual void OnUpdate() { } #region InternalScenarioMethods internal void Awake() => OnAwake(); internal void ScenarioStart() => OnScenarioStart(); internal void ScenarioComplete() => OnScenarioComplete(); internal void IterationStart() => OnIterationStart(); internal void IterationEnd() => OnIterationEnd(); internal void Update() => OnUpdate(); #endregion } }