Files
2025-01-25 04:38:09 +08:00

168 lines
4.0 KiB
C#

using System;
using UnityEngine;
namespace Thousandto.Launcher.Form
{
/// <summary>
/// 假进度处理
/// </summary>
public class UIFakeLoading
{
private UILoadingBasePanel _panel;
//步骤数组
private string[] _stepNames = null;
//最大的步骤
private int _maxStep = 0;
//当前步骤
private int _curentStep = -1;
//当前值
private float _currentValue = 0f;
//最后的一个整数值
private int _lastIntValue = 0;
//最大的值
private float _maxValue = 0.95f;
//是否运行
private bool _isRunning = false;
//是否启动
private bool _enabled = false;
public bool Enabled {
get
{
return _enabled;
}
set
{
if (_enabled != value)
{
_enabled = value;
//Debug.LogError("FakeLoading::::Enable=" + _enabled);
}
}
}
public int MaxStep
{
get
{
return _maxStep;
}
}
public string[] StepNames
{
get { return _stepNames; }
set
{
if (value != null)
{
_stepNames = value;
}
}
}
public UIFakeLoading(UILoadingBasePanel panel)
{
_panel = panel;
}
public void Start(bool isReset)
{
//Debug.LogError("Start:::::::" + isReset);
_isRunning = true;
if (isReset)
{
Reset();
}
}
public void Stop(bool isReset = false)
{
//Debug.LogError("Stop:::::::" + isReset);
_isRunning = false;
if (isReset)
{
Reset();
}
}
//设置最大步骤
public void SetMaxStep(int max)
{
_maxStep = max;
}
public void Reset()
{
_curentStep = -1;
_lastIntValue = -1;
_currentValue = 0;
_panel.Reset();
}
//设置步骤
public void SetStep(int step)
{
if (_curentStep != step)
{
_curentStep = step;
if (step >= _maxStep)
{
_curentStep = _maxStep - 1;
}
if (_curentStep >= 0)
{
_maxValue = MaxValue();
if (_stepNames != null && _stepNames.Length > _curentStep)
{
_panel.SetStepName(_stepNames[_curentStep]);
}
_panel.SetProgressMinToMax(_curentStep + 1, _maxStep);
}
else
{
_panel.SetStepName("");
_panel.SetProgressMinToMax(0,0);
}
_panel.Refresh();
}
}
//更新
public void Update(float deltaTime)
{
if (_enabled && _isRunning)
{
if (_currentValue < _maxValue)
{
_currentValue += deltaTime * Speed();
}
_panel.SetProgress(_currentValue);
_panel.Refresh();
}
}
//计算最大值
private float MaxValue()
{
if (_maxStep > 0 && _curentStep >=0)
{
return (float)(_curentStep+1) / (float)_maxStep * 0.05f + 0.95f;
}
return 1f;
}
//获取进度速度
private float Speed()
{
//当速度越靠近最大值, 就越慢.
return (_maxValue - _currentValue) * 0.2f + 0.15f;
}
}
}