Skip to content

Commit 198547b

Browse files
committed
feat: fixing progress bar
1 parent 736cf8c commit 198547b

File tree

3 files changed

+178
-114
lines changed

3 files changed

+178
-114
lines changed

Assets/ProgressBar/Scene/Demo.cs

+1-30
Original file line numberDiff line numberDiff line change
@@ -1,30 +1 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using UnityEngine;
4-
5-
public class Demo : MonoBehaviour {
6-
7-
public ProgressBar Pb;
8-
public ProgressBarCircle PbC;
9-
10-
private void Start()
11-
{
12-
Pb.BarValue = 50;
13-
PbC.BarValue = 50;
14-
}
15-
16-
void FixedUpdate () {
17-
18-
if(Input.GetKey(KeyCode.KeypadPlus))
19-
{
20-
Pb.BarValue += 1;
21-
PbC.BarValue += 1;
22-
}
23-
24-
if (Input.GetKey(KeyCode.KeypadMinus))
25-
{
26-
Pb.BarValue -= 1;
27-
PbC.BarValue -= 1;
28-
}
29-
}
30-
}
1+


Assets/ProgressBar/Script/ProgressBar.cs

+18-80
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,36 @@
55
[ExecuteInEditMode]
66
public class ProgressBar : MonoBehaviour
77
{
8-
[Header("Title Settings")]
9-
[SerializeField] private string title;
10-
[SerializeField] private Color titleColor = Color.white;
11-
[SerializeField] private Font titleFont;
12-
[SerializeField] private int titleFontSize = 14;
13-
14-
[Header("Bar Settings")]
8+
[Header("UI Components")]
9+
[SerializeField] private Image barBackground;
10+
[SerializeField] private Image bar;
11+
[SerializeField] private TextMeshProUGUI title;
12+
13+
[Header("Settings")]
14+
[Range(0f, 100f)]
15+
[SerializeField] private float barValue = 20f;
1516
[SerializeField] private Color barColor = Color.green;
16-
[SerializeField] private Color barBackgroundColor = Color.gray;
17-
[SerializeField] private Sprite barBackgroundSprite;
18-
[Range(1f, 100f)]
19-
[SerializeField] private int alertThreshold = 20;
20-
[SerializeField] private Color barAlertColor = Color.red;
21-
22-
[Header("Sound Alert")]
23-
[SerializeField] private AudioClip alertSound;
24-
[SerializeField] private bool repeatAlert = false;
25-
[SerializeField] private float repeatRate = 1f;
2617

27-
private Image bar;
28-
private Image barBackground;
29-
private TextMeshProUGUI titleText;
30-
private AudioSource audioSource;
31-
32-
private float barValue;
33-
public float BarValue
18+
private void Start()
3419
{
35-
get => barValue;
36-
set
20+
if (bar == null || title == null)
3721
{
38-
barValue = Mathf.Clamp(value, 0, 100);
39-
UpdateProgressBar(barValue);
22+
Debug.LogError("Bar or Title component is missing.");
23+
return;
4024
}
41-
}
42-
43-
private void Awake()
44-
{
45-
bar = transform.Find("Bar").GetComponent<Image>();
46-
barBackground = transform.Find("BarBackground").GetComponent<Image>();
47-
titleText = transform.Find("Title").GetComponent<TextMeshProUGUI>();
48-
audioSource = GetComponent<AudioSource>();
49-
}
5025

51-
private void Start()
52-
{
53-
InitializeUI();
5426
UpdateProgressBar(barValue);
5527
}
5628

57-
private void InitializeUI()
58-
{
59-
titleText.text = title;
60-
titleText.color = titleColor;
61-
titleText.fontSize = titleFontSize;
62-
63-
bar.color = barColor;
64-
barBackground.color = barBackgroundColor;
65-
barBackground.sprite = barBackgroundSprite;
66-
}
67-
6829
private void UpdateProgressBar(float value)
6930
{
70-
bar.fillAmount = value / 100;
71-
titleText.text = $"{title} {value:F0}%";
72-
73-
bar.color = value <= alertThreshold ? barAlertColor : barColor;
74-
}
75-
76-
private void Update()
77-
{
78-
if (!Application.isPlaying)
79-
{
80-
// Update the preview in the editor
81-
UpdatePreviewInEditor();
82-
}
83-
else
84-
{
85-
HandleSoundAlerts();
86-
}
87-
}
88-
89-
private void UpdatePreviewInEditor()
90-
{
91-
UpdateProgressBar(50); // Example for preview
92-
InitializeUI();
31+
bar.fillAmount = value / 100f;
32+
bar.color = barColor;
33+
title.text = $"Fixing... ({value:F0}%)";
9334
}
9435

95-
private void HandleSoundAlerts()
36+
private void OnValidate()
9637
{
97-
if (barValue <= alertThreshold && repeatAlert && Time.time > audioSource.time + repeatRate)
98-
{
99-
audioSource.PlayOneShot(alertSound);
100-
}
38+
UpdateProgressBar(barValue);
10139
}
102-
}
40+
}

Assets/Scenes/Reparation.unity

+159-4
Original file line numberDiff line numberDiff line change
@@ -2506,14 +2506,38 @@ PrefabInstance:
25062506
propertyPath: m_FontData.m_Alignment
25072507
value: 4
25082508
objectReference: {fileID: 0}
2509+
- target: {fileID: 114542602933161078, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
2510+
propertyPath: m_Color.b
2511+
value: 0
2512+
objectReference: {fileID: 0}
2513+
- target: {fileID: 114542602933161078, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
2514+
propertyPath: m_FillAmount
2515+
value: 0.90599996
2516+
objectReference: {fileID: 0}
2517+
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
2518+
propertyPath: Pb
2519+
value:
2520+
objectReference: {fileID: 1414136645}
2521+
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
2522+
propertyPath: bar
2523+
value:
2524+
objectReference: {fileID: 1457163935}
25092525
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
25102526
propertyPath: title
2511-
value: Health
2527+
value:
2528+
objectReference: {fileID: 1475119614}
2529+
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
2530+
propertyPath: barValue
2531+
value: 86.2
25122532
objectReference: {fileID: 0}
25132533
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
25142534
propertyPath: m_Enabled
2515-
value: 0
2535+
value: 1
25162536
objectReference: {fileID: 0}
2537+
- target: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
2538+
propertyPath: barBackground
2539+
value:
2540+
objectReference: {fileID: 1879785534}
25172541
- target: {fileID: 224763426186284840, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
25182542
propertyPath: m_Pivot.x
25192543
value: 0.5
@@ -2610,10 +2634,14 @@ PrefabInstance:
26102634
propertyPath: m_ConstrainProportionsScale
26112635
value: 1
26122636
objectReference: {fileID: 0}
2613-
m_RemovedComponents: []
2637+
m_RemovedComponents:
2638+
- {fileID: 114165064244077878, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
26142639
m_RemovedGameObjects: []
26152640
m_AddedGameObjects: []
2616-
m_AddedComponents: []
2641+
m_AddedComponents:
2642+
- targetCorrespondingSourceObject: {fileID: 1484564107729232, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
2643+
insertIndex: -1
2644+
addedObject: {fileID: 1475119614}
26172645
m_SourcePrefab: {fileID: 100100000, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
26182646
--- !u!224 &148503689 stripped
26192647
RectTransform:
@@ -30605,6 +30633,17 @@ BoxCollider:
3060530633
serializedVersion: 3
3060630634
m_Size: {x: 4.9154043, y: 2.0777142, z: 4.6336346}
3060730635
m_Center: {x: 0.00000059604645, y: -0.00000011920929, z: -0.00000011920929}
30636+
--- !u!114 &1414136645 stripped
30637+
MonoBehaviour:
30638+
m_CorrespondingSourceObject: {fileID: 114769781544105922, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
30639+
m_PrefabInstance: {fileID: 148503688}
30640+
m_PrefabAsset: {fileID: 0}
30641+
m_GameObject: {fileID: 0}
30642+
m_Enabled: 1
30643+
m_EditorHideFlags: 0
30644+
m_Script: {fileID: 11500000, guid: 149d864443ddca94ba93b092e7acd5ea, type: 3}
30645+
m_Name:
30646+
m_EditorClassIdentifier:
3060830647
--- !u!4 &1414432208 stripped
3060930648
Transform:
3061030649
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: a73df10e437557a40add860503f7acf0, type: 3}
@@ -31622,6 +31661,17 @@ PrefabInstance:
3162231661
m_AddedGameObjects: []
3162331662
m_AddedComponents: []
3162431663
m_SourcePrefab: {fileID: 9199261849990390558, guid: 095fc4f97bebec547be442229ad33025, type: 3}
31664+
--- !u!114 &1457163935 stripped
31665+
MonoBehaviour:
31666+
m_CorrespondingSourceObject: {fileID: 114542602933161078, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
31667+
m_PrefabInstance: {fileID: 148503688}
31668+
m_PrefabAsset: {fileID: 0}
31669+
m_GameObject: {fileID: 0}
31670+
m_Enabled: 1
31671+
m_EditorHideFlags: 0
31672+
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
31673+
m_Name:
31674+
m_EditorClassIdentifier:
3162531675
--- !u!1001 &1466199830
3162631676
PrefabInstance:
3162731677
m_ObjectHideFlags: 0
@@ -31925,6 +31975,100 @@ Transform:
3192531975
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: 0dc7ed63429e1b045bdec55b2f62133f, type: 3}
3192631976
m_PrefabInstance: {fileID: 549514227}
3192731977
m_PrefabAsset: {fileID: 0}
31978+
--- !u!1 &1475119610 stripped
31979+
GameObject:
31980+
m_CorrespondingSourceObject: {fileID: 1484564107729232, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
31981+
m_PrefabInstance: {fileID: 148503688}
31982+
m_PrefabAsset: {fileID: 0}
31983+
--- !u!114 &1475119614
31984+
MonoBehaviour:
31985+
m_ObjectHideFlags: 0
31986+
m_CorrespondingSourceObject: {fileID: 0}
31987+
m_PrefabInstance: {fileID: 0}
31988+
m_PrefabAsset: {fileID: 0}
31989+
m_GameObject: {fileID: 1475119610}
31990+
m_Enabled: 1
31991+
m_EditorHideFlags: 0
31992+
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
31993+
m_Name:
31994+
m_EditorClassIdentifier:
31995+
m_Material: {fileID: 0}
31996+
m_Color: {r: 1, g: 1, b: 1, a: 1}
31997+
m_RaycastTarget: 1
31998+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
31999+
m_Maskable: 1
32000+
m_OnCullStateChanged:
32001+
m_PersistentCalls:
32002+
m_Calls: []
32003+
m_text: Fixing... (86%)
32004+
m_isRightToLeft: 0
32005+
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
32006+
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
32007+
m_fontSharedMaterials: []
32008+
m_fontMaterial: {fileID: 0}
32009+
m_fontMaterials: []
32010+
m_fontColor32:
32011+
serializedVersion: 2
32012+
rgba: 4294967295
32013+
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
32014+
m_enableVertexGradient: 0
32015+
m_colorMode: 3
32016+
m_fontColorGradient:
32017+
topLeft: {r: 1, g: 1, b: 1, a: 1}
32018+
topRight: {r: 1, g: 1, b: 1, a: 1}
32019+
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
32020+
bottomRight: {r: 1, g: 1, b: 1, a: 1}
32021+
m_fontColorGradientPreset: {fileID: 0}
32022+
m_spriteAsset: {fileID: 0}
32023+
m_tintAllSprites: 0
32024+
m_StyleSheet: {fileID: 0}
32025+
m_TextStyleHashCode: -1183493901
32026+
m_overrideHtmlColors: 0
32027+
m_faceColor:
32028+
serializedVersion: 2
32029+
rgba: 4294967295
32030+
m_fontSize: 40
32031+
m_fontSizeBase: 40
32032+
m_fontWeight: 400
32033+
m_enableAutoSizing: 0
32034+
m_fontSizeMin: 18
32035+
m_fontSizeMax: 72
32036+
m_fontStyle: 0
32037+
m_HorizontalAlignment: 2
32038+
m_VerticalAlignment: 256
32039+
m_textAlignment: 65535
32040+
m_characterSpacing: 0
32041+
m_wordSpacing: 0
32042+
m_lineSpacing: 0
32043+
m_lineSpacingMax: 0
32044+
m_paragraphSpacing: 0
32045+
m_charWidthMaxAdj: 0
32046+
m_enableWordWrapping: 1
32047+
m_wordWrappingRatios: 0.4
32048+
m_overflowMode: 0
32049+
m_linkedTextComponent: {fileID: 0}
32050+
parentLinkedComponent: {fileID: 0}
32051+
m_enableKerning: 1
32052+
m_enableExtraPadding: 0
32053+
checkPaddingRequired: 0
32054+
m_isRichText: 1
32055+
m_parseCtrlCharacters: 1
32056+
m_isOrthographic: 1
32057+
m_isCullingEnabled: 0
32058+
m_horizontalMapping: 0
32059+
m_verticalMapping: 0
32060+
m_uvLineOffset: 0
32061+
m_geometrySortingOrder: 0
32062+
m_IsTextObjectScaleStatic: 0
32063+
m_VertexBufferAutoSizeReduction: 0
32064+
m_useMaxVisibleDescender: 1
32065+
m_pageToDisplay: 1
32066+
m_margin: {x: 0, y: 0, z: 0, w: 0}
32067+
m_isUsingLegacyAnimationComponent: 0
32068+
m_isVolumetricText: 0
32069+
m_hasFontAssetChanged: 0
32070+
m_baseMaterial: {fileID: 0}
32071+
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
3192832072
--- !u!1001 &1476097936
3192932073
PrefabInstance:
3193032074
m_ObjectHideFlags: 0
@@ -40758,6 +40902,17 @@ Transform:
4075840902
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: 6fec6d200fb25e04184d9b53e0e5c97e, type: 3}
4075940903
m_PrefabInstance: {fileID: 1713975463}
4076040904
m_PrefabAsset: {fileID: 0}
40905+
--- !u!114 &1879785534 stripped
40906+
MonoBehaviour:
40907+
m_CorrespondingSourceObject: {fileID: 114418616620393906, guid: 5eefba205a58c8c4a937c97b87c185a3, type: 3}
40908+
m_PrefabInstance: {fileID: 148503688}
40909+
m_PrefabAsset: {fileID: 0}
40910+
m_GameObject: {fileID: 0}
40911+
m_Enabled: 1
40912+
m_EditorHideFlags: 0
40913+
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
40914+
m_Name:
40915+
m_EditorClassIdentifier:
4076140916
--- !u!1001 &1879957295
4076240917
PrefabInstance:
4076340918
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)