Skip to content

Instantly share code, notes, and snippets.

@AkihiroImada
Last active November 23, 2016 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkihiroImada/59448385fba301379852c1138398a781 to your computer and use it in GitHub Desktop.
Save AkihiroImada/59448385fba301379852c1138398a781 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
[RequireComponent(typeof(CapturePanorama.CapturePanorama))]
public class CapturePanoramaMovieMaker : MonoBehaviour {
[SerializeField]
bool isOnStart_ = true;
[SerializeField]
int fileNumber_ = 0;
CapturePanorama.CapturePanorama capturePanorama_;
bool isRunning_ = false;
bool isRunningCorutine_ = false;
string fullPathBase_ = "";
int maxFrame_ = 0;
string imageFomat_ = "";
StringBuilder stringBuilder_;
public void StartCapture()
{
isRunning_ = true;
}
void Awake () {
capturePanorama_ = GetComponent<CapturePanorama.CapturePanorama>();
/* Tune FPS */
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = capturePanorama_.frameRate;
maxFrame_ = capturePanorama_.maxFramesToRecord;
stringBuilder_ = new StringBuilder();
stringBuilder_.Append(capturePanorama_.saveImagePath);
stringBuilder_.Append("/");
stringBuilder_.Append(capturePanorama_.panoramaName);
fullPathBase_ = stringBuilder_.ToString();
stringBuilder_.Length = 0;
if (capturePanorama_.imageFormat == CapturePanorama.CapturePanorama.ImageFormat.PNG) imageFomat_ = ".png";
else if (capturePanorama_.imageFormat == CapturePanorama.CapturePanorama.ImageFormat.JPEG) imageFomat_ = ".jpeg";
else if (capturePanorama_.imageFormat == CapturePanorama.CapturePanorama.ImageFormat.BMP) imageFomat_ = ".bmp";
}
void Start()
{
if (isOnStart_) StartCapture();
}
void Update () {
if (!isRunning_) return;
Time.timeScale = 0;
AudioListener.pause = true;
if (isRunningCorutine_)
{
}
else
{
if (fileNumber_ >= maxFrame_) UnityEditor.EditorApplication.isPlaying = false;
isRunningCorutine_ = true;
StartCoroutine(coCapture(() =>
{
fileNumber_++;
isRunningCorutine_ = false;
}));
Time.timeScale = 1;
AudioListener.pause = false;
}
}
IEnumerator coCapture(System.Action callback)
{
capturePanorama_.CaptureScreenshotSync(getFileName());
while (!File.Exists(getFullPath()))
{
Debug.Log(getFullPath() + " is not exist");
yield return 0;
}
callback();
}
string getFullPath()
{
stringBuilder_.Append(fullPathBase_);
stringBuilder_.Append(fileNumber_.ToString("D"+capturePanorama_.frameNumberDigits));
stringBuilder_.Append(imageFomat_);
string fullPath = stringBuilder_.ToString();
stringBuilder_.Length = 0;
//Debug.Log("getFullPath = " + fullPath);
return fullPath;
}
string getFileName()
{
stringBuilder_.Append(capturePanorama_.panoramaName);
stringBuilder_.Append(fileNumber_.ToString("D"+capturePanorama_.frameNumberDigits));
string fileName = stringBuilder_.ToString();
stringBuilder_.Length = 0;
//Debug.Log("getFileName = " + fileName);
return fileName;
}
[ContextMenu("SelectDirectory")]
void SetDirectory()
{
const string title = "Save Directory";
capturePanorama_.saveImagePath = UnityEditor.EditorUtility.SaveFolderPanel(title, "", "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment