@@ -10,14 +10,14 @@ public class ProgressBar : ProgressBarBase
1010 {
1111 [ SerializeField , HideInInspector ] private RectTransform _rectTransform ;
1212
13- [ SerializeField ] private BarSegment _bar ;
13+ [ SerializeField ] private BarSegment _fillBar ;
1414 [ SerializeField ] private Direction _direction ;
1515 [ SerializeField , Range ( 0f , 1f ) ] private float _center = 0.5f ;
1616
1717 [ SerializeField ] private ValueMode _minValueMode = ValueMode . Custom ;
1818 [ SerializeField ] private ValueMode _maxValueMode = ValueMode . Custom ;
1919
20- private RectTransform RectTransform
20+ public RectTransform RectTransform
2121 {
2222 get
2323 {
@@ -27,9 +27,27 @@ private RectTransform RectTransform
2727 }
2828 }
2929
30- public override float MinValue => GetValueByMode ( _minValueMode , base . MinValue ) ;
31- public override float MaxValue => GetValueByMode ( _maxValueMode , base . MaxValue ) ;
32-
30+ public BarSegment FillBar
31+ {
32+ get => _fillBar ;
33+ set
34+ {
35+ _fillBar = value ;
36+ if ( _fillBar != null )
37+ UpdateVisualProgress ( Value ) ;
38+ }
39+ }
40+ public override float MinValue
41+ {
42+ get => GetValueByMode ( _minValueMode , base . MinValue ) ;
43+ set => SetValueByMode ( _minValueMode , value , ( ctx ) => base . MinValue = ctx ) ;
44+ }
45+ public override float MaxValue
46+ {
47+ get => GetValueByMode ( _maxValueMode , base . MaxValue ) ;
48+ set => SetValueByMode ( _maxValueMode , value , ( ctx ) => base . MaxValue = ctx ) ;
49+ }
50+
3351 public float Center
3452 {
3553 get => _center ;
@@ -52,7 +70,7 @@ public Direction direction
5270#if UNITY_EDITOR
5371 private void Update ( )
5472 {
55- if ( Application . isPlaying == false && _bar != null )
73+ if ( Application . isPlaying == false && _fillBar != null )
5674 UpdateVisualProgress ( Value ) ;
5775 }
5876#endif
@@ -74,25 +92,33 @@ public float CalculateRightCenter(float position)
7492
7593 public void UpdateVisualProgress ( float value )
7694 {
95+ #if UNITY_EDITOR
96+ if ( _fillBar == null )
97+ {
98+ Debug . LogException ( new NullReferenceException ( "Fill Bar is null" ) , this ) ;
99+ return ;
100+ }
101+ #endif
102+
77103 var position = CalculatePosition ( value ) ;
78104
79105 switch ( _direction )
80106 {
81107 case Direction . Right :
82108 {
83- _bar . SetPosition ( 0f , position ) ;
109+ _fillBar . SetPosition ( 0f , position ) ;
84110 break ;
85111 }
86112 case Direction . Left :
87113 {
88- _bar . SetPosition ( - position + 1f , 1f ) ;
114+ _fillBar . SetPosition ( - position + 1f , 1f ) ;
89115 break ;
90116 }
91117 case Direction . Center :
92118 {
93119 float left = CalculateLeftCenter ( position ) ;
94120 float right = CalculateRightCenter ( position ) ;
95- _bar . SetPosition ( left , right ) ;
121+ _fillBar . SetPosition ( left , right ) ;
96122 break ;
97123 }
98124 default :
@@ -112,16 +138,66 @@ private float GetValueByMode(ValueMode mode, float customValue)
112138 } ;
113139 }
114140
141+ private void SetValueByMode ( ValueMode mode , float value , Action < float > customValue )
142+ {
143+ switch ( mode )
144+ {
145+ case ValueMode . Custom :
146+ {
147+ customValue . Invoke ( value ) ;
148+ base . MaxValue = value ;
149+ UpdateValue ( ) ;
150+ break ;
151+ }
152+ case ValueMode . Width :
153+ {
154+ var size = new Vector2 ( value , RectTransform . sizeDelta . y ) ;
155+ RectTransform . sizeDelta = size ;
156+ break ;
157+ }
158+ case ValueMode . Height :
159+ {
160+ var size = new Vector2 ( RectTransform . sizeDelta . x , value ) ;
161+ RectTransform . sizeDelta = size ;
162+ break ;
163+ }
164+ case ValueMode . HeightOrWidth :
165+ {
166+ SetHeightOrWidthOption ( value ) ;
167+ break ;
168+ }
169+ default :
170+ throw new ArgumentOutOfRangeException ( nameof ( mode ) , mode , null ) ;
171+ }
172+ }
173+
115174 private float GetHeightOrWidthOption ( )
116175 {
117- return _bar . axis switch
176+ return _fillBar . axis switch
118177 {
119178 BarSegment . Axis . Horizontal => RectTransform . rect . width ,
120179 BarSegment . Axis . Vertical => RectTransform . rect . height ,
121180 _ => throw new ArgumentOutOfRangeException ( )
122181 } ;
123182 }
124183
184+ private void SetHeightOrWidthOption ( float value )
185+ {
186+ var size = _fillBar . axis switch
187+ {
188+ BarSegment . Axis . Horizontal => new Vector2 ( value , RectTransform . sizeDelta . y ) ,
189+ BarSegment . Axis . Vertical => new Vector2 ( RectTransform . sizeDelta . x , value ) ,
190+ _ => throw new ArgumentOutOfRangeException ( )
191+ } ;
192+ RectTransform . sizeDelta = size ;
193+ base . UpdateValue ( ) ;
194+ }
195+
196+ protected override bool ShouldFilterSameValues ( )
197+ {
198+ return true ;
199+ }
200+
125201 public enum Direction
126202 {
127203 Right ,
0 commit comments