PostEffectHelper.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class PostEffectHelper {
  5. public static RawImage renderTargetRawImage = null;
  6. public static int renderTextureWidth = 640;
  7. public static Material material;
  8. static RenderTexture sharedRenderTexture;
  9. static int sharedRenderTextureRefCount;
  10. public static void RefSharedRenderTexture(float aspect = 0)
  11. {
  12. sharedRenderTextureRefCount++;
  13. if (sharedRenderTexture == null)
  14. {
  15. if (aspect == 0)
  16. aspect = Screen.width * 1f / Screen.height;
  17. int width = renderTextureWidth > Screen.width ? Screen.width : renderTextureWidth;
  18. int height = Mathf.RoundToInt(width / aspect);
  19. sharedRenderTexture = new RenderTexture(width, height, 16);
  20. sharedRenderTexture.hideFlags = HideFlags.DontSave;
  21. if (renderTargetRawImage != null)
  22. {
  23. renderTargetRawImage.texture = sharedRenderTexture;
  24. renderTargetRawImage.material = material;
  25. renderTargetRawImage.gameObject.SetActive(true);
  26. }
  27. }
  28. }
  29. public static void UnRefSharedRenderTexture()
  30. {
  31. sharedRenderTextureRefCount--;
  32. if (sharedRenderTextureRefCount <= 0)
  33. {
  34. if (renderTargetRawImage != null)
  35. {
  36. renderTargetRawImage.texture = null;
  37. renderTargetRawImage.material = null;
  38. renderTargetRawImage.gameObject.SetActive(false);
  39. }
  40. GameObject.DestroyImmediate(sharedRenderTexture);
  41. sharedRenderTexture = null;
  42. }
  43. }
  44. public static RenderTexture SharedRenderTexture
  45. {
  46. get { return sharedRenderTexture; }
  47. }
  48. }