@@ -31,6 +31,22 @@ public class JCS_Toggle
3131
3232 private JCS_ColorTweener mColorTweener = null ;
3333
34+ #if ( UNITY_EDITOR )
35+ [ Header ( "** Helper Variables (JCS_Toggle) **" ) ]
36+ 37+ [ Tooltip ( "Test module with the key?" ) ]
38+ [ SerializeField ]
39+ private bool mTestWithKey = false ;
40+ 41+ [ Tooltip ( "Key to toggle this toggle component." ) ]
42+ [ SerializeField ]
43+ private KeyCode mToggleOnOffKey = KeyCode . A ;
44+ 45+ [ Tooltip ( "Key to toggle interactable." ) ]
46+ [ SerializeField ]
47+ private KeyCode mToggleInteractable = KeyCode . S ;
48+ #endif
49+ 3450
3551 [ Header ( "** Initialize Variables (JCS_Toggle) **" ) ]
3652
@@ -113,6 +129,9 @@ protected override void Awake()
113129 this . mToggleSign = this . GetComponentInChildren < JCS_ToggleSign > ( ) ;
114130
115131 this . mColorTweener = this . GetComponent < JCS_ColorTweener > ( ) ;
132+ 133+ // Add interactable callback.
134+ this . interactableCallback += InteractableCallback ;
116135 }
117136
118137 private void Start ( )
@@ -127,8 +146,14 @@ private void Start()
127146#if ( UNITY_EDITOR )
128147 private void Update ( )
129148 {
130- if ( Input . GetKeyDown ( KeyCode . A ) )
149+ if ( ! mTestWithKey )
150+ return ;
151+ 152+ if ( Input . GetKeyDown ( mToggleOnOffKey ) )
131153 Toggle ( ) ;
154+ 155+ if ( Input . GetKeyDown ( mToggleInteractable ) )
156+ Interactable = ! Interactable ;
132157 }
133158#endif
134159
@@ -152,9 +177,15 @@ public override void JCS_OnClickCallback()
152177 /// <returns>
153178 /// true, after toggle once is on.
154179 /// false, after toggle once is off.
180+ ///
181+ /// ATTENTION: If toggle is not interactable will
182+ /// return false.
155183 /// </returns>
156184 public bool Toggle ( )
157185 {
186+ if ( ! Interactable )
187+ return false ;
188+ 158189 // Toggle it.
159190 mIsOn = ! mIsOn ;
160191
@@ -243,7 +274,7 @@ private void DoToggle(bool act)
243274 /// </summary>
244275 /// <param name="vec3"> vector to negative. </param>
245276 /// <returns> negative vector. </returns>
246- public static Vector3 ToNegative ( Vector3 vec3 )
277+ private static Vector3 ToNegative ( Vector3 vec3 )
247278 {
248279 Vector3 newVec ;
249280 newVec . x = - vec3 . x ;
@@ -252,5 +283,56 @@ public static Vector3 ToNegative(Vector3 vec3)
252283
253284 return newVec ;
254285 }
286+ 287+ /// <summary>
288+ /// After set interactable callback.
289+ /// </summary>
290+ private void InteractableCallback ( )
291+ {
292+ Color targetBackgroundColor = mOnBackgroundColor ;
293+ Color targetButtonColor = mOnButtonColor ;
294+ 295+ if ( ! mIsOn )
296+ {
297+ targetBackgroundColor = mOffBackgroundColor ;
298+ targetButtonColor = mOffButtonColor ;
299+ }
300+ 301+ if ( Interactable )
302+ {
303+ targetBackgroundColor . a = mInteractColor . a ;
304+ targetButtonColor . a = mInteractColor . a ;
305+ }
306+ else
307+ {
308+ // Stop color tweener if between the process of tweener.
309+ mToggleSign . ColorTweener . ResetTweener ( ) ;
310+ mColorTweener . ResetTweener ( ) ;
311+ 312+ targetBackgroundColor . a = mNotInteractColor . a ;
313+ targetButtonColor . a = mNotInteractColor . a ;
314+ }
315+ 316+ SetBackgroundColor ( targetBackgroundColor ) ;
317+ SetButtonColor ( targetButtonColor ) ;
318+ }
319+ 320+ /// <summary>
321+ /// Set the color of the toggle button.
322+ /// </summary>
323+ /// <param name="col"></param>
324+ private void SetButtonColor ( Color col )
325+ {
326+ mToggleSign . ColorTweener . LocalColor = col ;
327+ }
328+ 329+ /// <summary>
330+ /// Set the color of the toggle background.
331+ /// </summary>
332+ /// <param name="col"></param>
333+ private void SetBackgroundColor ( Color col )
334+ {
335+ mColorTweener . LocalColor = col ;
336+ }
255337 }
256338}
0 commit comments