Billboard.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public class Billboard : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. base.StartCoroutine(this.CoGetTargetCam());
  9. }
  10. private IEnumerator CoGetTargetCam()
  11. {
  12. while (this.m_targetCamera == null)
  13. {
  14. if (GameMain.Instance.VRMode && Application.isPlaying)
  15. {
  16. if (GameMain.Instance.OvrMgr != null)
  17. {
  18. this.m_targetCamera = GameMain.Instance.OvrMgr.EyeAnchor;
  19. }
  20. }
  21. else if (GameMain.Instance.MainCamera != null)
  22. {
  23. this.m_targetCamera = GameMain.Instance.MainCamera.transform;
  24. }
  25. yield return null;
  26. }
  27. this.m_bEnableTargetCam = true;
  28. yield break;
  29. }
  30. private void Update()
  31. {
  32. if (!this.m_bEnableTargetCam)
  33. {
  34. return;
  35. }
  36. Vector3 position = this.m_targetCamera.position;
  37. if (this.m_eLockLocal != Billboard.LOCK.NON)
  38. {
  39. Vector3 a = base.transform.InverseTransformPoint(position);
  40. if (this.m_eLockLocal == Billboard.LOCK.LOCK_X)
  41. {
  42. a.x = base.transform.localPosition.x;
  43. }
  44. else if (this.m_eLockLocal == Billboard.LOCK.LOCK_Y)
  45. {
  46. a.y = base.transform.localPosition.y;
  47. }
  48. else if (this.m_eLockLocal == Billboard.LOCK.LOCK_Z)
  49. {
  50. a.z = base.transform.localPosition.z;
  51. }
  52. Vector3 normalized = (a - base.transform.localPosition).normalized;
  53. base.transform.localRotation *= Quaternion.LookRotation(normalized);
  54. }
  55. else if (this.m_bUseUpVector)
  56. {
  57. base.transform.LookAt(position, this.m_vUpVector);
  58. }
  59. else
  60. {
  61. base.transform.LookAt(position);
  62. }
  63. }
  64. public Transform m_targetCamera;
  65. [Header("Up方向を固定する(World系)")]
  66. public bool m_bUseUpVector;
  67. public Vector3 m_vUpVector = new Vector3(0f, 1f, 0f);
  68. [Header("回転軸を指定する(Local系)")]
  69. public Billboard.LOCK m_eLockLocal;
  70. private bool m_bEnableTargetCam;
  71. public enum LOCK
  72. {
  73. NON,
  74. LOCK_X,
  75. LOCK_Y,
  76. LOCK_Z
  77. }
  78. }