RecorderSettings.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. namespace UnityEditor.Recorder
  7. {
  8. /// <summary>
  9. /// Sets which source camera to use for recording (by some specific Recorders).
  10. /// </summary>
  11. [Flags]
  12. public enum ImageSource
  13. {
  14. /// <summary>
  15. /// Use the current active camera.
  16. /// </summary>
  17. ActiveCamera = 1,
  18. /// <summary>
  19. /// Use the main camera.
  20. /// </summary>
  21. MainCamera = 2,
  22. /// <summary>
  23. /// Use the first camera that matches a GameObject tag.
  24. /// </summary>
  25. TaggedCamera = 4
  26. }
  27. /// <summary>
  28. /// Sets which frame rate type to use during recording.
  29. /// </summary>
  30. public enum FrameRatePlayback
  31. {
  32. /// <summary>
  33. /// The frame rate doesn't vary during recording, even if the actual frame rate is lower or higher.
  34. /// </summary>
  35. Constant,
  36. /// <summary>
  37. /// Use the application's frame rate, which might vary during recording. This option is not supported by all Recorders.
  38. /// </summary>
  39. Variable,
  40. }
  41. /// <summary>
  42. /// Sets which time or frame interval to record.
  43. /// </summary>
  44. public enum RecordMode
  45. {
  46. /// <summary>
  47. /// Record every frame between when the recording is started and when it is stopped (either using the UI or through API methods).
  48. /// </summary>
  49. Manual,
  50. /// <summary>
  51. /// Record one single frame according to the specified frame number.
  52. /// </summary>
  53. SingleFrame,
  54. /// <summary>
  55. /// Record all frames within an interval of frames according to the specified Start and End frame numbers.
  56. /// </summary>
  57. FrameInterval,
  58. /// <summary>
  59. /// Record all frames within a time interval according to the specified Start time and End time.
  60. /// </summary>
  61. TimeInterval
  62. }
  63. /// <summary>
  64. /// Main base class for a Recorder settings.
  65. /// Each Recorder needs to have its corresponding settings properly configured.
  66. /// </summary>
  67. public abstract class RecorderSettings : ScriptableObject
  68. {
  69. private static string s_OutputFileErrorMessage = "Recorder output file cannot be empty";
  70. /// <summary>
  71. /// Stores the path this Recorder will use to generate the output file.
  72. /// It can be either an absolute or a relative path.
  73. /// The file extension is automatically added.
  74. /// Wildcards such as <c>DefaultWildcard.Time</c> are supported.
  75. /// <seealso cref="DefaultWildcard"/>
  76. /// </summary>
  77. public string OutputFile
  78. {
  79. get { return fileNameGenerator.ToPath(); }
  80. set
  81. {
  82. if (string.IsNullOrEmpty(value))
  83. throw new ArgumentException(s_OutputFileErrorMessage);
  84. fileNameGenerator.FromPath(value);
  85. }
  86. }
  87. /// <summary>
  88. /// Indicates if this Recorder is active when starting the recording. If false, the Recorder is ignored and generates no output.
  89. /// </summary>
  90. public bool Enabled
  91. {
  92. get { return enabled; }
  93. set { enabled = value; }
  94. }
  95. [SerializeField] private bool enabled = true;
  96. /// <summary>
  97. /// Stores the current Take number. Automatically incremented after each recording session.
  98. /// </summary>
  99. public int Take
  100. {
  101. get { return take; }
  102. set { take = value; }
  103. }
  104. [SerializeField] internal int take = 1;
  105. /// <summary>
  106. /// Stores the file extension used by this Recorder (without the dot).
  107. /// </summary>
  108. protected internal abstract string Extension { get; }
  109. [SerializeField] internal int captureEveryNthFrame = 1;
  110. [SerializeField] internal FileNameGenerator fileNameGenerator;
  111. public FileNameGenerator FileNameGenerator => fileNameGenerator;
  112. public RecordMode RecordMode { get; set; }
  113. public FrameRatePlayback FrameRatePlayback { get; set; }
  114. public float FrameRate { get; set; }
  115. public int StartFrame { get; set; }
  116. public int EndFrame { get; set; }
  117. public float StartTime { get; set; }
  118. public float EndTime { get; set; }
  119. public bool CapFrameRate { get; set; }
  120. protected RecorderSettings()
  121. {
  122. fileNameGenerator = new FileNameGenerator(this)
  123. {
  124. Root = OutputPath.Root.Project,
  125. Leaf = "Recordings"
  126. };
  127. }
  128. /// <summary>
  129. /// Tests if the Recorder is correctly configured.
  130. /// </summary>
  131. /// <param name="errors">List of errors encountered.</param>
  132. /// <returns>True if there are no errors, False otherwise.</returns>
  133. protected internal virtual bool ValidityCheck(List<string> errors)
  134. {
  135. var ok = true;
  136. if (InputsSettings != null)
  137. {
  138. var inputErrors = new List<string>();
  139. var valid = InputsSettings.All(x => x.ValidityCheck(inputErrors));
  140. if (!valid)
  141. {
  142. errors.AddRange(inputErrors);
  143. ok = false;
  144. }
  145. }
  146. if (string.IsNullOrEmpty(fileNameGenerator.FileName))
  147. {
  148. errors.Add("Missing file name");
  149. ok = false;
  150. }
  151. if (Math.Abs(FrameRate) <= float.Epsilon)
  152. {
  153. ok = false;
  154. errors.Add("Invalid frame rate");
  155. }
  156. if (captureEveryNthFrame <= 0)
  157. {
  158. ok = false;
  159. errors.Add("Invalid frame skip value");
  160. }
  161. if (!IsPlatformSupported)
  162. {
  163. errors.Add("Current platform is not supported");
  164. ok = false;
  165. }
  166. return ok;
  167. }
  168. /// <summary>
  169. /// Indicates if the current platform is supported (True) or not (False).
  170. /// </summary>
  171. public virtual bool IsPlatformSupported
  172. {
  173. get { return true; }
  174. }
  175. /// <summary>
  176. /// Stores the list of Input settings required by this Recorder.
  177. /// </summary>
  178. public abstract IEnumerable<RecorderInputSettings> InputsSettings { get; }
  179. /// <summary>
  180. /// This method is automatically called each time a Recorder Settings group has changed in the Recorder Window and before starting recording.
  181. /// </summary>
  182. internal virtual void SelfAdjustSettings()
  183. {
  184. }
  185. /// <summary>
  186. /// Override this method if any post treatement needs to be done after this Recorder is duplicated in the Recorder Window.
  187. /// </summary>
  188. public virtual void OnAfterDuplicate()
  189. {
  190. }
  191. protected internal virtual bool HasErrors()
  192. {
  193. return false;
  194. }
  195. internal virtual bool HasWarnings()
  196. {
  197. return !ValidityCheck(new List<string>());
  198. }
  199. }
  200. }