using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 对话事件系统,用于处理游戏中的对话事件。 /// public class DialogueEventSystem : MonoBehaviour { /// /// 事件动作类,用于存储事件名称、角色和选择结果映射。 /// [System.Serializable] public class EventAction { public string eventName; public string eventRole; public System.Action onChoiceSelected; //选择回调 public Dictionary choiceResults; //选择结果映射 } //事件注册表,用于存储事件名称和对应的事件动作对象。 private Dictionary eventRegistry = new Dictionary(); private void Start() { // 示例:注册一个事件 RegisterEvent("event1", "role1", (int choice) => { Debug.Log("选择了选项:" + choice); //TODO: 这里可以添加选择后的逻辑处理,例如根据选择的选项执行不同的操作。 // 示例:添加选择结果 eventRegistry["event1"].choiceResults[0] = "选项A"; eventRegistry["event1"].choiceResults[1] = "选项B"; eventRegistry["event1"].choiceResults[2] = "选项C"; // 更多选择结果可以根据需要添加... // 触发选择回调 eventRegistry["event1"].onChoiceSelected?.Invoke(choice); // 触发选择回调 eventRegistry["event2"].onChoiceSelected?.Invoke(choice); // 注意:这里使用了延迟调用,确保在注册事件后立即触发回调。 // 延迟调用,确保在注册事件后立即触发回调 Invoke("TriggerChoiceCallback", 0.1f); }); } /// /// 注册事件。 /// /// /// /// public void RegisterEvent(string eventName, string eventRole, System.Action callback) { EventAction eventAction = new EventAction() { eventName = eventName, eventRole = eventRole, onChoiceSelected = callback, choiceResults = new Dictionary() }; eventRegistry[eventName] = eventAction; } /// /// 移除事件。 /// /// public void UnRegisterEvent(string eventName) { if (eventRegistry.ContainsKey(eventName)) { eventRegistry.Remove(eventName); } } /// /// 尝试获取事件。如果事件存在,则返回true并填充action参数;否则返回false。 /// /// /// /// public bool TryGetEvent(string eventName, out EventAction action) { return eventRegistry.TryGetValue(eventName, out action); } }