yanchi.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.UI;
  6. public class yanchi : MonoBehaviour
  7. {
  8. public RawImage rimage;
  9. public RenderTexture rt;
  10. Queue<Texture2D> qlist = new Queue<Texture2D>();
  11. List<Texture2D> list = new List<Texture2D>();
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. for (int i = 0; i < maxcount; i++)
  16. {
  17. list.Add(new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false));
  18. }
  19. StartCoroutine(GetRenederFPS());
  20. }
  21. int maxcount = 1;
  22. int count=0;
  23. byte[] bts;
  24. IEnumerator GetRenederFPS()
  25. {
  26. yield return new WaitForSeconds(1) ;
  27. while (true)
  28. {
  29. var req = AsyncGPUReadback.Request(rt);
  30. yield return new WaitUntil(() => req.done);
  31. if (!req.hasError)
  32. {
  33. if (bts == null)
  34. {
  35. bts = new byte[req.layerDataSize];
  36. }
  37. req.GetData<byte>().CopyTo(bts);
  38. list[count].LoadRawTextureData(bts);
  39. list[count].Apply();
  40. qlist.Enqueue(list[count]);
  41. if (qlist.Count >= maxcount)
  42. {
  43. rimage.texture = qlist.Dequeue();
  44. }
  45. count++;
  46. if(count>= list.Count)
  47. {
  48. count = 0;
  49. }
  50. // tex.SetPixels32(req.GetData<Color32>().ToArray());
  51. }
  52. else
  53. {
  54. Debug.LogError("Error AsyncGPUReadbackRequest.hasError");
  55. }
  56. }
  57. }
  58. // Update is called once per frame
  59. void Update()
  60. {
  61. }
  62. }