PhotoShopCamera.cs 841 B

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class PhotoShopCamera : MonoBehaviour
  6. {
  7. public Camera shotCam;
  8. public Action<Texture2D> onRenderFinish;
  9. public void Awake()
  10. {
  11. shotCam = GetComponent<Camera>();
  12. }
  13. public void OnPostRender()
  14. {
  15. //设定当前RenderTexture为快照相机的targetTexture
  16. RenderTexture rt = shotCam.targetTexture;
  17. RenderTexture.active = rt;
  18. Texture2D tex = new Texture2D(rt.width, rt.height);
  19. //读取缓冲区像素信息
  20. tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
  21. tex.Apply();
  22. if (onRenderFinish != null)
  23. {
  24. onRenderFinish.Invoke(tex);
  25. }
  26. Destroy(tex);
  27. }
  28. }