|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using TMPro; |
| 5 | +using Unity.Barracuda; |
| 6 | +using Unity.MLAgents; |
| 7 | +using Unity.MLAgents.Policies; |
| 8 | +using UnityEditor; |
| 9 | +#if UNITY_EDITOR |
| 10 | +using UnityEditor.Recorder; |
| 11 | +#endif |
| 12 | +using UnityEngine; |
| 13 | + |
| 14 | +/** |
| 15 | + * Usage Notes: |
| 16 | + * |
| 17 | + * Add onnx models to the list and they will be played sequentially. |
| 18 | + * Create a recorder and a video of the sequence will be captured automatically if "Auto Record" is selected. |
| 19 | + * Recording only works in the Editor (not in standalone build) |
| 20 | + * Create a TextMeshPro Text GameObject and attach it to have the number of training steps of the current model shown. |
| 21 | + * To manually control transition between models choose a very large time, or "Pause" the system, |
| 22 | + * then use "Force Next" to advance. |
| 23 | + * "Reset" will start the sequence from the beginning again, use "Start" to proceed after resetting. |
| 24 | + * "Time Scale Override" can be set, "Seconds Between Switches" will decrease proportionally if you increase this |
| 25 | + * (i.e it represents simulated seconds between switches, not real time) |
| 26 | + */ |
| 27 | + |
| 28 | + |
| 29 | +public class ModelCarousel : MonoBehaviour |
| 30 | +{ |
| 31 | + public bool m_Start = true; |
| 32 | + public bool m_Reset = false; |
| 33 | + public bool m_Pause = true; |
| 34 | + public bool m_ForceNext = false; |
| 35 | + public bool m_Loop = false; |
| 36 | + public bool m_AutoRecord = true; |
| 37 | + public bool m_ResetAgentOnModelChange = false; |
| 38 | + public int m_SecondsBetweenSwitches = 10; |
| 39 | + public float m_TimeScaleOverride = 0.0f; |
| 40 | + public List<NNModel> m_Models = new List<NNModel>(); |
| 41 | + public bool m_ShowStepNumber = true; |
| 42 | + public int m_StepNumberRounding = 10000; |
| 43 | + |
| 44 | + private int m_StepsSinceLastSwitch = 0; |
| 45 | + private int m_CurrentModelIndex = 0; |
| 46 | + private int m_CurrentlySetModelIndex = -1; |
| 47 | + |
| 48 | + private NNModel m_OriginalModel = null; |
| 49 | + |
| 50 | + private int k_FixedUpdatePerSecond; |
| 51 | + |
| 52 | + // The attached Agent |
| 53 | + Agent m_Agent; |
| 54 | + |
| 55 | + public TextMeshProUGUI textMeshComponent; |
| 56 | + |
| 57 | +#if UNITY_EDITOR |
| 58 | + private RecorderWindow GetRecorderWindow() |
| 59 | + { |
| 60 | + return (RecorderWindow)EditorWindow.GetWindow(typeof(RecorderWindow)); |
| 61 | + } |
| 62 | +#endif |
| 63 | + |
| 64 | + private void Reset() |
| 65 | + { |
| 66 | + m_Reset = false; |
| 67 | + m_StepsSinceLastSwitch = 0; |
| 68 | + m_CurrentModelIndex = 0; |
| 69 | + m_Agent.SetModel(m_OriginalModel.name, m_OriginalModel); |
| 70 | + textMeshComponent?.SetText("Ready to Start"); |
| 71 | + } |
| 72 | + |
| 73 | + private void OnEnable() |
| 74 | + { |
| 75 | + m_Agent = GetComponent<Agent>(); |
| 76 | + m_OriginalModel = m_Agent.GetComponent<BehaviorParameters>().Model; |
| 77 | + |
| 78 | + Reset(); |
| 79 | + |
| 80 | + k_FixedUpdatePerSecond = (int)(1.0f / Time.fixedDeltaTime); |
| 81 | + |
| 82 | + if (m_TimeScaleOverride > 0.0f) |
| 83 | + { |
| 84 | + Time.timeScale = m_TimeScaleOverride; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void StartRecording() |
| 89 | + { |
| 90 | +#if UNITY_EDITOR |
| 91 | + if (!m_AutoRecord) |
| 92 | + return; |
| 93 | + |
| 94 | + Debug.Log("Starting Recording"); |
| 95 | + RecorderWindow recorderWindow = GetRecorderWindow(); |
| 96 | + if (!recorderWindow.IsRecording()) |
| 97 | + recorderWindow.StartRecording(); |
| 98 | +#endif |
| 99 | + } |
| 100 | + |
| 101 | + void StopRecording() |
| 102 | + { |
| 103 | +#if UNITY_EDITOR |
| 104 | + if (!m_AutoRecord) |
| 105 | + return; |
| 106 | + |
| 107 | + Debug.Log("Stopping Recording"); |
| 108 | + RecorderWindow recorderWindow = GetRecorderWindow(); |
| 109 | + if (recorderWindow.IsRecording()) |
| 110 | + recorderWindow.StopRecording(); |
| 111 | +#endif |
| 112 | + } |
| 113 | + |
| 114 | + void UpdateStepNumberText() |
| 115 | + { |
| 116 | + if (!m_ShowStepNumber) |
| 117 | + return; |
| 118 | + |
| 119 | + var result = Regex.Match(m_Models[m_CurrentModelIndex].name, @".*-(\d+)$"); |
| 120 | + |
| 121 | + string newText = ""; |
| 122 | + if (result.Success && result.Groups.Count > 0) |
| 123 | + { |
| 124 | + var steps = Int32.Parse(result.Groups[1].Captures[0].Value); |
| 125 | + |
| 126 | + int round = m_StepNumberRounding; |
| 127 | + steps += round / 2; |
| 128 | + steps /= round; |
| 129 | + steps *= round; |
| 130 | + |
| 131 | + newText = $"After {steps:n0} steps"; |
| 132 | + } |
| 133 | + |
| 134 | + textMeshComponent?.SetText(newText); |
| 135 | + } |
| 136 | + |
| 137 | + void SetModel() |
| 138 | + { |
| 139 | + if (m_CurrentModelIndex < 0 || m_CurrentModelIndex >= m_Models.Count) |
| 140 | + return; |
| 141 | + |
| 142 | + m_Agent.SetModel(m_Models[m_CurrentModelIndex].name, m_Models[m_CurrentModelIndex]); |
| 143 | + m_CurrentlySetModelIndex = m_CurrentModelIndex; |
| 144 | + |
| 145 | + UpdateStepNumberText(); |
| 146 | + |
| 147 | + if (m_ResetAgentOnModelChange) |
| 148 | + m_Agent.EndEpisode(); |
| 149 | + } |
| 150 | + |
| 151 | + void FixedUpdate() |
| 152 | + { |
| 153 | + if (m_Start) |
| 154 | + { |
| 155 | + m_Start = false; |
| 156 | + m_Pause = false; |
| 157 | + StartRecording(); |
| 158 | + } |
| 159 | + |
| 160 | + if (m_Reset) |
| 161 | + { |
| 162 | + StopRecording(); |
| 163 | + Reset(); |
| 164 | + m_Pause = true; |
| 165 | + m_Start = false; |
| 166 | + } |
| 167 | + |
| 168 | + if (m_Pause && !m_ForceNext) |
| 169 | + return; |
| 170 | + |
| 171 | + if (m_CurrentlySetModelIndex != m_CurrentModelIndex) |
| 172 | + { |
| 173 | + SetModel(); |
| 174 | + } |
| 175 | + |
| 176 | + m_StepsSinceLastSwitch++; |
| 177 | + |
| 178 | + if (m_StepsSinceLastSwitch >= m_SecondsBetweenSwitches * k_FixedUpdatePerSecond || m_ForceNext) |
| 179 | + { |
| 180 | + m_ForceNext = false; |
| 181 | + m_StepsSinceLastSwitch = 0; |
| 182 | + m_CurrentModelIndex++; |
| 183 | + |
| 184 | + if (m_CurrentModelIndex == m_Models.Count) |
| 185 | + { |
| 186 | + if (m_Loop) |
| 187 | + { |
| 188 | + m_CurrentModelIndex = 0; |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + Application.Quit(0); |
| 193 | +#if UNITY_EDITOR |
| 194 | + EditorApplication.isPlaying = false; |
| 195 | +#endif |
| 196 | + return; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + SetModel(); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments