DialogueScript.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. public class DialogueScript : MonoBehaviour
  7. {
  8. public GameObject CanvasParent;
  9. public GameObject AiBtn;
  10. public RectTransform RectTransform;
  11. public RectTransform AiShowBtn;
  12. public InputField inputField;
  13. public Text messgaeText;
  14. public string ask = "换流变压器包含什么";
  15. public string userID = "1a1123fds12";
  16. public string chatId = "0e841c01-a8c0-11f0-8363-10ffe0282e4b";
  17. public string InputUrl;
  18. public float moveTime;
  19. private bool ifShow = false;
  20. private void Start()
  21. {
  22. btnOnclickShowCanvas();
  23. //RectTransform.anchoredPosition = new Vector2( Screen.width, 0);
  24. }
  25. public void SendRequest()
  26. {
  27. if (inputField.text.Equals(""))
  28. {
  29. Debug.LogError("输入为空");
  30. }
  31. else
  32. {
  33. ask = inputField.text;
  34. StartCoroutine(SendRequestIE());
  35. }
  36. }
  37. IEnumerator SendRequestIE()
  38. {
  39. string url = InputUrl + "userId=" + userID + "&chatId=" + chatId + "&ask=" + UnityWebRequest.EscapeURL(ask);
  40. UnityWebRequest request = UnityWebRequest.Get(url);
  41. request.SetRequestHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxOTc4MzgxNDE1NTc2NDUzMTIyIiwicm5TdHIiOiJGQlg2TXlWbzFuTUE1WU9Lb3B0UTk0cmJxZGREeGJMeCIsImNsaWVudGlkIjoiYmRkY2QwOTZmMGJkMzE1MjAwODhmNmVkNTFiOGQxNTgiLCJ0ZW5hbnRJZCI6IjAwMDAwMCIsInVzZXJJZCI6MTk3ODM4MTQxNTU3NjQ1MzEyMiwidXNlck5hbWUiOiJ4OXQ0azJ2OG4iLCJkZXB0SWQiOjE5NzgzOTMxNDU4Nzg2OTU5MzcsImRlcHROYW1lIjoi56ys5LiJ5pa55o6I5p2D6YOo6ZeoIiwiZGVwdENhdGVnb3J5IjoiIn0.c99_dEsWkWmDcLQmmkqwNgU4DvJR9QGKueQpB6Y7LoE");
  42. request.SetRequestHeader("clientId", "bddcd096f0bd31520088f6ed51b8d158");
  43. request.timeout = 300;
  44. messgaeText.text = "数据响应加载中...";
  45. yield return request.SendWebRequest();
  46. if (request.error == null)
  47. {
  48. Debug.Log(request.downloadHandler.text);
  49. getMessage message = JsonUtility.FromJson<getMessage>(request.downloadHandler.text);
  50. messgaeText.text = message.msg.Replace(" ", "\u00A0");
  51. if (messgaeText.text.Equals("timeout"))
  52. {
  53. messgaeText.text = "请求超时,请重新发送...";
  54. }
  55. else if (messgaeText.text.Equals(""))
  56. {
  57. messgaeText.text = "数据解析失败,请重新发送...";
  58. }
  59. }
  60. else
  61. {
  62. Debug.Log("报错:" + request.error);
  63. Debug.Log("响应代码:" + request.responseCode);
  64. if (request.error.Equals("Request timeout"))
  65. {
  66. messgaeText.text = "请求超时,请重新发送...";
  67. }
  68. }
  69. request.Dispose();
  70. }
  71. public void btnOnclickShowCanvas()
  72. {
  73. RectTransform.transform.gameObject.SetActive(ifShow);
  74. inputField.text = "";
  75. messgaeText.text = "";
  76. ifShow = !ifShow;
  77. }
  78. public void PointerShowAIBtn()
  79. {
  80. StopAllCoroutines();
  81. StartCoroutine(showAiBtn());
  82. }
  83. IEnumerator showAiBtn()
  84. {
  85. float Ttime = 0;
  86. while (Ttime < 1 )
  87. {
  88. AiShowBtn.anchoredPosition = new Vector2(Mathf.Lerp(AiShowBtn.anchoredPosition.x, -25, Ttime / 1), 0);
  89. Ttime += Time.deltaTime;
  90. yield return null;
  91. }
  92. }
  93. public void PointerHideAIBtn()
  94. {
  95. StopAllCoroutines();
  96. StartCoroutine(hideAiBtn());
  97. }
  98. IEnumerator hideAiBtn()
  99. {
  100. float Ttime = 0;
  101. while (Ttime < 1)
  102. {
  103. AiShowBtn.anchoredPosition = new Vector2(Mathf.Lerp(AiShowBtn.anchoredPosition.x, 0, Ttime / 1), 0);
  104. Ttime += Time.deltaTime;
  105. yield return null;
  106. }
  107. }
  108. class getMessage{
  109. public string msg;
  110. public string code;
  111. public string chatId;
  112. }
  113. }