50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
// 시야 가림용 구체(Sight)가 쓰는 머티리얼 — 알파 0(투명)이면 정상 시야, 1(불투명)이면 암전
|
|
[SerializeField] private Material _sightMat;
|
|
|
|
private static readonly int _baseColorId = Shader.PropertyToID("_BaseColor");
|
|
private static readonly int _colorId = Shader.PropertyToID("_Color");
|
|
|
|
private void Awake()
|
|
{
|
|
// 머티리얼 에셋을 직접 수정하므로 이전 씬에서 암전된 알파가 남아 넘어올 수 있다 — 씬 시작은 항상 투명
|
|
SetSightAlpha(0f);
|
|
}
|
|
|
|
// onoff=true: 시야 복구(투명하게), false: 시야 암전(불투명하게). 완료 시점이 필요하면 await 가능.
|
|
public async Awaitable FadeSight(bool onoff, float duration)
|
|
{
|
|
if (_sightMat == null)
|
|
{
|
|
Debug.LogWarning("[PlayerController] _sightMat이 비어 있어 시야 페이드를 건너뜁니다.");
|
|
return;
|
|
}
|
|
|
|
float start = _sightMat.GetColor(AlphaProp).a;
|
|
float end = onoff ? 0f : 1f;
|
|
|
|
float timer = 0f;
|
|
while (timer < duration)
|
|
{
|
|
timer += Time.deltaTime;
|
|
SetSightAlpha(Mathf.Lerp(start, end, Mathf.Clamp01(timer / duration)));
|
|
await Awaitable.NextFrameAsync(destroyCancellationToken);
|
|
}
|
|
SetSightAlpha(end);
|
|
}
|
|
|
|
// URP 셰이더는 _BaseColor, 레거시/커스텀 셰이더는 _Color를 쓴다
|
|
private int AlphaProp => _sightMat.HasProperty(_baseColorId) ? _baseColorId : _colorId;
|
|
|
|
private void SetSightAlpha(float alpha)
|
|
{
|
|
if (_sightMat == null) return;
|
|
Color color = _sightMat.GetColor(AlphaProp);
|
|
color.a = alpha;
|
|
_sightMat.SetColor(AlphaProp, color);
|
|
}
|
|
}
|