Sistema de Screenshots v2
Página 1 de 1
Sistema de Screenshots v2
Queridas pessoas! Tudo bem?
Estou compartilhando a versão 2 do meu script de Screenshot, desta vez mais robusto e completo que o anterior, sem perder sua facilidade de uso.
Fácil de definir o local e o nome do arquivo, resolução da imagem, etc...
*Ah, e sistema está disponível em Português e Inglês (dependendo da linguagem do seu Windows)
- Código:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
#if UNITY_EDITOR //Editor script
using UnityEditor;
using System.Threading.Tasks;
[CustomEditor(typeof(Screenshoots))]
public class Screenshoots_Editor : Editor
{
/// <summary>
/// Get the target of this editor
/// </summary>
/// <returns>(Screenshoot)target</returns>
public Screenshoots Target()
{
return (Screenshoots)target;
}
public override void OnInspectorGUI()
{
EditorUtility.ClearProgressBar();
//base.OnInspectorGUI(); //Standalone Unity inspector
#region FilePath
EditorGUILayout.BeginHorizontal(); //Start horizontal layout
Target().FilePath = EditorGUILayout.TextField(Target().FilePath); //Set the File Path string field
if (GUILayout.Button("...")) //Set the brownse button
{
Target().FilePath = EditorUtility.SaveFilePanel(Screenshoots.text(new string[] { "Save Screenshot.", "Salvar Captura de tela." }), Screenshoots.text(new string[] { "File","Arquivo" }), "Screenshot", "jpg"); //Open brownse pannel
}
EditorGUILayout.EndHorizontal(); //Finish Horizontal layout
#endregion
#region File Settings
EditorGUILayout.BeginHorizontal(); // Start horizontal layout
Target().resolution = (Screenshoots.imageSize)EditorGUILayout.EnumPopup(Screenshoots.text(new string[] { "Resolution: ","Resolução: " }), Target().resolution); //Create Resolution field
if (GUILayout.Button(Screenshoots.text(new string[] { "Take Screenshot", "Tirar Screenshoot" }))) //Create Screenshot pannel
{
Target().TakeScreenshot(); //Start screenshoot
EditorApplication.ExecuteMenuItem("Window/Panels/2 Game"); //Move to game Window (The system works only on game window)
}
EditorGUILayout.EndHorizontal(); //Finish horizontal layout
#endregion
EditorGUILayout.BeginHorizontal(); // Start horizontal layout
Target().ShowDialog = EditorGUILayout.Toggle(Screenshoots.text(new string[] { "Show finish dialog", "Mostrar diálogo final" }), Target().ShowDialog);
Target().ReplaceFile = EditorGUILayout.Toggle(Screenshoots.text(new string[] { "Replace file", "Substituir arquivo" }), Target().ReplaceFile);
EditorGUILayout.EndHorizontal(); //Finish horizontal layout
}
/// <summary>
/// Called after take screenshot
/// </summary>
/// <param name="path">the final path of Screenshot</param>
public static void OnScreenshotTaked(string path,bool DisplayDialogs)
{
EditorUtility.DisplayProgressBar(Screenshoots.text(new string[] { "Processing screenshot...", "Processando Captura de tela"}), Screenshoots.text(new string[] { "Please wait...", "Por favor aguarde..." }), 50);
ShowScreenshotDialog(path, DisplayDialogs);
}
/// <summary>
/// Count the time waiting
/// </summary>
static float TimeWaiting;
public static async void ShowScreenshotDialog(string path, bool DisplayDialogs)
{
//Make this thread wait 500ms
await Task.Delay(500);
//Check if Screenshot process is finished.
if (!File.Exists(path))
{
if (DisplayDialogs)
{
//Show loading messages
if (TimeWaiting < 3)
{
EditorUtility.DisplayProgressBar(Screenshoots.text(new string[] { "Processing screenshot...", "Processando Captura de tela" }), Screenshoots.text(new string[] { "Processing the image, Please wait...", "Processando a imagem, Por favor aguarde..." }), 50);
}
else
{
EditorUtility.DisplayProgressBar(Screenshoots.text(new string[] { "Processing screenshot...", "Processando Captura de tela" }), Screenshoots.text(new string[] { "Still processing... This may take a while... ", "Ainda processando, Isso pode demorar um pouco..." }), 50);
}
TimeWaiting += 0.5f;
//If the process not finished yet it calls this void again
ShowScreenshotDialog(path, DisplayDialogs);
}
}
else
{
if (DisplayDialogs)
{
//Finish the progress bar
EditorUtility.ClearProgressBar();
int Option = EditorUtility.DisplayDialogComplex(Screenshoots.text(new string[] { "screenshot saved!", "Captura de tela salva!" }), Screenshoots.text(new string[] { "Your screenshot has ben saved on: \n ''" + path + "''.", "Sua captura de tela foi salva em: \n ''" + path + "''." }), Screenshoots.text(new string[] { "Open Folder", "Abrir pasta" }), Screenshoots.text(new string[] { "Continue", "Continuar" }), Screenshoots.text(new string[] { "Open File", "Abrir arquivo" }));
switch (Option)
{
case 0: //Open the file directory
Application.OpenURL("file://" + Directory.GetParent(path));
break;
case 2: //Open the file
Application.OpenURL("file://" + path);
break;
case 1: //Close dialog
break;
default:
break;
}
}
Debug.Log("Screenshot saved on: ''" + path + "''");
}
}
}
public class Screenshoots_Window : EditorWindow
{
public static string text(string[] LangTexts)
{
string Result = LangTexts[0];
if (Application.systemLanguage == SystemLanguage.Portuguese)
{
Result = LangTexts[1];
}
return Result;
}
static string FilePath;
static int resolution;
static bool ShowDialog;
static bool ReplaceFile;
static bool Loaded = false;
static bool Fouldout;
[MenuItem("Screenshoot/Show")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(Screenshoots_Window));
}
public void OnGUI()
{
if (!Loaded)
{
LoadSettings();
}
EditorUtility.ClearProgressBar();
//base.OnInspectorGUI(); //Standalone Unity inspector
#region FilePath
EditorGUILayout.BeginHorizontal(); //Start horizontal layout
FilePath = EditorGUILayout.TextField(FilePath); //Set the File Path string field
if (GUILayout.Button("...")) //Set the brownse button
{
FilePath = EditorUtility.SaveFilePanel(Screenshoots.text(new string[] { "Save Screenshot.", "Salvar Captura de tela." }), Screenshoots.text(new string[] { "File", "Arquivo" }), "Screenshot", "jpg"); //Open brownse pannel
}
EditorGUILayout.EndHorizontal(); //Finish Horizontal layout
#endregion
//#region File Settings
resolution = (int)(Screenshoots.imageSize)EditorGUILayout.EnumPopup(Screenshoots.text(new string[] { "Resolution: ", "Resolução: " }), (Screenshoots.imageSize)resolution); //Create Resolution field
//#endregion
Fouldout = EditorGUILayout.Foldout(Fouldout, Screenshoots.text(new string[] { "More settings", "Mais opções" }));
if(Fouldout)
{
EditorGUILayout.BeginHorizontal(); // Start horizontal layout
ReplaceFile = EditorGUILayout.Toggle(Screenshoots.text(new string[] { "Replace file", "Substituir arquivo" }), ReplaceFile);
ShowDialog = EditorGUILayout.Toggle(Screenshoots.text(new string[] { "Show finish dialog", "Mostrar diálogo final" }), ShowDialog);
EditorGUILayout.EndHorizontal(); //Finish horizontal layout
}
GUILayout.FlexibleSpace();
EditorGUILayout.BeginHorizontal(); // Start horizontal layout
if (GUILayout.Button(Screenshoots.text(new string[] { "Manager", "Gerenciar" }), GUILayout.Height(40),GUILayout.MaxWidth(70), GUILayout.MinWidth(10))) //Create Manage button
{
Application.OpenURL("file://" + Directory.GetParent(FilePath));
}
if (GUILayout.Button(Screenshoots.text(new string[] { "Take Screenshot", "Tirar Screenshoot" }), GUILayout.Height(40))) //Create Screenshot button
{
TakeScreenshot(FilePath, resolution); //Start screenshoot
EditorApplication.ExecuteMenuItem("Window/Panels/2 Game"); //Move to game Window (The system works only on game window)
}
EditorGUILayout.EndHorizontal(); //Finish horizontal layout
SaveSettings();
}
/// <summary>
/// Called after take screenshot
/// </summary>
/// <param name="path">the final path of Screenshot</param>
public static void OnScreenshotTaked(string path, bool DisplayDialogs)
{
EditorUtility.DisplayProgressBar(Screenshoots.text(new string[] { "Processing screenshot...", "Processando Captura de tela" }), Screenshoots.text(new string[] { "Please wait...", "Por favor aguarde..." }), 50);
ShowScreenshotDialog(path, DisplayDialogs);
}
/// <summary>
/// Count the time waiting
/// </summary>
static float TimeWaiting;
public static async void ShowScreenshotDialog(string path, bool DisplayDialogs)
{
//Make this thread wait 500ms
await Task.Delay(500);
//Check if Screenshot process is finished.
if (!File.Exists(path))
{
if (DisplayDialogs)
{
//Show loading messages
if (TimeWaiting < 3)
{
EditorUtility.DisplayProgressBar(Screenshoots.text(new string[] { "Processing screenshot...", "Processando Captura de tela" }), Screenshoots.text(new string[] { "Processing the image, Please wait...", "Processando a imagem, Por favor aguarde..." }), 50);
}
else
{
EditorUtility.DisplayProgressBar(Screenshoots.text(new string[] { "Processing screenshot...", "Processando Captura de tela" }), Screenshoots.text(new string[] { "Still processing... This may take a while... ", "Ainda processando, Isso pode demorar um pouco..." }), 50);
}
TimeWaiting += 0.5f;
//If the process not finished yet it calls this void again
ShowScreenshotDialog(path, DisplayDialogs);
}
}
else
{
if (DisplayDialogs)
{
//Finish the progress bar
EditorUtility.ClearProgressBar();
int Option = EditorUtility.DisplayDialogComplex(Screenshoots.text(new string[] { "screenshot saved!", "Captura de tela salva!" }), Screenshoots.text(new string[] { "Your screenshot has ben saved on: \n ''" + path + "''.", "Sua captura de tela foi salva em: \n ''" + path + "''." }), Screenshoots.text(new string[] { "Open Folder", "Abrir pasta" }), Screenshoots.text(new string[] { "Continue", "Continuar" }), Screenshoots.text(new string[] { "Open File", "Abrir arquivo" }));
switch (Option)
{
case 0: //Open the file directory
Application.OpenURL("file://" + Directory.GetParent(path));
break;
case 2: //Open the file
Application.OpenURL("file://" + path);
break;
case 1: //Close dialog
break;
default:
break;
}
}
Debug.Log("Screenshot saved on: ''" + path + "''");
}
}
public static int BoolToInt(bool Target)
{
if (Target == true)
{
return 1;
} else
{
return 0;
}
}
public static bool IntToBool(int Target)
{
return Target != 0;
}
public static void SaveSettings()
{
PlayerPrefs.SetString("Screenshots_Location", FilePath);
PlayerPrefs.SetInt("Screenshots_resolution", (int)resolution);
PlayerPrefs.SetInt("Screenshots_ShowDialog", BoolToInt(ShowDialog));
PlayerPrefs.SetInt("Screenshots_ReplaceFile", BoolToInt(ReplaceFile));
PlayerPrefs.SetInt("Screenshots_Loaded", 1);
}
public static void LoadSettings()
{
FilePath = PlayerPrefs.GetString("Screenshots_Location");
resolution = PlayerPrefs.GetInt("Screenshots_resolution");
ShowDialog = PlayerPrefs.GetInt("Screenshots_ShowDialog") != 0;
ReplaceFile = PlayerPrefs.GetInt("Screenshots_ReplaceFile") != 0;
Loaded = PlayerPrefs.GetInt("Screenshots_Loaded") != 0;
}
/// <summary>
/// Take a new Screenshot manualiy
/// </summary>
/// <param name="path">The folder path of file (without file)</param>
/// <param name="name">The file name (without format)</param>
/// <param name="format">the format of file</param>
/// <param name="resolution">The resolution multipiler</param>
public void TakeScreenshot(string name, int resolution)
{
string _filePath = name; //Combine the file name with path
//Replace file or create a new
if (!ReplaceFile)
{
//Check if file exists on index
int i = 0;
while (File.Exists(_filePath))
{
i++;
_filePath = name.Replace(".", "_" + i + "."); //Set new name if file already exists
}
}
ScreenCapture.CaptureScreenshot(_filePath, resolution); //Create the screenshot
Debug.Log("Processing Screenshot...");
OnScreenshotTaked(_filePath, ShowDialog);
}
}
#endif
//Runtime script
public class Screenshoots : MonoBehaviour
{
[System.Serializable]
public enum imageSize
{
_1x = 1,
_2x = 2,
_3x = 3,
_4x = 4,
_5x = 5,
_6x = 6,
_7x = 7,
_8x = 8,
_9x = 9,
_10x = 10,
}
/// <summary>
/// The path of file with name
/// </summary>
public string FilePath;
/// <summary>
/// The Resolution multipiler of file
/// </summary>
[Tooltip("The resolution multipiler of the screenshot")]
[SerializeField]
public imageSize resolution;
public bool ShowDialog = true;
public bool ReplaceFile = false;
/// <summary>
/// File final patch
/// </summary>
public string _file { get; set; }
/// <summary>
/// Take a new Screenshot manualiy
/// </summary>
/// <param name="path">The folder path of file (without file)</param>
/// <param name="name">The file name (without format)</param>
/// <param name="format">the format of file</param>
/// <param name="resolution">The resolution multipiler</param>
public void TakeScreenshot(string name, int resolution)
{
string _filePath = name; //Combine the file name with path
//Replace file or create a new
if (!ReplaceFile)
{
//Check if file exists on index
int i = 0;
while (File.Exists(_filePath))
{
i++;
_filePath = name.Replace(".", "_" + i + "."); //Set new name if file already exists
}
}
ScreenCapture.CaptureScreenshot(_filePath, resolution); //Create the screenshot
_file = _filePath;
Debug.Log("Processing Screenshot...");
OnScreenshot();
}
/// <summary>
/// Called after "TakeScreemshot()"
/// </summary>
void OnScreenshot()
{
#if UNITY_EDITOR
Screenshoots_Editor.OnScreenshotTaked(_file, ShowDialog);
#endif
}
/// <summary>
/// Take the screenshot automatically
/// </summary>
public void TakeScreenshot()
{
TakeScreenshot(FilePath, (int)resolution);
}
/// <summary>
/// Translate some text
/// </summary>
/// <param name="LangTexts">Text to translate</param>
/// <returns></returns>
public static string text(string[] LangTexts)
{
string Result = LangTexts[0];
if (Application.systemLanguage == SystemLanguage.Portuguese)
{
Result = LangTexts[1];
}
return Result;
}
}
Para instalar, basta criar um novo documento de texto ou script chamado "Screenshoots.cs" (sem aspas) e colar o codigo acima.
*Para evitar confusão, por favor não compartilhe o código sem atribuir os créditos a mim. A venda deste conteúdo não é permitida e nem disponibilizada em outros lugares.
Tópicos semelhantes
» Metadados recusados AppleConnect [Screenshots]
» Como criar sistema de Habilidades (Skills) para o personagem 2D / e sistema de batalha 2D
» [TUTORIAL] Sistema de DIA E NOITE completo, com luzes noturnas e SISTEMA DE NUVENS
» Posso usar o sistema de marcas de derrapagem como base para outro sistema?
» Como fazer Sistema Dia e Noite Segui o sistema de Hora
» Como criar sistema de Habilidades (Skills) para o personagem 2D / e sistema de batalha 2D
» [TUTORIAL] Sistema de DIA E NOITE completo, com luzes noturnas e SISTEMA DE NUVENS
» Posso usar o sistema de marcas de derrapagem como base para outro sistema?
» Como fazer Sistema Dia e Noite Segui o sistema de Hora
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos