File size: 5,455 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TMPro;
using Unity.Barracuda;
using Unity.MLAgents;
using Unity.MLAgents.Policies;
using UnityEditor;
#if UNITY_EDITOR
using UnityEditor.Recorder;
#endif
using UnityEngine;

/**
 *  Usage Notes:
 *
 *  Add onnx models to the list and they will be played sequentially.
 *  Create a recorder and a video of the sequence will be captured automatically if "Auto Record" is selected.
 *  Recording only works in the Editor (not in standalone build)
 *  Create a TextMeshPro Text GameObject and attach it to have the number of training steps of the current model shown.
 *  To manually control transition between models choose a very large time, or "Pause" the system,
 *  then use "Force Next" to advance.
 *  "Reset" will start the sequence from the beginning again, use "Start" to proceed after resetting.
 *  "Time Scale Override" can be set, "Seconds Between Switches" will decrease proportionally if you increase this
 *  (i.e it represents simulated seconds between switches, not real time)
 */


public class ModelCarousel : MonoBehaviour
{
    public bool m_Start = true;
    public bool m_Reset = false;
    public bool m_Pause = true;
    public bool m_ForceNext = false;
    public bool m_Loop = false;
    public bool m_AutoRecord = true;
    public bool m_ResetAgentOnModelChange = false;
    public int m_SecondsBetweenSwitches = 10;
    public float m_TimeScaleOverride = 0.0f;
    public List<NNModel> m_Models = new List<NNModel>();
    public bool m_ShowStepNumber = true;
    public int m_StepNumberRounding = 10000;

    private int m_StepsSinceLastSwitch = 0;
    private int m_CurrentModelIndex = 0;
    private int m_CurrentlySetModelIndex = -1;

    private NNModel m_OriginalModel = null;

    private int k_FixedUpdatePerSecond;

    // The attached Agent
    Agent m_Agent;

    public TextMeshProUGUI textMeshComponent;

#if UNITY_EDITOR
    private RecorderWindow GetRecorderWindow()
    {
        return (RecorderWindow)EditorWindow.GetWindow(typeof(RecorderWindow));
    }
#endif

    private void Reset()
    {
        m_Reset = false;
        m_StepsSinceLastSwitch = 0;
        m_CurrentModelIndex = 0;
        m_Agent.SetModel(m_OriginalModel.name, m_OriginalModel);
        textMeshComponent?.SetText("Ready to Start");
    }

    private void OnEnable()
    {
        m_Agent = GetComponent<Agent>();
        m_OriginalModel = m_Agent.GetComponent<BehaviorParameters>().Model;

        Reset();

        k_FixedUpdatePerSecond = (int)(1.0f / Time.fixedDeltaTime);

        if (m_TimeScaleOverride > 0.0f)
        {
            Time.timeScale = m_TimeScaleOverride;
        }
    }

    void StartRecording()
    {
#if UNITY_EDITOR
        if (!m_AutoRecord)
            return;

        Debug.Log("Starting Recording");
        RecorderWindow recorderWindow = GetRecorderWindow();
        if (!recorderWindow.IsRecording())
            recorderWindow.StartRecording();
#endif
    }

    void StopRecording()
    {
#if UNITY_EDITOR
        if (!m_AutoRecord)
            return;

        Debug.Log("Stopping Recording");
        RecorderWindow recorderWindow = GetRecorderWindow();
        if (recorderWindow.IsRecording())
            recorderWindow.StopRecording();
#endif
    }

    void UpdateStepNumberText()
    {
        if (!m_ShowStepNumber)
            return;

        var result = Regex.Match(m_Models[m_CurrentModelIndex].name, @".*-(\d+)$");

        string newText = "";
        if (result.Success && result.Groups.Count > 0)
        {
            var steps = Int32.Parse(result.Groups[1].Captures[0].Value);

            int round = m_StepNumberRounding;
            steps += round / 2;
            steps /= round;
            steps *= round;

            newText = $"After {steps:n0} steps";
        }

        textMeshComponent?.SetText(newText);
    }

    void SetModel()
    {
        if (m_CurrentModelIndex < 0 || m_CurrentModelIndex >= m_Models.Count)
            return;

        m_Agent.SetModel(m_Models[m_CurrentModelIndex].name, m_Models[m_CurrentModelIndex]);
        m_CurrentlySetModelIndex = m_CurrentModelIndex;

        UpdateStepNumberText();

        if (m_ResetAgentOnModelChange)
            m_Agent.EndEpisode();
    }

    void FixedUpdate()
    {
        if (m_Start)
        {
            m_Start = false;
            m_Pause = false;
            StartRecording();
        }

        if (m_Reset)
        {
            StopRecording();
            Reset();
            m_Pause = true;
            m_Start = false;
        }

        if (m_Pause && !m_ForceNext)
            return;

        if (m_CurrentlySetModelIndex != m_CurrentModelIndex)
        {
            SetModel();
        }

        m_StepsSinceLastSwitch++;

        if (m_StepsSinceLastSwitch >= m_SecondsBetweenSwitches * k_FixedUpdatePerSecond || m_ForceNext)
        {
            m_ForceNext = false;
            m_StepsSinceLastSwitch = 0;
            m_CurrentModelIndex++;

            if (m_CurrentModelIndex == m_Models.Count)
            {
                if (m_Loop)
                {
                    m_CurrentModelIndex = 0;
                }
                else
                {
                    Application.Quit(0);
#if UNITY_EDITOR
                    EditorApplication.isPlaying = false;
#endif
                    return;
                }
            }

            SetModel();
        }
    }
}