152 lines
4.1 KiB
C#
152 lines
4.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using GaiaCommon1;
|
|
using UnityEngine;
|
|
|
|
namespace Gaia
|
|
{
|
|
[GaiaScriptOrder(500)]
|
|
public class ScreenShotter : MonoBehaviour
|
|
{
|
|
public KeyCode m_screenShotKey = KeyCode.F12;
|
|
|
|
public GaiaConstants.StorageFormat m_imageFormat = GaiaConstants.StorageFormat.JPG;
|
|
|
|
public string m_targetDirectory = "Screenshots";
|
|
|
|
public int m_targetWidth = 1900;
|
|
|
|
public int m_targetHeight = 1200;
|
|
|
|
public bool m_useScreenSize = true;
|
|
|
|
public Camera m_mainCamera;
|
|
|
|
private bool m_takeShot;
|
|
|
|
private bool m_refreshAssetDB;
|
|
|
|
public Texture2D m_watermark;
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (m_mainCamera == null)
|
|
{
|
|
m_mainCamera = FindCamera();
|
|
}
|
|
string path = Path.Combine(Application.dataPath, m_targetDirectory);
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (m_refreshAssetDB)
|
|
{
|
|
m_refreshAssetDB = false;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
m_mainCamera = FindCamera();
|
|
}
|
|
|
|
public Camera FindCamera()
|
|
{
|
|
Camera camera = Camera.main;
|
|
if (camera == null)
|
|
{
|
|
camera = GameObject.Find("Main Camera").GetComponent<Camera>();
|
|
}
|
|
if (camera == null)
|
|
{
|
|
camera = GameObject.Find("Camera").GetComponent<Camera>();
|
|
}
|
|
if (camera == null)
|
|
{
|
|
camera = GameObject.Find("FirstPersonCharacter").GetComponent<Camera>();
|
|
}
|
|
if (camera == null)
|
|
{
|
|
return null;
|
|
}
|
|
return camera;
|
|
}
|
|
|
|
private string ScreenShotName(int width, int height)
|
|
{
|
|
string text = Path.Combine(Application.dataPath, m_targetDirectory);
|
|
text = text.Replace('\\', '/');
|
|
if (text[text.Length - 1] == '/')
|
|
{
|
|
text = text.Substring(0, text.Length - 1);
|
|
}
|
|
if (m_imageFormat == GaiaConstants.StorageFormat.JPG)
|
|
{
|
|
return string.Format("{0}/Grab {1} w{2}h{3} x{4}y{5}z{6}r{7}.jpg", text, DateTime.Now.ToString("yyyyMMddHHmmss"), width, height, (int)m_mainCamera.transform.position.x, (int)m_mainCamera.transform.position.y, (int)m_mainCamera.transform.position.z, (int)m_mainCamera.transform.rotation.eulerAngles.y);
|
|
}
|
|
return string.Format("{0}/Grab {1} w{2}h{3} x{4}y{5}z{6}r{7}.png", text, DateTime.Now.ToString("yyyyMMdd HHmmss"), width, height, (int)m_mainCamera.transform.position.x, (int)m_mainCamera.transform.position.y, (int)m_mainCamera.transform.position.z, (int)m_mainCamera.transform.rotation.eulerAngles.y);
|
|
}
|
|
|
|
public void TakeHiResShot()
|
|
{
|
|
m_takeShot = true;
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (Input.GetKeyDown(m_screenShotKey) || m_takeShot)
|
|
{
|
|
if (m_useScreenSize)
|
|
{
|
|
m_targetWidth = Screen.width;
|
|
m_targetHeight = Screen.height;
|
|
}
|
|
m_refreshAssetDB = true;
|
|
RenderTexture renderTexture = new RenderTexture(m_targetWidth, m_targetHeight, 24);
|
|
m_mainCamera.targetTexture = renderTexture;
|
|
Texture2D texture2D = new Texture2D(m_targetWidth, m_targetHeight, TextureFormat.RGB24, mipChain: false);
|
|
m_mainCamera.Render();
|
|
RenderTexture.active = renderTexture;
|
|
texture2D.ReadPixels(new Rect(0f, 0f, m_targetWidth, m_targetHeight), 0, 0);
|
|
m_mainCamera.targetTexture = null;
|
|
RenderTexture.active = null;
|
|
UnityEngine.Object.Destroy(renderTexture);
|
|
if (m_watermark != null)
|
|
{
|
|
GaiaUtils.MakeTextureReadable(m_watermark);
|
|
texture2D = AddWatermark(texture2D, m_watermark);
|
|
}
|
|
byte[] bytes = texture2D.EncodeToJPG();
|
|
string text = ScreenShotName(m_targetWidth, m_targetHeight);
|
|
GaiaCommon1.Utils.WriteAllBytes(text, bytes);
|
|
m_takeShot = false;
|
|
Debug.Log($"Took screenshot to: {text}");
|
|
}
|
|
}
|
|
|
|
public Texture2D AddWatermark(Texture2D background, Texture2D watermark)
|
|
{
|
|
int num = background.width - watermark.width - 10;
|
|
int num2 = num + watermark.width;
|
|
int num3 = 8;
|
|
int num4 = num3 + watermark.height;
|
|
for (int i = num; i < num2; i++)
|
|
{
|
|
for (int j = num3; j < num4; j++)
|
|
{
|
|
Color pixel = background.GetPixel(i, j);
|
|
Color pixel2 = watermark.GetPixel(i - num, j - num3);
|
|
Color color = Color.Lerp(pixel, pixel2, pixel2.a / 1f);
|
|
background.SetPixel(i, j, color);
|
|
}
|
|
}
|
|
background.Apply();
|
|
return background;
|
|
}
|
|
}
|
|
}
|