| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- /* 准心对象 */
- public class CrossHair : MonoBehaviour
- {
- public static CrossHair ins;
- private Image image = null;
- private bool open = false;
- private bool visiable = false;
- private bool onlyShow = true;
- private GameObject infraredGuiderObj;
- void Awake()
- {
- ins = this;
- }
- void OnDestroy()
- {
- if (ins == this) ins = null;
- }
- void Start()
- {
- this.image = this.GetComponent<Image>();
- // this.open = UserSettings.ins.openCrossHair;
- this.visiable = this.image.enabled;
- //this.SetVisiable(false);
- SetOpen(UserSettings.ins.openCrossHair);
- //如果是靶子场景,把准心改为黑色
- if (SceneManager.GetActiveScene().name.Equals("Game")) {
- this.image.color = Color.black;
- infraredGuiderObj = FindObjectOfType<InfraredGuider>()?.gameObject;
- }
- }
- void Update()
- {
- //如果是新手指引的情况,或者校准时候,一定
- //if (infraredGuiderObj != null && infraredGuiderObj.activeSelf == true || AutoResetView.ins != null)
- //{
- // SetVisiable(true);
- //}
- //else {
- //按照条件显示图标
- if (open) SetVisiable(onlyShow && ArmBow.ins && ArmBow.ins.IsCanShoot());
- //}
- }
- void SetVisiable(bool value)
- {
- if (this.visiable == value) return;
- this.visiable = value;
- this.GetComponent<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;
- if (!this.onlyShow)
- {
- SetVisiable(false);
- }
- }
- public bool GetOnlyShow()
- {
- return this.onlyShow;
- }
- public RaycastHit GetRaycastHit() {
- float maxDistance = 100;
- int layerMask = 1 << 8; //TargetLayerMask
- RaycastHit raycastHit;
- Ray ray;
- if (Camera.main.name.EndsWith("Fixed")) {
- ray = new Ray(BowCamera.ins.transform.position, BowCamera.ins.transform.forward);
- } else {
- ray = Camera.main.ScreenPointToRay(this.transform.position, Camera.MonoOrStereoscopicEye.Mono);
- }
- Physics.Raycast(ray.origin, ray.direction, out raycastHit, maxDistance, layerMask);
- return raycastHit;
- }
- #region 固定相机更新
- RectTransform _parentRTF;
- RectTransform parentRTF {
- get {
- if (!_parentRTF) {
- _parentRTF = transform.parent.GetComponent<RectTransform>();
- }
- return _parentRTF;
- }
- }
- Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
- Vector3 targetPosition;
- public void UpdatePostionWhenFixedCamera() {
- if (!visiable) return;
- Transform transform = BowCamera.ins.transform;
- Ray ray = new Ray(transform.position, transform.forward);
- RaycastHit hitInfo;
- if (Physics.Raycast(ray, out hitInfo, 100)) {
- targetPosition = hitInfo.point;
- } else {
- targetPosition = transform.position + transform.forward * 100f;
- }
- Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
- v3.x *= parentRTF.rect.width;
- v3.y *= parentRTF.rect.height;
- v3.z = 0;
- this.transform.localPosition = v3;
- action_UpdatePostionWhenFixedCamera?.Invoke();
- }
- public System.Action action_UpdatePostionWhenFixedCamera;
- #endregion
- }
|