using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.SceneManagement; using Yarn.Unity; namespace AibisDream { public class DialogController : Singleton { private DialogueRunner dialogueRunner; private Dictionary> commandMap = new Dictionary>(); private void Start() { dialogueRunner = GetComponentInChildren(); } /// /// 开启对话 /// /// public void StartDialog(YarnProject yarnProject) { dialogueRunner.SetProject(yarnProject); dialogueRunner.StartDialogue("Start"); } /// /// 执行指令 /// /// 指令名称 public void InvokeOptionCommand(string commandName, YarnOption[] options) { if (commandName == null) { } if (commandMap.TryGetValue(commandName, out var action)) { action(options); } else { Debug.LogWarning($"没有找到 {commandName} 命令"); } } /// /// 注册带选项指令 /// /// 指令名称 /// 指令函数 public void RegisterOptionCommand(string commandName, Action commandAction) { if (commandMap.ContainsKey(commandName)) { Debug.Log($"命令 {commandName} 并未注册,重新注册会替换旧指令"); } commandMap[commandName] = commandAction; } /// /// 卸载指令 /// /// 指令名称 public void RemoveOptionCommand(string commandName) { if (commandMap.ContainsKey(commandName)) { commandMap.Remove(commandName); } else { Debug.Log($"命令 {commandName} 并未注册,重新注册会替换旧指令"); } } #region 各种命令 [YarnCommand("switch_tv")] public static void SwitchTV(string picName) { Debug.Log($"switch_tv {picName}"); } [YarnCommand("indoor")] public static void Indoor() { MainUIController.Instance.HideMainUI(); } [YarnCommand("character_into")] public static void CharacterInto(string picName) { Debug.Log($"character_into {picName}"); } [YarnCommand("character_moveto_fix")] public static void CharacterMoveToFix() { Debug.Log("character_moveto_fix"); } [YarnCommand("cover")] public static void Cover() { Debug.Log("Cover"); } [YarnCommand("load_scene")] public static IEnumerator LoadScene(string sceneName) { yield return SceneLoader.Instance.LoadSceneAsync(sceneName); } [YarnCommand("init_fix")] public static void InitFix() { } [YarnCommand("hide_dialog")] public static void HideDialog() { DialogUiViewer.Instance.HideDialog(); } #endregion } }