57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Assembly : Unity
|
|||
|
// Author : Kimch
|
|||
|
// Created : 2018-03-29
|
|||
|
//
|
|||
|
// Last Modified By :
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "KUIToggle" company="Kunpo"></copyright>
|
|||
|
// <summary></summary>
|
|||
|
// ***********************************************************************
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
[RequireComponent(typeof(Toggle))]
|
|||
|
public class KUIToggle : MonoBehaviour
|
|||
|
{
|
|||
|
public Graphic onGraphic;
|
|||
|
public Graphic offGrphic;
|
|||
|
|
|||
|
private Toggle.ToggleTransition _toggleTransition;
|
|||
|
|
|||
|
private void OnToggleValueChanged(bool value)
|
|||
|
{
|
|||
|
PlayEffect(onGraphic, value, _toggleTransition == Toggle.ToggleTransition.None);
|
|||
|
PlayEffect(offGrphic, !value, _toggleTransition == Toggle.ToggleTransition.None);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Play the appropriate effect.
|
|||
|
/// </summary>
|
|||
|
private void PlayEffect(Graphic graphic, bool isOn, bool instant)
|
|||
|
{
|
|||
|
if (graphic == null)
|
|||
|
return;
|
|||
|
|
|||
|
#if UNITY_EDITOR
|
|||
|
if (!Application.isPlaying)
|
|||
|
graphic.canvasRenderer.SetAlpha(isOn ? 1f : 0f);
|
|||
|
else
|
|||
|
#endif
|
|||
|
graphic.CrossFadeAlpha(isOn ? 1f : 0f, instant ? 0f : 0.1f, true);
|
|||
|
}
|
|||
|
|
|||
|
#region Unity
|
|||
|
|
|||
|
private void Start()
|
|||
|
{
|
|||
|
var toggle = GetComponent<Toggle>();
|
|||
|
toggle.onValueChanged.AddListener(OnToggleValueChanged);
|
|||
|
_toggleTransition = toggle.toggleTransition;
|
|||
|
OnToggleValueChanged(toggle.isOn);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|