using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; namespace DuckHunter { public class CrossHair : MonoBehaviour { public static CrossHair Instance; private bool open = false; private bool visiable = false; private bool onlyShow = true; private Image image = null; void Start() { Instance = this; InitRotateRangeVH(); image = transform.Find("Image").GetComponent(); //open = UserSettings.ins.openCrossHair; SetOpen(UserSettings.ins.openCrossHair); //visiable = image.enabled; visiable = open; image.enabled = open; UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked); GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow; CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load("Prefabs/CrossHairOutBoundChecker1")).GetComponent(); outBoundChecker.crossHair = transform as RectTransform; //读取准心值 if (AimHandler.ins && !AimHandler.ins.bRuning9Axis() && InfraredDemo._ins) SetOnlyShow(InfraredDemo._ins.getCrosshairValue() == 1); } void Update() { if (AutoResetView.ins != null) { //进入校准时候,一定显示准心 SetVisiable(true); } else { if (open) SetVisiable(onlyShow); else SetVisiable(false); } } void OnDestroy() { if (Instance == this) Instance = null; GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow; } void UpdateHideShow(bool mouseAwaked) { //transform.Find("Image").GetComponent().enabled = !mouseAwaked; SetOnlyShow(!mouseAwaked); } void SetVisiable(bool value) { if (this.visiable == value) return; this.visiable = value; this.image.enabled = this.visiable; } public void SetOpen(bool open) { this.open = open; if (!this.open) { SetVisiable(false); } } public bool GetOpen() { return this.open; } public void SetOnlyShow(bool onlyShow) { this.onlyShow = onlyShow; } public bool GetOnlyShow() { return this.onlyShow; } Vector2 mapPosition; float rotateRangeV; float rotateRangeH; void InitRotateRangeVH() { rotateRangeV = 25f; float halfScreenHeight = Screen.height * 0.5f; float halfScreenWidth = Screen.width * 0.5f; float halfRangeV = rotateRangeV * 0.5f; float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI); float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f; rotateRangeH = halfRangeH * 2f; } public void UpdatePositionByModuleRotation(Quaternion rotation) { Vector3 resEulerAngles = rotation.eulerAngles; resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2); resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2); mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width; mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height; transform.position = mapPosition; } public void Shoot() { if (GlobalData.MyDeviceMode == DeviceMode.Gun) { AudioManager.Instance.PlayGunShoot(null); } else { AudioManager.Instance.PlayShoot(null); } _PlayShootAnimation(); } Sequence _shootSequence; void _PlayShootAnimation() { if (_shootSequence != null) { _shootSequence.Kill(); _shootSequence = null; } _shootSequence = DOTween.Sequence(); _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f)); _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f)); } } }