Advertisement
netgrind

Unity Gif Capture

Nov 8th, 2014
2,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. //By Cale Bradbury - netgrind.net
  2. //Toss in Assets/Editor
  3. //Free for any use
  4. //use to slow down unity for capturing frames for external software (gifcam, etc, scrubs only)
  5. //export png sequence to assemble elsewhere (try imagemagick, photoshop)
  6. //protip - right under the game tab there is a drop down where you can pick ratios and set absolute size in pixels.
  7.  
  8. using UnityEngine;
  9. using System.Collections;
  10. using UnityEditor;
  11.  
  12. [ExecuteInEditMode]
  13. public class CaptureGifEditor : EditorWindow {
  14.  
  15.    
  16.     public int frames = 50;
  17.     public int frameDelay = 1;
  18.     public bool captureFrames = true;
  19.     public int captureUpscale = 1;
  20.     public string capturePath = "Folder/Name";
  21.     int i = -1;
  22.     int c=0;
  23.     private bool playing = true;
  24.     [MenuItem("Edit/Capture Gif %_g")]
  25.  
  26.     static void Init ()
  27.     {
  28.         CaptureGifEditor window = EditorWindow.GetWindow<CaptureGifEditor>();
  29.     }
  30.  
  31.     void Update()
  32.     {
  33.         c--;
  34.         if(c<=0){
  35.             c = frameDelay;
  36.             if(i>=0){
  37.                 Debug.Log("Captured frame "+(frames-i)+"/"+frames);
  38.                 EditorApplication.Step();
  39.                 if(captureFrames){
  40.                     string s = ""+(frames-i);
  41.                     while(s.Length<6)s = "0"+s;
  42.                     Application.CaptureScreenshot(capturePath+""+s+".png",captureUpscale);
  43.                 }
  44.                 i--;
  45.             }else if(i==-1){
  46.                 EditorApplication.Step();
  47.                 i--;
  48.                 EditorApplication.isPaused = !playing;
  49.             }
  50.         }
  51.     }
  52.  
  53.     void capture(){
  54.         playing = EditorApplication.isPlaying;
  55.         EditorApplication.isPaused=true;
  56.         i = frames-1;
  57.     }
  58.  
  59.     void OnGUI()
  60.     {
  61.         frames = EditorGUILayout.IntField("Frames", frames);
  62.         frameDelay = EditorGUILayout.IntField("Frame Delay", frameDelay);
  63.         captureFrames = GUILayout.Toggle(captureFrames,"Capture Frames");
  64.         if(captureFrames){
  65.             captureUpscale = EditorGUILayout.IntField("Upscale", captureUpscale);
  66.             capturePath = EditorGUILayout.TextField("Path (Path/ExtentionlessName)", capturePath);
  67.         }
  68.         if(GUILayout.Button("Capture"))
  69.         {
  70.             capture();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement