/// <summary> /// 检测对话 /// </summary> private void CheckTalk() { //创建新的 var touchIDs = npcIDToTalkShowStructDic.Keys.OfType <int>().ToArray(); foreach (int touchID in touchIDs) { TalkShowStruct talkShowStruct = npcIDToTalkShowStructDic[touchID]; float nextTime = talkShowStruct.nowTime + Time.deltaTime; var selectCreates = talkShowStruct.pointToCreateTime.Where(temp => temp.Value >= talkShowStruct.nowTime && temp.Value < nextTime).ToArray(); talkShowStruct.nowTime = nextTime; foreach (var selectCreate in selectCreates) { DialogueValue dialogueValue = dialogueStructData.SearchDialogueValueByID(selectCreate.Key.dialogueID); if (dialogueValue == null) { continue; } int thisDialogueNPCID = dialogueValue.npcID; //在哪个npc头顶显示 string thisDialogueValue = dialogueValue.showValue; //显示的文字 float intervalTime = talkShowStruct.pointToIntervalTime[selectCreate.Key]; //显示的持续时间 if (!npcIDToShowObjDic.ContainsKey(thisDialogueNPCID)) { GameObject createObj = GameObject.Instantiate <GameObject>(talkShowExplanObj);//这是一个UI面板(这个面板一经创建就不会消失了,但是正常情况下是隐藏的,只有当存在显示文字的时候才会显示) npcIDToShowObjDic.Add(thisDialogueNPCID, createObj); //设置位置 NPCDataInfo npcDataInfo = npcData.GetNPCDataInfo(iGameState.SceneName, thisDialogueNPCID); npcDataInfo.Load(); if (npcDataInfo != null) { createObj.transform.position = npcDataInfo.NPCObj.transform.position + npcDataInfo.TalkShowOffset; } } GameObject talkUIObj = npcIDToShowObjDic[thisDialogueNPCID];//取出用于显示文字的ui面板 UITalkShowControl uiTalkShowControl = talkUIObj.GetComponent <UITalkShowControl>(); if (uiTalkShowControl != null) { uiTalkShowControl.AddNewTalk(thisDialogueValue, intervalTime); } } if (talkShowStruct.nowTime > talkShowStruct.maxTime) { npcIDToTalkShowStructDic.Remove(touchID); } } if (npcIDToTalkShowStructDic.Count <= 0) { runTaskStruct.StopTask(); } }
/// <summary> /// 显示对话 /// </summary> /// <param name="dialogueCondition">对话的文本</param> private void ShowTalk(DialogueCondition dialogueCondition) { TalkShowStruct talkShowStruct = new TalkShowStruct(); Queue <int> targetTaskIDs = new Queue <int>(); Queue <DialoguePoint> targetTaskPoints = new Queue <DialoguePoint>(); PushDialoguePointTask(dialogueCondition.topPoint, targetTaskIDs, targetTaskPoints); float maxTime = 0; foreach (DialoguePoint targetTaskPoint in targetTaskPoints) { DialogueValue dialogueValue = dialogueStructData.SearchDialogueValueByID(targetTaskPoint.dialogueID); if (dialogueValue == null) { continue; } string otherValue = dialogueValue.otherValue; if (string.IsNullOrEmpty(otherValue)) { continue; } string[] splitsValue = otherValue.Split(','); if (splitsValue.Length == 2) { float startTime = 0; //开始时间 float intervalTime = 0; //持续时间 if (!(float.TryParse(splitsValue[0], out startTime) && float.TryParse(splitsValue[1], out intervalTime))) { continue; } float endTime = startTime + intervalTime; maxTime = maxTime > endTime ? maxTime : endTime; talkShowStruct.pointToCreateTime.Add(targetTaskPoint, startTime); talkShowStruct.pointToIntervalTime.Add(targetTaskPoint, intervalTime); } } talkShowStruct.maxTime = maxTime; npcIDToTalkShowStructDic.Add(dialogueCondition.touchNPCID, talkShowStruct); }