CameraMove.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class CameraMove : MonoBehaviour, IScrollHandler
  7. {
  8. // Start is called before the first frame update
  9. int speeed = 1;
  10. Vector3 camAng;
  11. float camHeight = 2.5f;
  12. bool IsMouse = false;
  13. ObjectPosition[] OpjectList = new ObjectPosition[6];
  14. public bool IsTimeline = true;
  15. bool istrue = false;
  16. int upperLimit = 60;
  17. int lowerLimit = 20;
  18. private void Awake()
  19. {
  20. //消息监听
  21. MessageManager.Instance.OnAddlisten(5001, ReadPosInfo);
  22. MessageManager.Instance.OnAddlisten((int)MessageId.MouseIsActive, ShowMouse);
  23. }
  24. private void ShowMouse(object obj)
  25. {
  26. object[] oc = obj as object[];
  27. IsMouse = (bool)oc[0];
  28. ShowMouse(IsMouse);
  29. }
  30. private void ReadPosInfo(object obj)
  31. {
  32. object[] oc = obj as object[];
  33. int index = (int)oc[0];
  34. //CommonDefine.Instance.OpjectList[index] = oc[1] as ObjectPosition;
  35. Debug.Log("正在加载存档位置");
  36. if (CameraLogin.Instance.OpjectList[index] != null)
  37. {
  38. this.transform.position = new Vector3(CameraLogin.Instance.OpjectList[index].PositionX, CameraLogin.Instance.OpjectList[index].PositionY, CameraLogin.Instance.OpjectList[index].PositionZ);
  39. this.transform.eulerAngles = new Vector3(CameraLogin.Instance.OpjectList[index].RotationX, CameraLogin.Instance.OpjectList[index].RotationY, CameraLogin.Instance.OpjectList[index].RotationZ);
  40. camAng.x = CameraLogin.Instance.OpjectList[index].RotationX;
  41. camAng.y = CameraLogin.Instance.OpjectList[index].RotationY;
  42. }
  43. }
  44. void Start()
  45. {
  46. ShowMouse(IsMouse);
  47. }
  48. // Update is called once per frame
  49. void Update()
  50. {
  51. GameObjectMove();
  52. GameObjectRotate();
  53. GameObjectState();
  54. }
  55. #region 移动
  56. void GameObjectMove()
  57. {
  58. if (CommonDefine.Instance.IsGuide || UINavigation.PopHistory.Count > 0)
  59. {
  60. ShowMouse(true);
  61. return;
  62. }
  63. //if (IsTimeline) return;
  64. if (Input.GetKeyDown(KeyCode.Q))
  65. {
  66. IsMouse = !IsMouse;
  67. ShowMouse(IsMouse);
  68. }
  69. istrue = CommonDefine.Instance.GetViewIsOpen();
  70. if (IsMouse || !istrue)
  71. {
  72. return;
  73. }
  74. if (Input.GetKey(KeyCode.D))
  75. {
  76. transform.Translate(100 * Time.deltaTime * speeed, 0, 0);
  77. }
  78. if (Input.GetKey(KeyCode.A))
  79. {
  80. transform.Translate(-100 * Time.deltaTime * speeed, 0, 0);
  81. }
  82. if (Input.GetKey(KeyCode.W))
  83. {
  84. transform.Translate(0, 0, 100 * Time.deltaTime * speeed);
  85. }
  86. if (Input.GetKey(KeyCode.S))
  87. {
  88. transform.Translate(0, 0, -100 * Time.deltaTime * speeed);
  89. }
  90. if (Input.GetKey(KeyCode.LeftShift))
  91. {
  92. transform.Translate(0, -100 * Time.deltaTime * speeed, 0);
  93. }
  94. if (Input.GetKey(KeyCode.Space))
  95. {
  96. transform.Translate(0, 100 * Time.deltaTime * speeed, 0);
  97. }
  98. }
  99. void GameObjectRotate()
  100. {
  101. istrue = CommonDefine.Instance.GetViewIsOpen();
  102. if (IsMouse || !istrue)
  103. {
  104. return;
  105. }
  106. float y = Input.GetAxis("Mouse X");
  107. float x = Input.GetAxis("Mouse Y");
  108. camAng.y += y;
  109. if (camAng.x <= 50 && camAng.x >= -90)
  110. {
  111. if (camAng.x - x > 50)
  112. {
  113. camAng.x = 50;
  114. }
  115. else if (camAng.x - x < -90)
  116. {
  117. camAng.x = -90;
  118. }
  119. else
  120. {
  121. camAng.x -= x;
  122. }
  123. this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
  124. float camy = camAng.y;
  125. this.transform.eulerAngles = new Vector3(camAng.x, camy, this.transform.eulerAngles.z);
  126. Vector3 startPos = transform.position;
  127. startPos.y += camHeight;
  128. //theHead.position = startPos;
  129. }
  130. else
  131. {
  132. camAng.x = camAng.x - 360;
  133. }
  134. }
  135. #endregion
  136. void GameObjectState()
  137. {
  138. //if (Input.GetKeyDown(KeyCode.Space))
  139. //{
  140. // //这个地方可以写“再按一次显示上帝视角”的提示
  141. // StartCoroutine(resetTimes());
  142. //}
  143. }
  144. public void ShowMouse(bool isShow)
  145. {
  146. if (isShow)
  147. {
  148. Cursor.lockState = CursorLockMode.None;
  149. }
  150. else
  151. {
  152. Cursor.lockState = CursorLockMode.Locked;
  153. }
  154. }
  155. public void OnScroll(PointerEventData eventData)
  156. {
  157. float value = eventData.scrollDelta.y;
  158. if (lowerLimit <= this.GetComponent<Camera>().fieldOfView && this.GetComponent<Camera>().fieldOfView <= upperLimit)
  159. {
  160. this.GetComponent<Camera>().fieldOfView += value;
  161. if (this.GetComponent<Camera>().fieldOfView > upperLimit)
  162. {
  163. this.GetComponent<Camera>().fieldOfView = upperLimit;
  164. }
  165. else if (lowerLimit > this.GetComponent<Camera>().fieldOfView)
  166. {
  167. this.GetComponent<Camera>().fieldOfView = lowerLimit;
  168. }
  169. }
  170. }
  171. //void ReadPosInfo(int index)
  172. //{
  173. // Debug.Log("正在加载存档位置");
  174. // if (OpjectList[index] != null)
  175. // {
  176. // this.transform.position = new Vector3(OpjectList[index].PositionX, OpjectList[index].PositionY, OpjectList[index].PositionZ);
  177. // this.transform.eulerAngles = new Vector3(OpjectList[index].RotationX, OpjectList[index].RotationY, OpjectList[index].RotationZ);
  178. // }
  179. //}
  180. }