using MEC;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EyeSightCanvas : MonoBehaviour
{
const string TAG = "eyeSight";
public static EyeSightCanvas Instance;
public bool m_IsDebug;
/// <summary>
/// 相机
/// </summary>
public Transform m_eye;
/// <summary>
/// 进度背景图
/// </summary>
public Image m_progressImage;
/// <summary>
/// 进度百分比
/// </summary>
public Text m_progressPercent;
public int Count => _infoList.Count;
private Ray _ray;
private RaycastHit _hit;
private float _time = 0;
private bool _isEye = false;
public int _lookDis = 10;
public float checkTime=2f;
private EyeInfo _curEyeOBJ = null;
public List<EyeInfo> _infoList = new List<EyeInfo>();
private Action endEvent;
private bool _IsShow;
public GameObject InfoPanel;
public bool IsShow
{
get { return _IsShow; }
set { _IsShow = value; gameObject.SetActive(value); }
}
public void AddCheck(EyeInfo e)
{
m_progressImage.transform.parent.gameObject.SetActive(true);
_infoList.Add(e);
m_progressPercent.gameObject.SetActive(false);
m_progressImage.gameObject.SetActive(false);
m_progressImage.fillAmount = 0;
m_progressPercent.text = "0%";
}
public void RemoveCheck(string info)
{
var eyeinfo = _infoList.Find(t => t.CheckName == info);
if (eyeinfo != null)
{
_infoList.Remove(eyeinfo);
}
}
public void RemoveAllCheck()
{
_infoList.Clear();
}
public void SetEndEvent(Action end)
{
endEvent = end;
}
private void Awake()
{
Instance = this;
}
private void Update()
{
//射线检查
_ray = new Ray(m_eye.position, m_eye.forward);
//if (Physics.Raycast(_ray, out _hit, _lookDis, 1<<LayerMask.NameToLayer("EyeCheck")))
//{
// if (m_IsDebug) Debug.DrawRay(m_eye.position, m_eye.forward * 2, Color.green);
// if (!_isEye)
// {
// // _curEyeOBJ = _infoList.Find(t => t.CheckName == _hit.collider.name);
// Debug.Log("1111111111111111111");
// _curEyeOBJ = _hit.transform.GetComponent<EyeInfo>();
// if (_curEyeOBJ != null)
// {
// Timing.RunCoroutineSingleton(CheckCountDown(), TAG, SingletonBehavior.Overwrite);
// _isEye = true;
// }
// }
//}
//else
//{
// Debug.Log("2222222222222222222");
// if (m_IsDebug) Debug.DrawRay(m_eye.position, m_eye.forward * 5, Color.red);
// if (_isEye)
// {
// Clear();
// }
//}
if (Physics.Raycast(_ray, out _hit, _lookDis))
{
if (m_IsDebug) Debug.DrawRay(m_eye.position, m_eye.forward * 2, Color.green);
// _curEyeOBJ = _infoList.Find(t => t.CheckName == _hit.collider.name);
if (_hit.transform.gameObject.layer == LayerMask.NameToLayer("EyeCheck"))
{
if (!_isEye)
{
_curEyeOBJ = _hit.transform.GetComponent<EyeInfo>();
if (_curEyeOBJ != null&&_curEyeOBJ.IsNeedCheck)
{
Timing.RunCoroutineSingleton(CheckCountDown(), TAG, SingletonBehavior.Overwrite);
_isEye = true;
}
}
}
else
{
if (m_IsDebug) Debug.DrawRay(m_eye.position, m_eye.forward * 5, Color.red);
if (_isEye)
{
Clear();
}
}
}
else
{
if (m_IsDebug) Debug.DrawRay(m_eye.position, m_eye.forward * 5, Color.red);
if (_isEye)
{
Clear();
}
}
//射线检查
//_ray = new Ray(m_eye.transform.position, m_eye.transform.forward);
//Debug.DrawRay(m_eye.transform.position, m_eye.transform.forward * 2, Color.red);
//if (Physics.Raycast(_ray, out _hit, 500f, 1 << LayerMask.NameToLayer("EyeCheck")))
//{
// Debug.DrawRay(m_eye.transform.position, m_eye.transform.forward * 2, Color.green);
// Debug.Log(_hit.transform.name);
//}
}
private void Clear()
{
m_progressPercent.gameObject.SetActive(false);
m_progressImage.gameObject.SetActive(false);
Timing.KillCoroutines(TAG);
_time = 0;
_isEye = false;
_curEyeOBJ = null;
m_progressPercent.text = "0%";
}
private void Close()
{
m_progressImage.transform.parent.gameObject.SetActive(false);
}
/// <summary>
/// 检查进度计数
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private IEnumerator<float> CheckCountDown()
{
m_progressPercent.gameObject.SetActive(true);
m_progressImage.gameObject.SetActive(true);
while (_curEyeOBJ != null && _time < checkTime)
{
m_progressPercent.text = (_time / checkTime * 100).ToString("f0") + "%";
m_progressImage.fillAmount = _time / checkTime;
_time += Time.deltaTime;
yield return Timing.WaitForOneFrame;
}
if (_curEyeOBJ != null)
{
Debug.Log("-----------巡检完成--------");
for (int i = 0; i < _infoList.Count; i++)
{
_infoList[i].SetPanelFalse();
}
_curEyeOBJ.FinishYunJian(InfoPanel);
// _infoList.Remove(_curEyeOBJ);
if (_infoList.Count == 0)
{
Close();
endEvent?.Invoke();
}
Clear();
}
}
}
Unity注视时间进度百分比制作
最新推荐文章于 2024-07-11 14:34:54 发布