Unity注视时间进度百分比制作

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();
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值