using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class FourthGCamCtrl : MonoBehaviour { public Vector3 QuaterView = Vector3.zero; public float InitY; private static FourthGCamCtrl instance; private Camera cam; public static FourthGCamCtrl Instance { get { return instance; } } void Awake() { instance = this; QuaterView = transform.eulerAngles; //记录俯视视角 InitY = transform.position.y; cam = this.GetComponent(); } private Transform target; private Vector3 preTargetPosition; private bool lookOver = false; private float height = 3; //查看人员模式下,摄像机高度 private float distance = 2; //查看人员模式下,人与摄像机之间的距离 private float dampTrace = 20.0f; //查看人员模式下,摄像机平滑追踪的变量 private float lookingAtHeight = 3.5f; private float lookingAtDistance = 5; private bool isQuading = false; // Use this for initialization void Start () { MessageDispatcher.AddListener("OpenFourthG", EnableCamera); MessageDispatcher.AddListener("QuadrupleMode", DisableCamera); MessageDispatcher.AddListener("4GNobodySelected", NobodySelected); MessageDispatcher.AddListener("4GLookOver", LookOverTarget); MessageDispatcher.AddListener("4GCancelLookOver", CancelLookOver); } void OnEnable() { /*MessageDispatcher.AddListener("OpenFourthG", EnableCamera); MessageDispatcher.AddListener("QuadrupleMode", DisableCamera); MessageDispatcher.AddListener("4GNobodySelected", NobodySelected); MessageDispatcher.AddListener("4GLookOver", LookOverTarget); MessageDispatcher.AddListener("4GCancelLookOver", CancelLookOver);*/ } void OnDisable() { /*MessageDispatcher.RemoveListener("OpenFourthG", EnableCamera); MessageDispatcher.RemoveListener("QuadrupleMode", DisableCamera); MessageDispatcher.RemoveListener("4GNobodySelected", NobodySelected); MessageDispatcher.RemoveListener("4GLookOver", LookOverTarget); MessageDispatcher.RemoveListener("4GCancelLookOver", CancelLookOver);*/ } void OnDestroy() { MessageDispatcher.RemoveListener("OpenFourthG", EnableCamera); MessageDispatcher.RemoveListener("QuadrupleMode", DisableCamera); MessageDispatcher.RemoveListener("4GNobodySelected", NobodySelected); MessageDispatcher.RemoveListener("4GLookOver", LookOverTarget); MessageDispatcher.RemoveListener("4GCancelLookOver", CancelLookOver); } /// /// 开启4G图传时才激活此相机 /// /// void EnableCamera(IMessage obj) { var value = (bool)obj.Data; //如果打开了4G图传,又处于四分屏模式,则此相机禁用 if(value ==true && isQuading == true) { this.enabled = false; cam.enabled = false; } //如果打开了4G图传,未处于四分屏模式,则此相机激活 else if(value==true && isQuading == false) { this.enabled = true; cam.enabled = true; } //如果关闭了4G图传,则禁用此相机 else { this.enabled = false; cam.enabled = false; } } /// /// 开启四分屏模式时禁用此相机 /// /// void DisableCamera(IMessage obj) { var value = (bool)obj.Data; isQuading = value; this.enabled = !value; cam.enabled = !value; } // Update is called once per frame void Update () { //有目标人员并且相机激活时才执行 if (target != null && cam.enabled==true) { if (!lookOver) { transform.position = new Vector3(target.position.x, transform.position.y, target.position.z); } else { //相机的位置,在人员胸前 transform.position = Vector3.Lerp(transform.position, target.position + (target.forward * distance) + (Vector3.up * height), Time.deltaTime * dampTrace); //transform.LookAt(target.position); transform.LookAt(target.position + (target.forward * lookingAtDistance) + (Vector3.up * lookingAtHeight)); } } } /// /// 查看人员视角 /// /// private void LookOverTarget(IMessage obj) { var targetInfo = (CloneGameObjInfo)obj.Data; target = EntitiesManager.Instance.GetEntityByID(targetInfo.gameObjID).transform; GetComponent().orthographic = false; //改为透视视角 lookOver = true; } /// /// 取消查看人员视角 /// /// private void CancelLookOver(IMessage obj) { //恢复到俯视角度 transform.eulerAngles = QuaterView; transform.position = new Vector3(transform.position.x, InitY, transform.position.z); GetComponent().orthographic = true; //改回正交视角 lookOver = false; } /// /// 在4G图传中有人员被选中,相机跟随该人员 /// /// public void SetTarget(Transform tar) { target = tar; } /// /// 没有人员被选中,取消地图相机跟随 /// /// void NobodySelected(IMessage obj) { //preTargetPosition = target.position; target = null; } }