@@ -19,12 +19,24 @@ namespace JCSUnity
1919 [ RequireComponent ( typeof ( RectTransform ) ) ]
2020 public class JCS_Canvas : MonoBehaviour
2121 {
22+ public enum ShowMethod
23+ {
24+ CUSTOM = 0 ,
25+ ENABLE = 1 ,
26+ FADE = 2 ,
27+ }
28+
2229 /* Variables */
2330
2431 public static JCS_Canvas main = null ;
2532
2633 private const string RESIZE_UI_PATH = "UI/ResizeUI" ;
2734
35+ // Execution to show canvas.
36+ public Action doShow = null ;
37+ // Execution to hide canvas.
38+ public Action doHide = null ;
39+
2840 // Execution when canvas is shown.
2941 public Action < JCS_Canvas > onShow = null ;
3042 // Execution when canvas is hidden.
@@ -90,11 +102,11 @@ public class JCS_Canvas : MonoBehaviour
90102 [ SerializeField ]
91103 private bool mMainCanvas = true ;
92104
93- [ Separator ( "Runtime Variables (JCS_Canvas)" ) ]
94-
95- [ Tooltip ( "Turn on when you want to fade the canvas by default." ) ]
105+ [ Tooltip ( "The method to display this canvas." ) ]
96106 [ SerializeField ]
97- private bool mFade = false ;
107+ private ShowMethod mShowMethod = ShowMethod . ENABLE ;
108+
109+ [ Separator ( "Runtime Variables (JCS_Canvas)" ) ]
98110
99111 [ Tooltip ( "How fast the canvas fades." ) ]
100112 [ SerializeField ]
@@ -117,11 +129,11 @@ public class JCS_Canvas : MonoBehaviour
117129
118130 [ Tooltip ( "Play sound when active the canvas." ) ]
119131 [ SerializeField ]
120- private AudioClip mActiveSound = null ;
132+ private AudioClip mSoundOnShow = null ;
121133
122134 [ Tooltip ( "Play sound when deactive the canvas." ) ]
123135 [ SerializeField ]
124- private AudioClip mDeactiveSound = null ;
136+ private AudioClip mSoundOnHide = null ;
125137
126138 /* Setter & Getter */
127139
@@ -132,14 +144,15 @@ public class JCS_Canvas : MonoBehaviour
132144
133145 public bool DisplayOnAwake { get { return this . mDisplayOnAwake ; } }
134146 public bool MainCanvas { get { return this . mMainCanvas ; } }
147+ public ShowMethod showMethod { get { return this . mShowMethod ; } set { this . mShowMethod = value ; } }
135148
136- public bool Fade { get { return this . mFade ; } set { this . mFade = value ; } }
137149 public float FadeFriction { get { return this . mFadeFriction ; } set { this . mFadeFriction = value ; } }
138150 public float FadeInAmount { get { return this . mFadeInAmount ; } set { this . mFadeInAmount = value ; } }
139151 public float FadeOutAmount { get { return this . mFadeOutAmount ; } set { this . mFadeOutAmount = value ; } }
152+
140153 public JCS_TimeType TimeType { get { return this . mTimeType ; } set { this . mTimeType = value ; } }
141- public AudioClip ActiveSound { get { return this . mActiveSound ; } set { this . mActiveSound = value ; } }
142- public AudioClip DeactiveSound { get { return this . mDeactiveSound ; } set { this . mDeactiveSound = value ; } }
154+ public AudioClip SoundOnShow { get { return this . mSoundOnShow ; } set { this . mSoundOnShow = value ; } }
155+ public AudioClip SoundOnHide { get { return this . mSoundOnHide ; } set { this . mSoundOnHide = value ; } }
143156
144157 /* Functions */
145158
@@ -153,13 +166,18 @@ private void Awake()
153166
154167 if ( JCS_UISettings . instance . RESIZE_UI && ! JCS_ScreenSettings . instance . IsNone ( ) )
155168 {
169+ GameObject spawned = JCS_Util . Instantiate ( RESIZE_UI_PATH ) ;
170+
156171 // resizable UI in order to resize the UI correctly
157- mResizeUI = JCS_Util . Instantiate ( RESIZE_UI_PATH ) . GetComponent < JCS_ResizeUI > ( ) ;
172+ mResizeUI = spawned . GetComponent < JCS_ResizeUI > ( ) ;
173+
158174 mResizeUI . transform . SetParent ( this . transform ) ;
159175 }
160176
161177 JCS_UIManager . instance . AddCanvas ( this ) ;
162178
179+ AssignDefaultShowHide ( ) ;
180+
163181 if ( mDisplayOnAwake )
164182 Show ( ) ;
165183 else
@@ -208,6 +226,33 @@ private void Test()
208226 }
209227#endif
210228
229+ /// <summary>
230+ /// Assign the default show/hide behaviour.
231+ /// </summary>
232+ private void AssignDefaultShowHide ( )
233+ {
234+ switch ( mShowMethod )
235+ {
236+ case ShowMethod . CUSTOM :
237+ {
238+ // ..
239+ }
240+ break ;
241+ case ShowMethod . ENABLE :
242+ {
243+ doShow += ShowEnable ;
244+ doHide += HideEnable ;
245+ }
246+ break ;
247+ case ShowMethod . FADE :
248+ {
249+ doShow += ShowFade ;
250+ doHide += HideFade ;
251+ }
252+ break ;
253+ }
254+ }
255+
211256 /// <summary>
212257 /// Return the `canvas` that is the parent of the `trans` object.
213258 ///
@@ -219,6 +264,7 @@ public static JCS_Canvas GuessCanvas(Transform trans = null)
219264 if ( trans != null )
220265 {
221266 var canvas = trans . GetComponentInParent < JCS_Canvas > ( ) ;
267+
222268 if ( canvas != null )
223269 return canvas ;
224270 }
@@ -235,13 +281,13 @@ public void AddComponentToResizeCanvas(Component com)
235281 Transform newParent = ( mResizeUI != null ) ? mResizeUI . transform : this . mCanvas . transform ;
236282
237283 if ( newParent == null )
238- Debug . LogError ( "Attach resize canvas exception: " + com ) ;
284+ Debug . LogError ( $ "Attach resize canvas exception: { com } " ) ;
239285 else
240286 com . transform . SetParent ( newParent ) ;
241287
242- // We will expect COM to be one of the UI component from built-in
243- // Unity. If this is true, we resize it's component to match
244- // the current screen space.
288+ // We will expect COM to be one of the UI component from
289+ // built-in Unity. If this is true, we resize it's component
290+ // to match the current screen space.
245291 var rect = com . GetComponent < RectTransform > ( ) ;
246292 FitScreenSize ( rect ) ;
247293 }
@@ -258,78 +304,85 @@ public bool IsShown()
258304 /// Show the canvas so it's visible.
259305 /// </summary>
260306 public void Show ( bool mute = false )
261- {
262- Show ( mFade , mute ) ;
263- }
264- public void Show ( bool fade , bool mute = false )
265307 {
266308 if ( ! mute )
267- JCS_SoundPlayer . PlayByAttachment ( mDeactiveSound , JCS_SoundMethod . PLAY_SOUND ) ;
309+ {
310+ JCS_SoundPlayer . PlayByAttachment (
311+ mSoundOnShow ,
312+ JCS_SoundMethod . PLAY_SOUND ) ;
313+ }
268314
269315 mCanvas . enabled = true ;
270316
271- if ( fade )
272- {
273- mFading = JCS_FadeType . IN ;
274-
275- mFadeAlpa = mFadeInAmount ;
276- }
277- else
278- {
279- if ( mCanvasGroup != null )
280- mCanvasGroup . alpha = mFadeInAmount ;
281- }
317+ doShow ? . Invoke ( ) ;
282318
283319 onShow ? . Invoke ( this ) ;
284320 }
285321
322+ #region Show
323+
324+ private void ShowEnable ( )
325+ {
326+ if ( mCanvasGroup != null )
327+ mCanvasGroup . alpha = mFadeInAmount ;
328+ }
329+
330+ private void ShowFade ( )
331+ {
332+ mFading = JCS_FadeType . IN ;
333+ mFadeAlpa = mFadeInAmount ;
334+ }
335+
336+ #endregion
337+
286338 /// <summary>
287339 /// Hide the canvas so it's invisible.
288340 /// </summary>
289341 public void Hide ( bool mute = false )
290- {
291- Hide ( mFade , mute ) ;
292- }
293- public void Hide ( bool fade , bool mute = false )
294342 {
295343 if ( ! mute )
296- JCS_SoundPlayer . PlayByAttachment ( mActiveSound , JCS_SoundMethod . PLAY_SOUND ) ;
297-
298- if ( fade )
299344 {
300- // Remains enabled since we're going to do fading.
301- mCanvas . enabled = true ;
302-
303- mFading = JCS_FadeType . OUT ;
304-
305- mFadeAlpa = mFadeOutAmount ;
345+ JCS_SoundPlayer . PlayByAttachment (
346+ mSoundOnHide ,
347+ JCS_SoundMethod . PLAY_SOUND ) ;
306348 }
307- else
308- {
309- mCanvas . enabled = false ;
310349
311- if ( mCanvasGroup != null )
312- mCanvasGroup . alpha = mFadeOutAmount ;
313- }
350+ doHide ? . Invoke ( ) ;
314351
315352 onHide ? . Invoke ( this ) ;
316353 }
317354
355+ #region Hide
356+
357+ private void HideEnable ( )
358+ {
359+ mCanvas . enabled = false ;
360+
361+ if ( mCanvasGroup != null )
362+ mCanvasGroup . alpha = mFadeOutAmount ;
363+ }
364+
365+ private void HideFade ( )
366+ {
367+ // Remains enabled since we're going to do fading.
368+ mCanvas . enabled = true ;
369+
370+ mFading = JCS_FadeType . OUT ;
371+ mFadeAlpa = mFadeOutAmount ;
372+ }
373+
374+ #endregion
375+
318376 /// <summary>
319377 /// Toggle the canvas' visibility.
320378 /// </summary>
321379 /// <param name="mute"> True to mute the sound. </param>
322- ///
323380 public void ToggleVisibility ( bool mute = false )
324- {
325- ToggleVisibility ( mFade , mute ) ;
326- }
327- public void ToggleVisibility ( bool fade , bool mute = false )
328381 {
329382 if ( IsShown ( ) )
330- Hide ( fade , mute ) ;
383+ Hide ( mute ) ;
331384 else
332- Show ( fade , mute ) ;
385+ Show ( mute ) ;
333386 }
334387
335388 /// <summary>
@@ -342,7 +395,7 @@ private void CheckMainCanvas()
342395
343396 if ( main != null )
344397 {
345- Debug . LogWarning ( "Having multiple main canvases is often not allowed: " + this . gameObject . name ) ;
398+ Debug . LogWarning ( $ "Having multiple main canvases is often not allowed: { gameObject . name } " ) ;
346399 return ;
347400 }
348401
0 commit comments