91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Studio.UVC.UI
|
|
{
|
|
/// <summary>
|
|
/// On/Off 상태를 나타내는 버튼
|
|
/// </summary>
|
|
public class UVCSwitchButton : MonoBehaviour
|
|
{
|
|
private RectTransform rect;
|
|
private Button button_switch;
|
|
private RectTransform switch_rect;
|
|
private Image image_background;
|
|
|
|
private bool selected = false;
|
|
public bool Selected { get { return selected; } }
|
|
|
|
[SerializeField]
|
|
private float space;
|
|
[SerializeField]
|
|
private float speed_MoveTo;
|
|
[SerializeField]
|
|
private float duration_MoveTo;
|
|
|
|
private float switchBtnSize;
|
|
private float bgWidthSize;
|
|
|
|
private float process;
|
|
public Action<bool> onClickSwitch;
|
|
private float timer;
|
|
|
|
[Header("BG_Color")]
|
|
[SerializeField]
|
|
private Color normalBG;
|
|
[SerializeField]
|
|
private Color SelectBG;
|
|
public void Init()
|
|
{
|
|
rect = GetComponent<RectTransform>();
|
|
button_switch = GetComponentInChildren<Button>();
|
|
switch_rect = button_switch.GetComponent<RectTransform>();
|
|
button_switch.onClick.AddListener(OnClickSwichBtn);
|
|
var images = GetComponentsInChildren<Image>();
|
|
image_background = images.FirstOrDefault(x => x.name.Equals(nameof(image_background), System.StringComparison.OrdinalIgnoreCase));
|
|
|
|
image_background.color = normalBG;
|
|
switchBtnSize = switch_rect.sizeDelta.x;
|
|
bgWidthSize = rect.sizeDelta.x;
|
|
}
|
|
|
|
private void OnClickSwichBtn()
|
|
{
|
|
StopCoroutine(SwitchOnOff());
|
|
selected = !selected;
|
|
StartCoroutine(SwitchOnOff());
|
|
}
|
|
|
|
IEnumerator SwitchOnOff()
|
|
{
|
|
ProcessWinding();
|
|
var pos = selected ? new Vector2(bgWidthSize - (switchBtnSize + space), switch_rect.anchoredPosition.y) : new Vector2(space, switch_rect.anchoredPosition.y);
|
|
|
|
while (process < 1f)
|
|
{
|
|
timer += speed_MoveTo * 0.1f;
|
|
process += timer / duration_MoveTo;
|
|
switch_rect.anchoredPosition = Vector2.Lerp(switch_rect.anchoredPosition,pos, process);
|
|
yield return null;
|
|
}
|
|
|
|
image_background.color = selected ? SelectBG : normalBG;
|
|
timer = 0;
|
|
onClickSwitch?.Invoke(selected);
|
|
}
|
|
|
|
|
|
private void ProcessWinding()
|
|
{
|
|
timer = 0;
|
|
if (process > 0f)
|
|
{
|
|
process = 1f - process;
|
|
}
|
|
}
|
|
}
|
|
}
|