using UnityEngine; using System.Text.RegularExpressions; namespace MLAgents { /// /// Demonstration Recorder Component. /// [RequireComponent(typeof(Agent))] public class DemonstrationRecorder : MonoBehaviour { public bool record; public string demonstrationName; private Agent recordingAgent; private string filePath; private DemonstrationStore demoStore; /// /// Initializes Demonstration store. /// private void Start() { if (Application.isEditor && record) { recordingAgent = GetComponent(); demoStore = new DemonstrationStore(); demonstrationName = SanitizeName(demonstrationName); demoStore.Initialize( demonstrationName, recordingAgent.brain.brainParameters, recordingAgent.brain.name); Monitor.Log("Recording Demonstration of Agent: ", recordingAgent.name); } } /// /// Removes all characters except alphanumerics from demonstration name. /// public static string SanitizeName(string demoName) { var rgx = new Regex("[^a-zA-Z0-9 -]"); demoName = rgx.Replace(demoName, ""); return demoName; } /// /// Forwards AgentInfo to Demonstration Store. /// public void WriteExperience(AgentInfo info) { demoStore.Record(info); } /// /// Closes Demonstration store. /// private void OnApplicationQuit() { if (Application.isEditor && record) { demoStore.Close(); } } } }