123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class CameraMove : MonoBehaviour, IScrollHandler
- {
- // Start is called before the first frame update
- int speeed = 1;
- Vector3 camAng;
- float camHeight = 2.5f;
- bool IsMouse = false;
- ObjectPosition[] OpjectList = new ObjectPosition[6];
- public bool IsTimeline = true;
- bool istrue = false;
- int upperLimit = 60;
- int lowerLimit = 20;
- private void Awake()
- {
- //消息监听
- MessageManager.Instance.OnAddlisten(5001, ReadPosInfo);
- MessageManager.Instance.OnAddlisten((int)MessageId.MouseIsActive, ShowMouse);
- }
- private void ShowMouse(object obj)
- {
- object[] oc = obj as object[];
- IsMouse = (bool)oc[0];
- ShowMouse(IsMouse);
- }
- private void ReadPosInfo(object obj)
- {
- object[] oc = obj as object[];
- int index = (int)oc[0];
- //CommonDefine.Instance.OpjectList[index] = oc[1] as ObjectPosition;
- Debug.Log("正在加载存档位置");
- if (CameraLogin.Instance.OpjectList[index] != null)
- {
- this.transform.position = new Vector3(CameraLogin.Instance.OpjectList[index].PositionX, CameraLogin.Instance.OpjectList[index].PositionY, CameraLogin.Instance.OpjectList[index].PositionZ);
- this.transform.eulerAngles = new Vector3(CameraLogin.Instance.OpjectList[index].RotationX, CameraLogin.Instance.OpjectList[index].RotationY, CameraLogin.Instance.OpjectList[index].RotationZ);
- camAng.x = CameraLogin.Instance.OpjectList[index].RotationX;
- camAng.y = CameraLogin.Instance.OpjectList[index].RotationY;
- }
- }
- void Start()
- {
- ShowMouse(IsMouse);
- }
- // Update is called once per frame
- void Update()
- {
- GameObjectMove();
- GameObjectRotate();
- GameObjectState();
- }
- #region 移动
- void GameObjectMove()
- {
- if (CommonDefine.Instance.IsGuide || UINavigation.PopHistory.Count > 0)
- {
- ShowMouse(true);
- return;
- }
- //if (IsTimeline) return;
- if (Input.GetKeyDown(KeyCode.Q))
- {
- IsMouse = !IsMouse;
- ShowMouse(IsMouse);
- }
- istrue = CommonDefine.Instance.GetViewIsOpen();
- if (IsMouse || !istrue)
- {
- return;
- }
- if (Input.GetKey(KeyCode.D))
- {
- transform.Translate(100 * Time.deltaTime * speeed, 0, 0);
- }
- if (Input.GetKey(KeyCode.A))
- {
- transform.Translate(-100 * Time.deltaTime * speeed, 0, 0);
- }
- if (Input.GetKey(KeyCode.W))
- {
- transform.Translate(0, 0, 100 * Time.deltaTime * speeed);
- }
- if (Input.GetKey(KeyCode.S))
- {
- transform.Translate(0, 0, -100 * Time.deltaTime * speeed);
- }
- if (Input.GetKey(KeyCode.LeftShift))
- {
- transform.Translate(0, -100 * Time.deltaTime * speeed, 0);
- }
- if (Input.GetKey(KeyCode.Space))
- {
- transform.Translate(0, 100 * Time.deltaTime * speeed, 0);
- }
- }
- void GameObjectRotate()
- {
- istrue = CommonDefine.Instance.GetViewIsOpen();
- if (IsMouse || !istrue)
- {
- return;
- }
- float y = Input.GetAxis("Mouse X");
- float x = Input.GetAxis("Mouse Y");
- camAng.y += y;
- if (camAng.x <= 50 && camAng.x >= -90)
- {
- if (camAng.x - x > 50)
- {
- camAng.x = 50;
- }
- else if (camAng.x - x < -90)
- {
- camAng.x = -90;
- }
- else
- {
- camAng.x -= x;
- }
- this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
- float camy = camAng.y;
- this.transform.eulerAngles = new Vector3(camAng.x, camy, this.transform.eulerAngles.z);
- Vector3 startPos = transform.position;
- startPos.y += camHeight;
- //theHead.position = startPos;
- }
- else
- {
- camAng.x = camAng.x - 360;
- }
- }
- #endregion
- void GameObjectState()
- {
- //if (Input.GetKeyDown(KeyCode.Space))
- //{
- // //这个地方可以写“再按一次显示上帝视角”的提示
- // StartCoroutine(resetTimes());
- //}
- }
- public void ShowMouse(bool isShow)
- {
- if (isShow)
- {
- Cursor.lockState = CursorLockMode.None;
- }
- else
- {
- Cursor.lockState = CursorLockMode.Locked;
- }
- }
- public void OnScroll(PointerEventData eventData)
- {
- float value = eventData.scrollDelta.y;
- if (lowerLimit <= this.GetComponent<Camera>().fieldOfView && this.GetComponent<Camera>().fieldOfView <= upperLimit)
- {
- this.GetComponent<Camera>().fieldOfView += value;
- if (this.GetComponent<Camera>().fieldOfView > upperLimit)
- {
- this.GetComponent<Camera>().fieldOfView = upperLimit;
- }
- else if (lowerLimit > this.GetComponent<Camera>().fieldOfView)
- {
- this.GetComponent<Camera>().fieldOfView = lowerLimit;
- }
- }
- }
- //void ReadPosInfo(int index)
- //{
- // Debug.Log("正在加载存档位置");
- // if (OpjectList[index] != null)
- // {
- // this.transform.position = new Vector3(OpjectList[index].PositionX, OpjectList[index].PositionY, OpjectList[index].PositionZ);
- // this.transform.eulerAngles = new Vector3(OpjectList[index].RotationX, OpjectList[index].RotationY, OpjectList[index].RotationZ);
- // }
- //}
- }
|