UI_ScreenFitBase.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. public abstract class UI_ScreenFitBase : MonoBehaviour
  5. {
  6. protected static Camera m_NGUICamera
  7. {
  8. get
  9. {
  10. if (!UI_ScreenFitBase.m_UseNGUICam)
  11. {
  12. GameObject gameObject = GameObject.Find("UI Root/Camera");
  13. if (gameObject)
  14. {
  15. UI_ScreenFitBase.m_UseNGUICam = gameObject.GetComponent<Camera>();
  16. }
  17. else
  18. {
  19. UI_ScreenFitBase.m_UseNGUICam = UICamera.mainCamera;
  20. }
  21. }
  22. return UI_ScreenFitBase.m_UseNGUICam;
  23. }
  24. set
  25. {
  26. UI_ScreenFitBase.m_UseNGUICam = value;
  27. }
  28. }
  29. protected virtual void Start()
  30. {
  31. if (!UI_ScreenFitBase.m_NGUICamera)
  32. {
  33. GameObject gameObject = GameObject.Find("UI Root/Camera");
  34. if (gameObject)
  35. {
  36. UI_ScreenFitBase.m_NGUICamera = gameObject.GetComponent<Camera>();
  37. }
  38. else
  39. {
  40. UI_ScreenFitBase.m_NGUICamera = UICamera.mainCamera;
  41. }
  42. }
  43. if (!UI_ScreenFitBase.m_NGUIRoot)
  44. {
  45. GameObject gameObject2 = GameObject.Find("UI Root");
  46. if (gameObject2)
  47. {
  48. UI_ScreenFitBase.m_NGUIRoot = gameObject2.GetComponent<UIRoot>();
  49. }
  50. }
  51. if (Application.isPlaying && (GameMain.Instance.VRMode || this.m_CheckFirstOnly))
  52. {
  53. if (this.m_CheckFirstOnly)
  54. {
  55. this.FitAction();
  56. }
  57. base.enabled = this.m_AlwaysActive;
  58. }
  59. }
  60. protected virtual void Update()
  61. {
  62. if (Application.isPlaying && GameMain.Instance.VRMode && !this.m_AlwaysActive)
  63. {
  64. return;
  65. }
  66. this.FitAction();
  67. }
  68. protected abstract void FitAction();
  69. public static Vector3 PointToScreenPos(Vector3 point)
  70. {
  71. Vector3 position = UI_ScreenFitBase.m_NGUICamera.ScreenToWorldPoint(point);
  72. Vector3 result = UI_ScreenFitBase.m_NGUICamera.transform.parent.InverseTransformPoint(position);
  73. result.z = 0f;
  74. return result;
  75. }
  76. protected float ScreenRate(float Min = 0f, float Max = 1f, float Rate = 1f)
  77. {
  78. return Mathf.Clamp((float)Screen.width / (float)Screen.height * Rate, Min, Max);
  79. }
  80. private static Camera m_UseNGUICam;
  81. protected static UIRoot m_NGUIRoot;
  82. [SerializeField]
  83. [Header("解像度に合わせた処理を最初だけ行う")]
  84. protected bool m_CheckFirstOnly;
  85. [SerializeField]
  86. [Header("VR時でも常にアクティブか")]
  87. protected bool m_AlwaysActive;
  88. }