CustomCameraStack.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Fort23.Mono;
  2. using Mono;
  3. using UnityEngine;
  4. using UnityEngine.Rendering.Universal;
  5. public class CustomCameraStack : MonoBehaviour
  6. {
  7. private UniversalAdditionalCameraData _cameraData;
  8. public Camera camera;
  9. public static CustomCameraStack currCustomCameraStack;
  10. private void OnEnable()
  11. {
  12. currCustomCameraStack = this;
  13. SetUICamera();
  14. camera = GetComponent<Camera>();
  15. }
  16. public void SetUICamera()
  17. {
  18. if (UIManager.Instance != null && UIManager.Instance.UICamera != null)
  19. {
  20. UniversalAdditionalCameraData cameraData = UIManager.Instance.UICamera.gameObject.GetComponent<UniversalAdditionalCameraData>();
  21. if (cameraData != null)
  22. {
  23. UIManager.Instance.CurrCustomCameraStack = this;
  24. cameraData.renderType = CameraRenderType.Overlay;
  25. }
  26. AddCamera(UIManager.Instance.UICamera);
  27. }
  28. }
  29. private void Update()
  30. {
  31. if (currCustomCameraStack == this && UIManager.Instance != null && UIManager.Instance.CurrCustomCameraStack == null)
  32. {
  33. SetUICamera();
  34. }
  35. }
  36. public void AddCamera(Camera camera)
  37. {
  38. if (_cameraData == null)
  39. {
  40. _cameraData = GetComponent<Camera>().GetUniversalAdditionalCameraData();
  41. }
  42. if (!_cameraData.cameraStack.Contains(camera))
  43. {
  44. _cameraData.cameraStack.Add(camera);
  45. }
  46. }
  47. public void RemoveCamera(Camera camera)
  48. {
  49. if (_cameraData == null)
  50. {
  51. _cameraData = GetComponent<Camera>().GetUniversalAdditionalCameraData();
  52. }
  53. if (_cameraData.cameraStack.Contains(camera))
  54. {
  55. _cameraData.cameraStack.Remove(camera);
  56. }
  57. }
  58. private void OnDisable()
  59. {
  60. if (_cameraData != null && _cameraData.cameraStack != null && _cameraData.cameraStack[^1] != null && UIManager.Instance.CurrCustomCameraStack == this)
  61. {
  62. UniversalAdditionalCameraData cameraData = _cameraData.cameraStack[^1].gameObject.GetComponent<UniversalAdditionalCameraData>();
  63. if (cameraData != null)
  64. {
  65. cameraData.renderType = CameraRenderType.Base;
  66. }
  67. UIManager.Instance.CurrCustomCameraStack = null;
  68. }
  69. _cameraData.cameraStack.Clear();
  70. }
  71. }