【Unity】Unity3d截图 指定相机截图

文章中主要参考了 http://blog.csdn.net/anyuanlzh/article/details/17008909;

对其中还有 一些疑惑:就是 如果相机有天空盒 就无法截取到3D物体 比如Cube 等

Camera :主要对物体进行截取 Clear Flags 不选择 天空盒

Camera2: 主要对UI进行截取

 

using UnityEngine;
using System.Collections;

public class Screenshot : MonoBehaviour
{
    public Camera mainCamera;
    public Camera uiCamera;
    /// <summary>  
    /// 对相机截图。   
    /// </summary>  
    /// <returns>The screenshot2.</returns>  
    /// <param name="camera">Camera.要被截屏的相机</param>  
    /// <param name="rect">Rect.截屏的区域</param>  
    Texture2D CaptureCamera(Camera camera, Camera camera2 ,Rect rect)
    {
        // 创建一个RenderTexture对象  
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, -1);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机  
        camera.targetTexture = rt;
        camera.Render();
        //ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。  
        camera2.targetTexture = rt;
        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;
        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;
    }

    void OnGUI()
    {
        if (GUILayout.Button("AAAA"))
        {
            CaptureCamera(mainCamera, uiCamera,new Rect(0, 0, Screen.width, Screen.height));
        }
    }
}

 

 

 

 

 

 

指定相机 截图
void SaveFile ()
	{

		shot_Number++;
		if (shot_Number > 9) {
			shot_Number = 0;
		}

		//HideUI ();
		//yield return new WaitForEndOfFrame ();
		//保存

		//yield return new WaitForSeconds(0.5f);
		# if UNITY_EDITOR
//		temp = "/StreamingAssets/ScreenShot/";
		temp = "/ScreenShot/";
		string path = string.Format ("{0:D4}{1:D2}.png", temp, shot_Number.ToString ());
//		filename = Application.dataPath + path;
		filename = Application.persistentDataPath + path;
		# else


		#if UNITY_ANDROID
		temp = "/ScreenShot/";
		string path  = string.Format ("{0:D4}{1:D2}.png", temp,shot_Number.ToString());
		filename = Application.persistentDataPath + path;

		#endif

		#if UNITY_IPHONE
		temp = "/Raw/ScreenShot/";
		string path  = string.Format ("{0:D4}{1:D2}.png", temp,shot_Number.ToString());
		filename = Application.temporaryCachePath + path;

		#endif

		#if UNITY_STANDALONE_WIN
		filename = "Screenshot.png";
		#endif
		#endif

		Camera earthCamera;
		GameObject earth = GameObject.Find ("EarthCamera");
		if (earth) {
			earthCamera = GameObject.Find ("EarthCamera").GetComponent<Camera> ();
		} else {
			return;
		}
		 
		RenderTexture renderTexture;
		//深度问题depth
		renderTexture = new RenderTexture (Screen.width, Screen.height, 24);
		earthCamera.targetTexture = renderTexture;
		earthCamera.Render ();

		Texture2D myTexture2D = new Texture2D (renderTexture.width, renderTexture.height);
		RenderTexture.active = renderTexture;
		myTexture2D.ReadPixels (new Rect (0, 0, renderTexture.width, renderTexture.height), 0, 0);
		myTexture2D.Apply ();
		byte[] bytes = myTexture2D.EncodeToJPG ();
		myTexture2D.Compress (true);
		myTexture2D.Apply ();  
		RenderTexture.active = null;


		System.IO.File.WriteAllBytes (filename, bytes);  
		//Debug.Log (string.Format ("截屏了一张图片: {0}", filename));  
		Debug.Log ("保存图片的路径" + filename);

		earthCamera.targetTexture = null;  
		GameObject.Destroy (renderTexture); 

	}

 

 

 

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Unity_阿黄

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

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

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

打赏作者

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

抵扣说明:

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

余额充值