FXCameraCapture.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using UnityEngine;
  3. public class FXCameraCapture : BaseBehaviour
  4. {
  5. private void Update()
  6. {
  7. }
  8. public void OnEnable()
  9. {
  10. if (this.texture == null)
  11. {
  12. Log.Error("纹理为空!");
  13. return;
  14. }
  15. if (!Application.isEditor && this.texture != null)
  16. {
  17. this.texture.Release();
  18. this.texture.width = Screen.width;
  19. this.texture.height = Screen.height;
  20. if (!this.texture.IsCreated())
  21. {
  22. this.texture.Create();
  23. }
  24. }
  25. this.origCamera = base.GetComponent<Camera>();
  26. this.capCamera = new GameObject("CaptureTexture", new Type[]
  27. {
  28. typeof(Camera)
  29. }).GetComponent<Camera>();
  30. UnityEngine.Object.DontDestroyOnLoad(this.capCamera);
  31. }
  32. public void OnDisable()
  33. {
  34. this.origCamera.targetTexture = null;
  35. UnityEngine.Object.DestroyObject(this.capCamera);
  36. }
  37. public void OnPreRender()
  38. {
  39. this.capCamera.CopyFrom(this.origCamera);
  40. this.capCamera.cullingMask = this.cullMaskLayerMask;
  41. this.capCamera.targetTexture = this.texture;
  42. this.capCamera.gameObject.SetActive(false);
  43. this.capCamera.Render();
  44. }
  45. private Camera origCamera;
  46. private Camera capCamera;
  47. [HideInInspector]
  48. public RenderTexture texture;
  49. public LayerMask cullMaskLayerMask;
  50. }