Skip to content

Commit cdbecc7

Browse files
committed
fix(UI): Ensure enabled when fading
1 parent 9e7830a commit cdbecc7

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

Assets/JCSUnity/Scripts/UI/JCS_Canvas.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using UnityEngine;
1111
using MyBox;
12+
using NUnit.Framework.Internal;
1213

1314
namespace JCSUnity
1415
{
@@ -35,14 +36,40 @@ public class JCS_Canvas : MonoBehaviour
3536
// Execution when canvas is hidden by fading.
3637
public Action<JCS_Canvas> onHideFade = null;
3738

38-
private Canvas mCanvas = null;
3939

40-
private CanvasGroup mCanvasGroup = null;
40+
41+
4142

4243
private RectTransform mAppRect = null; // Application Rect (Window)
4344

45+
#if UNITY_EDITOR
46+
[Separator("Helper Variables (JCS_Canvas)")]
47+
48+
[Tooltip("Turn on this to test this behaviour.")]
49+
[SerializeField]
50+
private bool mTest = false;
51+
52+
[Tooltip("Key to show canvas.")]
53+
[SerializeField]
54+
private KeyCode mKeyShow = KeyCode.A;
55+
56+
[Tooltip("Key to hide canvas.")]
57+
[SerializeField]
58+
private KeyCode mKeyHide = KeyCode.S;
59+
#endif
60+
4461
[Separator("Check Variables (JCS_Canvas)")]
4562

63+
[Tooltip("Canvas.")]
64+
[SerializeField]
65+
[ReadOnly]
66+
private Canvas mCanvas = null;
67+
68+
[Tooltip("Canvas group.")]
69+
[SerializeField]
70+
[ReadOnly]
71+
private CanvasGroup mCanvasGroup = null;
72+
4673
[Tooltip("Resize object.")]
4774
[SerializeField]
4875
[ReadOnly]
@@ -161,9 +188,26 @@ private void Start()
161188

162189
private void Update()
163190
{
191+
#if UNITY_EDITOR
192+
Test();
193+
#endif
194+
164195
DoFading();
165196
}
166197

198+
#if UNITY_EDITOR
199+
private void Test()
200+
{
201+
if (!mTest)
202+
return;
203+
204+
if (Input.GetKeyDown(mKeyShow))
205+
Show();
206+
else if (Input.GetKeyDown(mKeyHide))
207+
Hide();
208+
}
209+
#endif
210+
167211
/// <summary>
168212
/// Return the `canvas` that is the parent of the `trans` object.
169213
///
@@ -222,16 +266,16 @@ public void Show(bool fade, bool mute = false)
222266
if (!mute)
223267
JCS_SoundPlayer.PlayByAttachment(mDeactiveSound, JCS_SoundMethod.PLAY_SOUND);
224268

225-
if (fade)
269+
mCanvas.enabled = true;
270+
271+
if (fade && mCanvasGroup)
226272
{
227273
mFading = JCS_FadeType.IN;
228274

229275
mFadeAlpa = mFadeInAmount;
230276
}
231277
else
232278
{
233-
mCanvas.enabled = true;
234-
235279
if (mCanvasGroup != null)
236280
mCanvasGroup.alpha = mFadeInAmount;
237281
}
@@ -251,8 +295,11 @@ public void Hide(bool fade, bool mute = false)
251295
if (!mute)
252296
JCS_SoundPlayer.PlayByAttachment(mActiveSound, JCS_SoundMethod.PLAY_SOUND);
253297

254-
if (fade)
298+
if (fade && mCanvasGroup)
255299
{
300+
// Remains enabled since we're going to do fading.
301+
mCanvas.enabled = true;
302+
256303
mFading = JCS_FadeType.OUT;
257304

258305
mFadeAlpa = mFadeOutAmount;

Assets/_Project/Scenes/Sound/FT_SoundSlider.unity

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,15 @@ MonoBehaviour:
338338
m_Script: {fileID: 11500000, guid: 2d3ead9775289064eaae6c9fb96c82b3, type: 3}
339339
m_Name:
340340
m_EditorClassIdentifier:
341+
mTest: 0
342+
mKeyShow: 97
343+
mKeyHide: 115
344+
mCanvas: {fileID: 0}
345+
mCanvasGroup: {fileID: 0}
341346
mResizeUI: {fileID: 0}
342347
mDisplayOnAwake: 1
343348
mMainCanvas: 0
344-
mFade: 0
349+
mFade: 1
345350
mFadeFriction: 0.2
346351
mFadeInAmount: 1
347352
mFadeOutAmount: 0
@@ -395,7 +400,7 @@ Canvas:
395400
m_PrefabInstance: {fileID: 0}
396401
m_PrefabAsset: {fileID: 0}
397402
m_GameObject: {fileID: 140457477}
398-
m_Enabled: 1
403+
m_Enabled: 0
399404
serializedVersion: 3
400405
m_RenderMode: 1
401406
m_Camera: {fileID: 0}
@@ -1069,6 +1074,11 @@ MonoBehaviour:
10691074
m_Script: {fileID: 11500000, guid: 2d3ead9775289064eaae6c9fb96c82b3, type: 3}
10701075
m_Name:
10711076
m_EditorClassIdentifier:
1077+
mTest: 0
1078+
mKeyShow: 97
1079+
mKeyHide: 115
1080+
mCanvas: {fileID: 0}
1081+
mCanvasGroup: {fileID: 0}
10721082
mResizeUI: {fileID: 0}
10731083
mDisplayOnAwake: 1
10741084
mMainCanvas: 1

0 commit comments

Comments
 (0)