Skip to content

Commit

Permalink
feat: fixing progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoneerC committed Sep 26, 2024
1 parent 736cf8c commit 198547b
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 114 deletions.
31 changes: 1 addition & 30 deletions Assets/ProgressBar/Scene/Demo.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo : MonoBehaviour {

public ProgressBar Pb;
public ProgressBarCircle PbC;

private void Start()
{
Pb.BarValue = 50;
PbC.BarValue = 50;
}

void FixedUpdate () {

if(Input.GetKey(KeyCode.KeypadPlus))
{
Pb.BarValue += 1;
PbC.BarValue += 1;
}

if (Input.GetKey(KeyCode.KeypadMinus))
{
Pb.BarValue -= 1;
PbC.BarValue -= 1;
}
}
}

98 changes: 18 additions & 80 deletions Assets/ProgressBar/Script/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,98 +5,36 @@
[ExecuteInEditMode]
public class ProgressBar : MonoBehaviour
{
[Header("Title Settings")]
[SerializeField] private string title;
[SerializeField] private Color titleColor = Color.white;
[SerializeField] private Font titleFont;
[SerializeField] private int titleFontSize = 14;

[Header("Bar Settings")]
[Header("UI Components")]
[SerializeField] private Image barBackground;
[SerializeField] private Image bar;
[SerializeField] private TextMeshProUGUI title;

[Header("Settings")]
[Range(0f, 100f)]
[SerializeField] private float barValue = 20f;
[SerializeField] private Color barColor = Color.green;
[SerializeField] private Color barBackgroundColor = Color.gray;
[SerializeField] private Sprite barBackgroundSprite;
[Range(1f, 100f)]
[SerializeField] private int alertThreshold = 20;
[SerializeField] private Color barAlertColor = Color.red;

[Header("Sound Alert")]
[SerializeField] private AudioClip alertSound;
[SerializeField] private bool repeatAlert = false;
[SerializeField] private float repeatRate = 1f;

private Image bar;
private Image barBackground;
private TextMeshProUGUI titleText;
private AudioSource audioSource;

private float barValue;
public float BarValue
private void Start()
{
get => barValue;
set
if (bar == null || title == null)
{
barValue = Mathf.Clamp(value, 0, 100);
UpdateProgressBar(barValue);
Debug.LogError("Bar or Title component is missing.");
return;
}
}

private void Awake()
{
bar = transform.Find("Bar").GetComponent<Image>();
barBackground = transform.Find("BarBackground").GetComponent<Image>();
titleText = transform.Find("Title").GetComponent<TextMeshProUGUI>();
audioSource = GetComponent<AudioSource>();
}

private void Start()
{
InitializeUI();
UpdateProgressBar(barValue);
}

private void InitializeUI()
{
titleText.text = title;
titleText.color = titleColor;
titleText.fontSize = titleFontSize;

bar.color = barColor;
barBackground.color = barBackgroundColor;
barBackground.sprite = barBackgroundSprite;
}

private void UpdateProgressBar(float value)
{
bar.fillAmount = value / 100;
titleText.text = $"{title} {value:F0}%";

bar.color = value <= alertThreshold ? barAlertColor : barColor;
}

private void Update()
{
if (!Application.isPlaying)
{
// Update the preview in the editor
UpdatePreviewInEditor();
}
else
{
HandleSoundAlerts();
}
}

private void UpdatePreviewInEditor()
{
UpdateProgressBar(50); // Example for preview
InitializeUI();
bar.fillAmount = value / 100f;
bar.color = barColor;
title.text = $"Fixing... ({value:F0}%)";
}

private void HandleSoundAlerts()
private void OnValidate()
{
if (barValue <= alertThreshold && repeatAlert && Time.time > audioSource.time + repeatRate)
{
audioSource.PlayOneShot(alertSound);
}
UpdateProgressBar(barValue);
}
}
}
163 changes: 159 additions & 4 deletions Assets/Scenes/Reparation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -2506,14 +2506,38 @@ PrefabInstance:
propertyPath: m_FontData.m_Alignment
value: 4
objectReference: {fileID: 0}
- target: {fileID: 114542602933161078, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: m_Color.b
value: 0
objectReference: {fileID: 0}
- target: {fileID: 114542602933161078, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: m_FillAmount
value: 0.90599996
objectReference: {fileID: 0}
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: Pb
value:
objectReference: {fileID: 1414136645}
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: bar
value:
objectReference: {fileID: 1457163935}
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: title
value: Health
value:
objectReference: {fileID: 1475119614}
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: barValue
value: 86.2
objectReference: {fileID: 0}
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: m_Enabled
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: barBackground
value:
objectReference: {fileID: 1879785534}
- target: {fileID: 224763426186284840, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
propertyPath: m_Pivot.x
value: 0.5
Expand Down Expand Up @@ -2610,10 +2634,14 @@ PrefabInstance:
propertyPath: m_ConstrainProportionsScale
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedComponents:
- {fileID: 114165064244077878, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_AddedComponents:
- targetCorrespondingSourceObject: {fileID: 1484564107729232, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
insertIndex: -1
addedObject: {fileID: 1475119614}
m_SourcePrefab: {fileID: 100100000, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
--- !u!224 &148503689 stripped
RectTransform:
Expand Down Expand Up @@ -30605,6 +30633,17 @@ BoxCollider:
serializedVersion: 3
m_Size: {x: 4.9154043, y: 2.0777142, z: 4.6336346}
m_Center: {x: 0.00000059604645, y: -0.00000011920929, z: -0.00000011920929}
--- !u!114 &1414136645 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
m_PrefabInstance: {fileID: 148503688}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 149d864443ddca94ba93b092e7acd5ea, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1414432208 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: a73df10e437557a40add860503f7acf0, type: 3}
Expand Down Expand Up @@ -31622,6 +31661,17 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 9199261849990390558, guid: 095fc4f97bebec547be442229ad33025, type: 3}
--- !u!114 &1457163935 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 114542602933161078, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
m_PrefabInstance: {fileID: 148503688}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &1466199830
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -31925,6 +31975,100 @@ Transform:
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: 0dc7ed63429e1b045bdec55b2f62133f, type: 3}
m_PrefabInstance: {fileID: 549514227}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1475119610 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1484564107729232, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
m_PrefabInstance: {fileID: 148503688}
m_PrefabAsset: {fileID: 0}
--- !u!114 &1475119614
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1475119610}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: Fixing... (86%)
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4294967295
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 40
m_fontSizeBase: 40
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_HorizontalAlignment: 2
m_VerticalAlignment: 256
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!1001 &1476097936
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -40758,6 +40902,17 @@ Transform:
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: 6fec6d200fb25e04184d9b53e0e5c97e, type: 3}
m_PrefabInstance: {fileID: 1713975463}
m_PrefabAsset: {fileID: 0}
--- !u!114 &1879785534 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 114418616620393906, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
m_PrefabInstance: {fileID: 148503688}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &1879957295
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 198547b

Please sign in to comment.