DialogueManager.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LuaInterface;
  5. public delegate void DialogEndCallback(int dialogId);
  6. public class PlayDialogueInfo
  7. {
  8. public int dialogId;
  9. public LuaTable luaTable = null;
  10. public LuaFunction luaCallback = null;
  11. public DialogEndCallback callbackFun = null;
  12. public PlayDialogueInfo(int id)
  13. {
  14. dialogId = id;
  15. }
  16. public PlayDialogueInfo(int id,DialogEndCallback cb)
  17. {
  18. dialogId = id;
  19. callbackFun = cb;
  20. }
  21. public PlayDialogueInfo(int id,LuaTable luaTable,LuaFunction lua_cb)
  22. {
  23. dialogId = id;
  24. this.luaTable = luaTable;
  25. this.luaCallback = lua_cb;
  26. }
  27. }
  28. public class DialogManager : Singleton<DialogManager>
  29. {
  30. readonly Queue<PlayDialogueInfo> mDialogInfos = new Queue<PlayDialogueInfo>();
  31. Coroutine mRunningCoroutine;
  32. public bool IsDoingDialog { get { return mRunningCoroutine != null || mDialogInfos.Count > 0; } }
  33. public void PlayDialog(int dialogId)
  34. {
  35. if (mRunningCoroutine == null)
  36. mRunningCoroutine = UIMgr.Instance.StartCoroutine(DoDialog(dialogId));
  37. else
  38. mDialogInfos.Enqueue(new PlayDialogueInfo(dialogId));
  39. }
  40. IEnumerator DoDialog(int dialogId,DialogEndCallback cb = null,LuaTable luaTb = null, LuaFunction luaCb = null)
  41. {
  42. bool needRestoreStoryBlackLine = false;
  43. Transform storyBlackLine = UIMgr.Instance.transform.Find("StoryBlackLine");
  44. if (storyBlackLine != null && storyBlackLine.gameObject.activeInHierarchy)
  45. {
  46. needRestoreStoryBlackLine = true;
  47. storyBlackLine.gameObject.SetActive(false);
  48. }
  49. yield break;
  50. //JSONObject param = new JSONObject();
  51. //param.AddField("dialog_id", dialogId);
  52. //UIMgr.Instance.Open(UIPageIDs.PAGE_ID_DIALOG_NEW, param);
  53. //while (!mDialogPage.IsOpened)
  54. // yield return 1;
  55. //while (mDialogPage != null && mDialogPage.IsPlaying)
  56. // yield return 1;
  57. //mDialogPage.Close(null);
  58. //mDialogPage = null;
  59. if (needRestoreStoryBlackLine && storyBlackLine != null)
  60. storyBlackLine.gameObject.SetActive(true);
  61. if (cb != null)
  62. cb(dialogId);
  63. if (luaCb != null)
  64. luaCb.Call(luaTb, dialogId);
  65. if (mDialogInfos.Count > 0)
  66. {
  67. PlayDialogueInfo info = mDialogInfos.Dequeue();
  68. mRunningCoroutine = UIMgr.Instance.StartCoroutine(DoDialog(info.dialogId, info.callbackFun, info.luaTable, info.luaCallback));
  69. }
  70. else
  71. mRunningCoroutine = null;
  72. }
  73. }