FXCameraDistort.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using UnityEngine;
  3. namespace Distort
  4. {
  5. public class FXCameraDistort : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. if (!Application.isEditor && this.texture != null)
  10. {
  11. this.texture.Release();
  12. this.texture.width = Screen.width;
  13. this.texture.height = Screen.height;
  14. if (!this.texture.IsCreated())
  15. {
  16. this.texture.Create();
  17. }
  18. }
  19. }
  20. private void Update()
  21. {
  22. }
  23. private void OnDisable()
  24. {
  25. this.mainCamera.targetTexture = null;
  26. this.mainCamera = null;
  27. if (this.tempTexture)
  28. {
  29. RenderTexture.ReleaseTemporary(this.texture);
  30. }
  31. }
  32. private void OnEnable()
  33. {
  34. if (this.mainCamera == null)
  35. {
  36. this.mainCamera = Camera.main.GetComponent<Camera>();
  37. }
  38. if (this.texture == null)
  39. {
  40. this.texture = RenderTexture.GetTemporary(this.mainCamera.pixelWidth, this.mainCamera.pixelHeight);
  41. this.tempTexture = true;
  42. }
  43. if (this.texture != null && !this.texture.IsCreated())
  44. {
  45. this.texture.Create();
  46. }
  47. this.screenPad.localScale = new Vector3(this.mainCamera.aspect, 1f, 1f) * base.GetComponent<Camera>().orthographicSize * 2f;
  48. this.mainCamera.targetTexture = this.texture;
  49. this.screenPad.GetComponent<Renderer>().sharedMaterial.SetTexture("_MainTex", this.texture);
  50. }
  51. public Transform screenPad;
  52. private Camera mainCamera;
  53. public RenderTexture texture;
  54. private bool tempTexture;
  55. }
  56. }