340 lines
8.3 KiB
C#
340 lines
8.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
/*
|
|
//Set properties in C# example codes.
|
|
using RainbowArt.CleanFlatUI;
|
|
public class ProgressBarSpecialPatternAutoDemo : MonoBehaviour
|
|
{
|
|
//The ProgressBarSpecialPatternAuto Component.
|
|
public ProgressBarSpecialPatternAuto m_ProgressBar;
|
|
void Start()
|
|
{
|
|
//Set the minimum value.
|
|
m_ProgressBar.MinValue = 25;
|
|
//Set the maximum value.
|
|
m_ProgressBar.MaxValue = 100;
|
|
//Set whether to show the text.
|
|
m_ProgressBar.HasText = false;
|
|
//Set the speed of the current progress auto changing.
|
|
m_ProgressBar.LoadSpeed = 0.2f;
|
|
//Set whether the progress increasing or decreasing.
|
|
m_ProgressBar.Forward = true;
|
|
//Set whether to auto loop.
|
|
m_ProgressBar.Loop = true;
|
|
|
|
//Set whether to play pattern.
|
|
m_ProgressBar.PatternPlay = true;
|
|
//Set the speed of playing pattern.
|
|
m_ProgressBar.PatternSpeed = 0.5f;
|
|
//Set whether to play pattern forward.
|
|
m_ProgressBar.PatternForward = true;
|
|
//Set the scale of the pattern.
|
|
m_ProgressBar.PatternScale = 5;
|
|
}
|
|
}
|
|
*/
|
|
|
|
namespace RainbowArt.CleanFlatUI
|
|
{
|
|
[ExecuteAlways]
|
|
public class ProgressBarSpecialPatternAuto : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
float minValue = 0f;
|
|
|
|
[SerializeField]
|
|
float maxValue = 100.0f;
|
|
|
|
[SerializeField]
|
|
[Range(0, 1)]
|
|
float loadSpeed = 0.1f;
|
|
|
|
[SerializeField]
|
|
bool forward = true;
|
|
|
|
[SerializeField]
|
|
bool loop = true;
|
|
|
|
[SerializeField]
|
|
bool hasText = true;
|
|
|
|
[SerializeField]
|
|
TextMeshProUGUI text;
|
|
|
|
[SerializeField]
|
|
RectTransform foreground;
|
|
|
|
[SerializeField]
|
|
RectTransform foregroundArea;
|
|
|
|
[SerializeField]
|
|
RawImage patternImage;
|
|
|
|
[SerializeField]
|
|
RectTransform patternRect;
|
|
|
|
[SerializeField]
|
|
bool patternPlay = true;
|
|
|
|
[SerializeField]
|
|
float patternSpeed = 0.5f;
|
|
|
|
[SerializeField]
|
|
bool patternForward = true;
|
|
|
|
[SerializeField]
|
|
float patternScale = 5;
|
|
|
|
float currentValue = 0f;
|
|
bool bDelayedUpdate = false;
|
|
|
|
public float MinValue
|
|
{
|
|
get => minValue;
|
|
set
|
|
{
|
|
if(minValue == value)
|
|
{
|
|
return;
|
|
}
|
|
minValue = value;
|
|
OnValueChanged();
|
|
}
|
|
}
|
|
public float MaxValue
|
|
{
|
|
get => maxValue;
|
|
set
|
|
{
|
|
if(maxValue == value)
|
|
{
|
|
return;
|
|
}
|
|
maxValue = value;
|
|
OnValueChanged();
|
|
}
|
|
}
|
|
|
|
public bool HasText
|
|
{
|
|
get => hasText;
|
|
set
|
|
{
|
|
if (hasText == value)
|
|
{
|
|
return;
|
|
}
|
|
hasText = value;
|
|
UpdateText();
|
|
}
|
|
}
|
|
|
|
public float LoadSpeed
|
|
{
|
|
get => loadSpeed;
|
|
set
|
|
{
|
|
loadSpeed = value;
|
|
}
|
|
}
|
|
|
|
public bool Forward
|
|
{
|
|
get => forward;
|
|
set
|
|
{
|
|
forward = value;
|
|
}
|
|
}
|
|
|
|
public bool Loop
|
|
{
|
|
get => loop;
|
|
set
|
|
{
|
|
loop = value;
|
|
}
|
|
}
|
|
|
|
public bool PatternPlay
|
|
{
|
|
get => patternPlay;
|
|
set
|
|
{
|
|
patternPlay = value;
|
|
}
|
|
}
|
|
|
|
public float PatternSpeed
|
|
{
|
|
get => patternSpeed;
|
|
set
|
|
{
|
|
patternSpeed = value;
|
|
}
|
|
}
|
|
|
|
public bool PatternForward
|
|
{
|
|
get => patternForward;
|
|
set
|
|
{
|
|
patternForward = value;
|
|
}
|
|
}
|
|
|
|
public float PatternScale
|
|
{
|
|
get => patternScale;
|
|
set
|
|
{
|
|
patternScale = value;
|
|
}
|
|
}
|
|
|
|
void OnValueChanged()
|
|
{
|
|
if(maxValue < 0)
|
|
{
|
|
maxValue = 100.0f;
|
|
}
|
|
if(minValue < 0)
|
|
{
|
|
minValue = 0f;
|
|
}
|
|
currentValue = Mathf.Clamp(minValue, 0, maxValue);
|
|
Rect r = patternImage.uvRect;
|
|
r.width = (currentValue / maxValue)*patternScale;
|
|
patternImage.uvRect = r;
|
|
UpdateGUI();
|
|
}
|
|
|
|
void InitValue()
|
|
{
|
|
if(forward)
|
|
{
|
|
currentValue = minValue;
|
|
}
|
|
else
|
|
{
|
|
currentValue = maxValue;
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
InitValue();
|
|
UpdateGUI();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if(Application.isPlaying)
|
|
{
|
|
if(forward)
|
|
{
|
|
if (currentValue < maxValue)
|
|
{
|
|
currentValue += loadSpeed * (Time.deltaTime * 100);
|
|
if (currentValue >= maxValue)
|
|
{
|
|
currentValue = maxValue;
|
|
}
|
|
UpdateGUI();
|
|
}
|
|
if(loop)
|
|
{
|
|
if (currentValue >= maxValue)
|
|
{
|
|
currentValue = minValue;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (currentValue > minValue)
|
|
{
|
|
currentValue -= loadSpeed * (Time.deltaTime * 100);
|
|
if (currentValue <= minValue)
|
|
{
|
|
currentValue = minValue;
|
|
}
|
|
UpdateGUI();
|
|
}
|
|
if(loop)
|
|
{
|
|
if (currentValue <= minValue)
|
|
{
|
|
currentValue = maxValue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(bDelayedUpdate)
|
|
{
|
|
bDelayedUpdate = false;
|
|
OnValueChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
void UpdateGUI()
|
|
{
|
|
UpdateForegroundAndPattern();
|
|
UpdateText();
|
|
}
|
|
|
|
void UpdateForegroundAndPattern()
|
|
{
|
|
float maxWidth = foregroundArea.rect.width;
|
|
Vector2 offsetMax1 = foreground.offsetMax;
|
|
offsetMax1.x = -(maxWidth - maxWidth*(currentValue / maxValue));
|
|
foreground.offsetMax = offsetMax1;
|
|
if(patternPlay)
|
|
{
|
|
Rect r = patternImage.uvRect;
|
|
if(patternForward)
|
|
{
|
|
r.x -= Time.deltaTime * patternSpeed;
|
|
}
|
|
else
|
|
{
|
|
r.x += Time.deltaTime * patternSpeed;
|
|
}
|
|
r.width = (currentValue / maxValue)*patternScale;
|
|
patternImage.uvRect = r;
|
|
}
|
|
else
|
|
{
|
|
Rect r = patternImage.uvRect;
|
|
r.width = (currentValue / maxValue)*patternScale;
|
|
patternImage.uvRect = r;
|
|
}
|
|
}
|
|
|
|
void UpdateText()
|
|
{
|
|
if (text != null && text.gameObject.activeSelf != hasText)
|
|
{
|
|
text.gameObject.SetActive(hasText);
|
|
}
|
|
if (hasText && (text != null))
|
|
{
|
|
text.text = Mathf.Floor(currentValue) +"/"+ Mathf.Floor(maxValue);
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
protected void OnValidate()
|
|
{
|
|
bDelayedUpdate = true;
|
|
}
|
|
#endif
|
|
}
|
|
} |