/// <summary> /// 添加新的对话 /// </summary> /// <param name="talkValue">对话内容</param> /// <param name="timeDuration">对话的持续时间</param> public void AddNewTalk(string talkValue, float timeDuration) { if (talkShowObjStructList == null || thisCanvas == null || ExplanObj == null) { return; } GameObject createObj = GameObject.Instantiate <GameObject>(ExplanObj); createObj.transform.SetParent(backRect); createObj.SetActive(true); RectTransform panel = createObj.GetComponent <RectTransform>(); panel.localScale = Vector3.one; panel.localEulerAngles = Vector2.zero; panel.localPosition = new Vector3(panel.localPosition.x, panel.localPosition.y, 0); Text text = panel.GetChild(1).GetComponent <Text>(); text.text = talkValue; TalkShowObjStruct talkShowObjStruct = new TalkShowObjStruct() { TimeRemaining = timeDuration, Panel = panel, Text = text, CanvasGroup = panel.GetComponent <CanvasGroup>() }; talkShowObjStructList.Add(talkShowObjStruct); }
private void Update() { float nowHeight = 0; //更新位置 for (int i = 0; i < talkShowObjStructList.Count; i++) { TalkShowObjStruct talkShowObjStruct = talkShowObjStructList[i]; talkShowObjStruct.TimeRemaining -= Time.deltaTime; if (talkShowObjStruct.TimeRemaining > 1) { if (talkShowObjStruct.CanvasGroup.alpha < 1) { talkShowObjStruct.CanvasGroup.alpha += Time.deltaTime; } } else if (talkShowObjStruct.TimeRemaining >= 0) { talkShowObjStruct.CanvasGroup.alpha = talkShowObjStruct.TimeRemaining; } else { GameObject.Destroy(talkShowObjStruct.Panel.gameObject); talkShowObjStructList.Remove(talkShowObjStruct); continue; } //查看此时的text高度 float tempHeight = talkShowObjStruct.Text.GetComponent <RectTransform>().rect.height; //不需要设置面板高度,因为面板是被上层控制大小的 nowHeight += tempHeight + 15; } //Rect canvasRect = thisCanvas.pixelRect; //canvasRect.height = nowHeight; RectTransform rectTrans = thisCanvas.GetComponent <RectTransform>(); rectTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, nowHeight); //根据摄像机更新方向 Camera mainCamera = Camera.main; Vector3 cameraForward = mainCamera.transform.forward; cameraForward.y = 0; cameraForward.Normalize(); thisCanvas.transform.forward = cameraForward; }