마지막 과제 - 쿨타임 버튼 만들기 (코루틴을 이용한)
필드
public 버튼, 이미지(Image front), 텍스트(TMP_Text)
private 쿨타임 시간, ready=true/false, 경과시간
start()
{
버튼.onClick.AddListener(() => {
if(ready = false)
{
쿨타임중 => 출력
}
else
{
StarCoroutine(this.코루틴호출);
}
});
timeText.gameObject.SetActive(true);
IEnumerator Co...()
{ false일때
front.fillAmount = 0;
timeText.gameObject.SetActive(true);
while(true)
{
경과시간 += deltaTime;
fillAmount = 경과시간 / 쿨타임;
fillAmount = fillAmount;
절대값
int time = Mathf.RoundToInt(쿨타임-경과시간)
timeText.text = time값 출력
if(경과시간 >= 쿨타임)
{
경과시간 = 0;
break;
}
yield return null; => 반환(코루틴필수)
"스킬준비" => 출력
timeText.gameObject.SetActive(true);
true;
버튼만들기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestComponent1Main : MonoBehaviour
{
public SwitchButton switchButton;
public CheckBoxButton checkBoxButton;
// Start is called before the first frame update
void Start()
{
switchButton.onChangeState = (state) =>
{
Debug.Log($"스위치 버튼을 눌렀습니다 {state}");
};
checkBoxButton.oncheckstate = (state) =>
{
Debug.Log($"체크박스 버튼을 눌렀습니다 {state}");
};
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestComponent1Main : MonoBehaviour
{
// 변수위나 앞에 작성
//외부로 노출 됨 => 프라이빗으로 작성했어도 어싸인가능하게 할수있음
//[SerializeField] private Button btnBlue;
//[SerializeField] private Button btnYellow;
//[SerializeField] private Button btnGreen;
//public Button[] btns;
public List<Button> buttonList;
// Start is called before the first frame update
void Start()
{
//this.btnBlue.onClick.AddListener(() => {
// Debug.Log("파랑색 버튼이 클릭 되었습니다");
//});this.btnYellow.onClick.AddListener(() => {
// Debug.Log("파랑색 버튼이 클릭 되었습니다");
//});this.btnGreen.onClick.AddListener(() => {
// Debug.Log("파랑색 버튼이 클릭 되었습니다");
//});
Debug.Log(buttonList); // 리스트의 인스턴스
Debug.Log(buttonList.Count); // 리스트의 요소수
// 요소의 타입 : button
for(int i = 0; i < buttonList.Count; i++)
{
//캡쳐
int index = i;
Button btn = buttonList[index];
btn.onClick.AddListener(() => {
Debug.Log(index); // i로 출력하면 2로 출력 => 클로져현상 => 상위적용된
});
}
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestComponent1Main : MonoBehaviour
{
[SerializeField] private Slider slider;
// Start is called before the first frame update
void Start()
{
this.slider.onValueChanged.AddListener((value) =>
{
Debug.Log(value);
});
}
'수업내용' 카테고리의 다른 글
쉐이더_렌더링파이프라인 / 밤송이(raycast) (0) | 2024.09.02 |
---|---|
json (0) | 2024.08.27 |
ClimbCloud(20240822) (0) | 2024.08.22 |
CatEscape(20240821) 진행중 / 화살표 공장까지 완성 (0) | 2024.08.21 |
20240820 수업내용 (0) | 2024.08.20 |