/
Unity保存大图截图
void OnGUI(){
if (GUI.Button (new Rect (5, 5, 120, 30), "CaptureScreen1"))
Application.CaptureScreenshot("Screenshot1.png", 1);//屏幕像素大小的图片
if (GUI.Button (new Rect (5, 40, 120, 30), "CaptureScreen2"))
Application.CaptureScreenshot("Screenshot2.png", 2);//屏幕2倍大
if (GUI.Button (new Rect (5, 180, 120, 30), "CaptureScreen10"))
Application.CaptureScreenshot("Screenshot10.png", 10);//屏幕3倍大
}放大系数小于0时,按默认值0处理,即图片不放大也不缩小
针对特定相机截图
/// <summary>
/// 对相机截图。
/// </summary>
/// <returns>The screenshot2.</returns>
/// <param name="camera">Camera.要被截屏的相机</param>
/// <param name="rect">Rect.截屏的区域</param>
Texture2D CaptureCamera(Camera camera, Rect rect)
{
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Apply();
// 重置相关参数,以使用camera继续在屏幕上显示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// 最后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一张照片: {0}", filename));
return screenShot;
}
System.Diagnostics.Process process = new System.Diagnostics.Process(); //系统进程
process.StartInfo.CreateNoWindow = true; //不显示调用程序窗口
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;//
process.StartInfo.UseShellExecute = true; //采用操作系统自动识别模式
//Debug.Log(path);
process.StartInfo.FileName=path; //要打印的文件路径
process.StartInfo.Verb="print"; //指定执行的动作,打印:print 打开:open …………
process.Start();
public class ex: MonoBehaviour {
private System.Drawing.Printing.PrintDocument printDocument1;
void Start () {
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage); }
void OnGUI(){
if(GUI.Button(new Rect(5,5,80,50),"print"))
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bitmap = new Bitmap("image.jpg");//根目录
e.Graphics.DrawImage(bitmap, 150, 240, 500, 700);
}
}
判断目标在自己的前后
Vector3.Dot(transform.forward, target.position)
返回值为正时,目标在自己的前方,反之在自己的后方
2.判断目标在机子的左右
Vector3.Cross(transform.forward, target.position).y
返回值为正时,目标在自己的右方,反之在自己的左方
/
using System.IO;
using UnityEditor;
using UnityEngine;
public class ScreenCaptureEditor : EditorWindow
{
private static string directory = "Screenshots/Capture/";
private static string latestScreenshotPath = "";
private bool initDone = false;
private GUIStyle BigText;
void InitStyles()
{
initDone = true;
BigText = new GUIStyle(GUI.skin.label)
{
fontSize = 20,
fontStyle = FontStyle.Bold
};
}
private void OnGUI()
{
if (!initDone)
{
InitStyles();
}
GUILayout.Label("Screen Capture Tools", BigText);
if (GUILayout.Button("单张截图"))
{
TakeScreenshot();
}
GUILayout.Label("当前分辨率: " + GetResolution());
if (GUILayout.Button("打开文件夹"))
{
ShowFolder();
}
GUILayout.Label("保存路径: " + directory);
}
[MenuItem("Tools/Screenshots/打开窗口 &`", false, 0)]
public static void ShowWindow()
{
GetWindow(typeof(ScreenCaptureEditor));
}
[MenuItem("Tools/Screenshots/存储路径 &2", false, 2)]
private static void ShowFolder()
{
if (File.Exists(latestScreenshotPath))
{
EditorUtility.RevealInFinder(latestScreenshotPath);
return;
}
Directory.CreateDirectory(directory);
EditorUtility.RevealInFinder(directory);
}
[MenuItem("Tools/Screenshots/单张截图 &1", false, 1)]
private static void TakeScreenshot()
{
Directory.CreateDirectory(directory);
var currentTime = System.DateTime.Now;
var filename = currentTime.ToString().Replace('/', '-').Replace(':', '_') + ".png";
var path = directory + filename;
ScreenCapture.CaptureScreenshot(path);
latestScreenshotPath = path;
Debug.Log($"截图路径: <b>{path}</b> 分辨率: <b>{GetResolution()}</b>");
}
private static string GetResolution()
{
Vector2 size = Handles.GetMainGameViewSize();
Vector2Int sizeInt = new Vector2Int((int)size.x, (int)size.y);
return $"{sizeInt.x}x{sizeInt.y}";
}
}
【载屏到文件】慢
ScreenCapture.CaptureScreenshot("xxx.png")
因为CPU要向GPU等待数据,非常卡,推荐
【全量载屏到纹理】快
// 先等一帧
yield return new WaitForEndOfFrame();
var screentex= RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.Default, "rt", false, false);
// 全量渲一帧
ScreenCapture.CaptureScreenshotIntoRenderTexture(rt);
// 获得平台差异
var rect = SystemInfo.graphicsUVStartsAtTop ? new Vector4(1.0f, -1.0f, 0.0f, 1.0f) : new Vector4(1.0f, 1.0f, 0.0f, 0.0f);
var displayTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.Default, "displayTex", false, false, FilterMode.Bilinear);
// 画到指定纹理上
Graphics.Blit(screentex, displayTex, new Vector2(rect.x, rect.y), new Vector2(rect.z, rect.w));
RenderTexture.ReleaseTemporary(screentex);
【不带UI截图到RT】快
var mainCamera = GetComponent<Camera>();
if (mainCamera != null)
{
var screentex= RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.Default, "rt", false, false);
var lastTex = mainCamera.targetTexture;
mainCamera.targetTexture = screentex;
mainCamera.Render();
mainCamera.targetTexture = lastTex;
}
【直接读】慢
Texture2D CaptureScreenshot2(Rect rect)
{
Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();//然后将这些纹理数据,成一个png图片文件
/