TestThreadCamera.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.UI;
  6. public class TestThreadCamera : MonoBehaviour
  7. {
  8. public Camera ca;
  9. Texture2D screenShot;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. RenderTexture rt = new RenderTexture(640, 480, 0, RenderTextureFormat.ARGB32);
  14. ca.targetTexture = rt;
  15. screenShot = new Texture2D(ca.targetTexture.width,ca.targetTexture.height,TextureFormat.RGBA32, false);
  16. StartCoroutine(GetRenederFPS());
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. }
  22. byte[] bts;
  23. IEnumerator GetRenederFPS()
  24. {
  25. while (true)
  26. {
  27. var req = AsyncGPUReadback.Request(ca.targetTexture,0, TextureFormat.RGBA32);
  28. yield return new WaitUntil(() => req.done);
  29. if (!req.hasError)
  30. {
  31. if (bts == null)
  32. {
  33. bts = new byte[req.layerDataSize];
  34. }
  35. req.GetData<byte>().CopyTo(bts);
  36. screenShot.LoadRawTextureData(bts);
  37. screenShot.Apply();
  38. // tex.SetPixels32(req.GetData<Color32>().ToArray());
  39. this.GetComponent<RawImage>().texture = screenShot;
  40. }
  41. else
  42. {
  43. Debug.LogError("Error AsyncGPUReadbackRequest.hasError");
  44. }
  45. }
  46. }
  47. }