CameraManagerAdon.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Adon.Game.Helper;
  2. using Cinemachine;
  3. using Cinemachine.PostFX;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. //using UnityEngine.Rendering.PostProcessing;
  8. namespace AdonGameKit
  9. {
  10. public class CameraManagerAdon : MonoBehaviour
  11. {
  12. public Camera m_MainCamera;
  13. public Camera m_CharacterCamera;
  14. public Transform m_Hero;
  15. public Transform m_Timeline_CM_Respawn;//出生相机Timeline对象
  16. public CinemachineFreeLook m_CM_ThirdPerson;//第三人称相机
  17. public CinemachineVirtualCamera m_CM_TopDown;//俯视相机
  18. public CinemachineVirtualCamera m_CM_Win;//胜利相机
  19. public CinemachineVirtualCameraBase m_CM_Active;//当前活跃相机
  20. public CinemachineTargetGroup m_CinemachineTargetGroup;
  21. public GameObject LockTarget;
  22. public float m_Angle;
  23. public float m_Sight;
  24. public bool IsCensorCameraAngle;
  25. //static private CameraManagerAdon m_Instance;
  26. //static public CameraManagerAdon Instance
  27. //{
  28. // get
  29. // {
  30. // return m_Instance;
  31. // }
  32. //}
  33. private void Awake()
  34. {
  35. m_MainCamera = Camera.main;
  36. m_Timeline_CM_Respawn = transform.Find("CMStateDrivenCamera/Timeline_CM_Respawn");
  37. m_CM_ThirdPerson = transform.Find("CMStateDrivenCamera/CM_ThirdPerson").GetComponent<CinemachineFreeLook>();
  38. m_CM_TopDown = transform.Find("CMStateDrivenCamera/CM_TopDown").GetComponent<CinemachineVirtualCamera>();
  39. m_CM_Win = transform.Find("CMStateDrivenCamera/CM_Win").GetComponent<CinemachineVirtualCamera>();
  40. m_CM_Active = m_CM_ThirdPerson;
  41. m_CinemachineTargetGroup = transform.Find("TargetGroup").GetComponent<CinemachineTargetGroup>();
  42. m_Hero = m_CinemachineTargetGroup.m_Targets[0].target;
  43. }
  44. void Start()
  45. {
  46. }
  47. // Update is called once per frame
  48. void Update()
  49. {
  50. /////更新锁定相机
  51. /////根据相机与玩家方向,计算与锁定目标对角度,大于指定角度则旋转相机到差值
  52. //Vector3 dir = m_MainCamera.transform.position - m_Hero.transform.position;//相机与玩家方向
  53. ////计算怪角度偏移
  54. //float HitAngle = PlayerHelperAdon.Angle_360(dir, LockTarget.transform.position);
  55. //Debug.Log("相机与目标角度"+ HitAngle);
  56. //if (HitAngle > 270)
  57. //{
  58. // CM_ThirdPerson.m_XAxis.Value = HitAngle - 270;
  59. //}
  60. }
  61. ///// <summary>
  62. ///// 设置锁定目标
  63. ///// </summary>
  64. ///// <param name="_LockTarget"></param>
  65. //public void SetLockTarget(GameObject _LockTarget)
  66. //{
  67. // LockTarget = _LockTarget;
  68. // m_CinemachineTargetGroup.m_Targets[1].target = LockTarget.transform;
  69. // m_CinemachineTargetGroup.m_Targets[1].weight = 1;
  70. //}
  71. ///// <summary>
  72. ///// 释放目标
  73. ///// </summary>
  74. //public void ReleaseTarget()
  75. //{
  76. // LockTarget = null;
  77. // m_CinemachineTargetGroup.m_Targets[1].target = null;
  78. // m_CinemachineTargetGroup.m_Targets[1].weight = 0;
  79. //}
  80. /// <summary>
  81. /// 播放极限闪避效果
  82. /// </summary>
  83. /// <param name="_color"></param>
  84. public void PlyeExterme()
  85. {
  86. //m_MainCamera.GetComponent<AmplifyColorEffect>().enabled = true;
  87. }
  88. public void StopExterme()
  89. {
  90. //m_MainCamera.GetComponent<AmplifyColorEffect>().enabled = false;
  91. }
  92. /// <summary>
  93. /// 出生镜头切换到游戏镜头
  94. /// </summary>
  95. public void RespawnToPlay()
  96. {
  97. m_Timeline_CM_Respawn.gameObject.SetActive(false);
  98. //SwitchToCM_ThirdPerson();
  99. SwitchToCM_TopDown();
  100. }
  101. /// <summary>
  102. /// 切换到第三人称相机
  103. /// </summary>
  104. public void SwitchToCM_ThirdPerson()
  105. {
  106. m_CM_TopDown.gameObject.SetActive(false);
  107. m_CM_ThirdPerson.gameObject.SetActive(true);
  108. m_CM_Active = m_CM_ThirdPerson;
  109. }
  110. /// <summary>
  111. /// 切换到俯视相机
  112. /// </summary>
  113. public void SwitchToCM_TopDown()
  114. {
  115. m_CM_ThirdPerson.gameObject.SetActive(false);
  116. m_CM_TopDown.gameObject.SetActive(true);
  117. m_CM_Active = m_CM_TopDown;
  118. }
  119. /// <summary>
  120. /// 切换到胜利相机
  121. /// </summary>
  122. public void SwitchToCM_Win()
  123. {
  124. m_CM_ThirdPerson.gameObject.SetActive(false);
  125. m_CM_TopDown.gameObject.SetActive(false);
  126. m_CM_Win.gameObject.SetActive(true);
  127. //m_CM_Win.m_XAxis.Value = m_CM_Win.m_XAxis.Value + m_Hero.eulerAngles.y;
  128. m_CM_Active = m_CM_Win;
  129. }
  130. public void SwitchCameraState()
  131. {
  132. if(m_CM_Active == m_CM_TopDown)
  133. {
  134. SwitchToCM_ThirdPerson();
  135. }
  136. else if (m_CM_Active == m_CM_ThirdPerson)
  137. {
  138. SwitchToCM_TopDown();
  139. }
  140. }
  141. //void OnGUI()
  142. //{
  143. // //默认CullingMask=-1,即渲染任何层
  144. // if (GUI.Button(new Rect(10.0f, 10.0f, 200.0f, 45.0f), "CullingMask=-1"))
  145. // {
  146. // m_MainCamera.cullingMask = -1;
  147. // }
  148. // //不渲染任何层
  149. // if (GUI.Button(new Rect(10.0f, 60.0f, 200.0f, 45.0f), "CullingMask=0"))
  150. // {
  151. // m_MainCamera.cullingMask = 0;
  152. // }
  153. // //仅渲染第0层
  154. // if (GUI.Button(new Rect(10.0f, 110.0f, 200.0f, 45.0f), "CullingMask=1<<0"))
  155. // {
  156. // m_MainCamera.cullingMask = 1 << 0;
  157. // }
  158. // //仅渲染第8层
  159. // if (GUI.Button(new Rect(10.0f, 160.0f, 200.0f, 45.0f), "CullingMask=1<<8"))
  160. // {
  161. // m_MainCamera.cullingMask = 1 << 8;
  162. // }
  163. // //渲染第8层与第0层
  164. // if (GUI.Button(new Rect(10.0f, 210.0f, 200.0f, 45.0f), "CullingMask=0&&8"))
  165. // {
  166. // //注:不可大意写成camera.cullingMask = 1 << 8+1;或
  167. // //camera.cullingMask = 1+1<<8 ;因为根据运算符优先次序其分别等价于
  168. // //camera.cullingMask = 1 << (8+1)和camera.cullingMask = (1+1)<<8
  169. // m_MainCamera.cullingMask = (1 << 8) + 1;
  170. // }
  171. //}
  172. #if UNITY_EDITOR
  173. /// <summary>
  174. /// 绘制视野配置
  175. /// </summary>
  176. private void OnDrawGizmosSelected()
  177. {
  178. Vector3 forward = transform.forward;
  179. UnityEditor.Handles.color = new Color(1.0f, 0.0f, 0.0f, 0.1f);
  180. Vector3 rotatedForward = Quaternion.Euler(0, -m_Angle * 0.5f, 0) * transform.forward;
  181. UnityEditor.Handles.DrawSolidArc(transform.position, Vector3.up, rotatedForward, m_Angle, m_Sight);
  182. }
  183. #endif
  184. }
  185. }