29 lines
1009 B
C#
29 lines
1009 B
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class CharacterVoiceObject : MonoBehaviour
|
|
{
|
|
public CharacterData Character;
|
|
public AudioSource VoiceSource;
|
|
|
|
private static readonly Dictionary<CharacterData, CharacterVoiceObject> _registry = new();
|
|
|
|
// Character를 비워 두면 Dictionary 널 키로 예외가 나므로 등록하지 않는다.
|
|
// (화자 없는 대화는 CharacterVoiceObject 자체를 붙이지 않는 씬 플레이어로 처리한다)
|
|
private void OnEnable()
|
|
{
|
|
if (Character != null) _registry[Character] = this;
|
|
else Debug.LogWarning($"[CharacterVoiceObject] Character가 비어 있어 등록하지 않음: {name}");
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (Character != null) _registry.Remove(Character);
|
|
}
|
|
|
|
public static CharacterVoiceObject Find(CharacterData data)
|
|
=> _registry.TryGetValue(data, out var obj) ? obj : null;
|
|
|
|
public void Play(AudioClip clip) => VoiceSource.PlayOneShot(clip);
|
|
}
|