✅ 할일
📄 내용
→ 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public Text TimeTxt;
public Animator TimeTxtAnim;
public float WarningTime = 20.0f; //타이머 애니메이션 시작할 시간
public float WarningBackground = 25.0f; //애니메이션 시작할 시간
private GameManager _gameManager; //게임매니저에 접근하기 위한 변수
private Camera _mainCamera; //메인카메라
public Color StartColor;
public Color EndColor;
public float ColorChangeDuration = 10.0f; //배경색 변경에 걸리는 시간
// Start is called before the first frame update
void Start()
{
_gameManager = FindObjectOfType<GameManager>(); //게임매니저 찾기
_mainCamera = Camera.main; //메인카메라 찾기
}
// Update is called once per frame
void Update()
{
float time = _gameManager.GetTime(); //time에 시간 넣기
if(time >= WarningTime)
{
TimeTxtAnim.enabled = true;
}
//배경색 변경 (time - 시작 시간) / 변경에 걸리는 시간
float t = Mathf.Clamp01((time - WarningBackground) / ColorChangeDuration * 2); //보간에 사용될 시간 값을 계산
_mainCamera.backgroundColor = Color.Lerp(StartColor, EndColor, t);
}
}
- 게임 시간 25초가 되면 배경색이 바뀌는 기능 추가
Lerp() 두 개의 색상 사이의 보간값(Interpolation)을 계산하는 함수
⇒ 수정된 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public Text TimeTxt;
public Animator TimeTxtAnim;
public Color StartColor;
public Color EndColor;
private GameManager _gameManager; //게임매니저에 접근하기 위한 변수
private Camera _mainCamera; //메인카메라
private float _warningTime = 20.0f; //타이머 애니메이션 시작할 시간
private float _warningBackground = 25.0f; //애니메이션 시작할 시간
private float _colorChangeDuration = 10.0f; //배경색 변경에 걸리는 시간
void Start()
{
_gameManager = FindObjectOfType<GameManager>(); //게임매니저 찾기
_mainCamera = Camera.main; //메인카메라 찾기
}
// Update is called once per frame
void Update()
{
float time = _gameManager.GetTime(); //time에 시간 넣기
if(time >= _warningTime)
{
TimeTxtAnim.enabled = true;
}
//배경색 변경 (time - 시작 시간) / 변경에 걸리는 시간
float t = Mathf.Clamp01((time - _warningBackground) / _colorChangeDuration * 2); //보간에 사용될 시간 값을 계산
_mainCamera.backgroundColor = Color.Lerp(StartColor, EndColor, t);
}
}
- ColorChangeDuration
- public 에서 private으로 변경
- 변수명 수정
_colorChangeDuration
- Color
- Unity에서 조작하기 위해 public 유지
→ 컴포넌트

- Start Color
5A5AE1
- End Color
EC6262
💫Troubleshooting