Unity简单操作:PC端 将游戏场景 摄像机画面截屏保存到路径

 仅适合摄像机是正交模式,Projection=Orthorgraphic

Unity2017.x+及以上 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaptureScreenshotC : MonoBehaviour {

	private int count = 0;

	void Update () {
        if (Input.GetKeyDown(KeyCode.P))
        {
			OnJTButtonClick();
		}
	}

	public void ScreenShotFile(string fileName) {
		UnityEngine.ScreenCapture.CaptureScreenshot(fileName);
	}
	public void OnJTButtonClick() {
		count++;
		Debug.Log(Application.persistentDataPath + "/" + count + ".png");
		ScreenShotFile(Application.persistentDataPath + "/"+count+".png");
	}
}

unity5.6.x及以下

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI; 

public class CaptureScreenshotC : MonoBehaviour
{

    public RawImage CaptureScreenshot;    


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        { 
            CaptureScreenshot.texture = CaptureCamera(Camera.main, new Rect(0, 0, 1920 , 1440),true); 
        }
    }
 
   


    /// <summary>
    /// 相机截图
    /// </summary>
    /// <param name="camera">截屏相机</param>
    /// <param name="rect">截屏区域</param>
    /// <returns></returns>
    public Texture2D CaptureCamera(Camera camera, Rect rect,bool isSaveToFlie)
    {
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        camera.targetTexture = rt;
        camera.Render();
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();
        if (isSaveToFlie) {

            string path = UnityEngine.Application.persistentDataPath + "/" + "AAA.png";
            Debug.Log("保存位置:" + path);
            SaveTextureToFile(screenShot, path);
        }
        camera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
        return screenShot;
    }

    private void SaveTextureToFile(Texture2D texture2D,string mFileName)
    {
        byte[] bytes = texture2D.EncodeToPNG();
        //保存
        System.IO.File.WriteAllBytes(mFileName, bytes);
    }
     

}

 仅适合摄像机是投影模式,Projection=Perspective,但是不包含UI

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class CaptureScreenshotC : MonoBehaviour
{

    public RawImage CaptureScreenshot;

    public Camera Camera;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            //获取正交模式的 摄像机画面
            try
            {
                //CaptureScreenshot.texture = CaptureCamera(Camera, new Rect(0, 0, 1920, 1440), true);
            }
            catch (System.Exception e)
            {
                              
            }

            //获取投影模式的摄像机画面
            CameraCapture(Camera,"AAA.PNG");


        }
    }




    /// <summary>
    /// 相机截图
    /// </summary>
    /// <param name="camera">截屏相机</param>
    /// <param name="rect">截屏区域</param>
    /// <returns></returns>
    public Texture2D CaptureCamera(Camera camera, Rect rect, bool isSaveToFlie)
    {
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        camera.targetTexture = rt;
        camera.Render();
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();
        if (isSaveToFlie)
        {

            string path = UnityEngine.Application.persistentDataPath + "/" + "AAA.png";
            Debug.Log("保存位置:" + path);
            SaveTextureToFile(screenShot, path);
        }
        camera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
        return screenShot;
    }

    private void SaveTextureToFile(Texture2D texture2D, string mFileName)
    {
        byte[] bytes = texture2D.EncodeToPNG();
        //保存
        System.IO.File.WriteAllBytes(mFileName, bytes);
    }





    void CameraCapture(Camera m_Camera, string filename)
    {
        int width = Screen.width;
        int height = Screen.height;

        RenderTexture rt = new RenderTexture(width, height, 16);
        m_Camera.targetTexture = rt;
        m_Camera.Render();
        RenderTexture.active = rt;

        Texture2D t = new Texture2D(width, height);
        t.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        t.Apply();

        string path = Path(filename);
        Debug.Log(path);
        System.IO.File.WriteAllBytes(path, t.EncodeToJPG());
        m_Camera.targetTexture = null;
    }

    public static string Path(string fileName)
    {

#if UNITY_EDITOR

        //  return Application.dataPath + "/StreamingAssets/" + fileName;

        return UnityEngine.Application.persistentDataPath + "/" + fileName;

#elif UNITY_IPHONE
 
    return Application.dataPath+"/Ray/" + fileName;
 
#elif UNITY_ANDROID
 
    return "jar:file://" + Application.dataPath + "!/assets/" + fileName;

#else
    return Application.dataPath + "/StreamingAssets/" + fileName;
 
#endif

    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AD_喵了个咪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值