CameraImageMirror.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5. public class CameraImageMirror : MonoBehaviour
  6. {
  7. static Material m_Material = null;
  8. static Material material
  9. {
  10. get
  11. {
  12. if (m_Material == null)
  13. {
  14. m_Material = new Material(Shader.Find("Custom/MirrorImage"));
  15. m_Material.hideFlags = HideFlags.DontSave;
  16. }
  17. return m_Material;
  18. }
  19. }
  20. // --------------------------------------------------------
  21. Camera c;
  22. bool hasRenderTextureRef = false;
  23. // --------------------------------------------------------
  24. protected void Start()
  25. {
  26. // Disable if we don't support image effects
  27. if (!SystemInfo.supportsImageEffects)
  28. {
  29. enabled = false;
  30. return;
  31. }
  32. // Disable if the shader can't run on the users graphics card
  33. if (material.shader == null || !material.shader.isSupported)
  34. {
  35. enabled = false;
  36. return;
  37. }
  38. if (c == null)
  39. c = GetComponent<Camera>();
  40. }
  41. void OnEnable()
  42. {
  43. if (c == null)
  44. c = GetComponent<Camera>();
  45. if (c != null)
  46. {
  47. PostEffectHelper.material = material;
  48. PostEffectHelper.RefSharedRenderTexture(c.aspect);
  49. hasRenderTextureRef = true;
  50. c.targetTexture = PostEffectHelper.SharedRenderTexture;
  51. }
  52. }
  53. protected void OnDisable()
  54. {
  55. if (c.targetTexture != null)
  56. c.targetTexture = null;
  57. if (hasRenderTextureRef)
  58. PostEffectHelper.UnRefSharedRenderTexture();
  59. }
  60. }