Advertisement
PotiSolhdoost

Untitled

Jul 3rd, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7.  
  8. public class OpenAIManager : MonoBehaviour
  9. {
  10. private string serverURL = "https://openaichess.soroushmirzaee.repl.co/query/";
  11. string query;
  12. public TextMeshProUGUI response;
  13. public string stringArray;
  14. public float timeBtChar, timeBtWords;
  15. int i = 0;
  16.  
  17.  
  18. private void Start()
  19. {
  20. query = "Teach in the simplest way possible how a rook moves in chess";
  21. SetQuery(query);
  22. }
  23.  
  24. public void SetQuery(string q)
  25. {
  26. query = q;
  27. StartCoroutine(SendGetRequest(serverURL + query));
  28. }
  29. IEnumerator SendGetRequest(string url)
  30. {
  31. string resp;
  32. using(UnityWebRequest webRequest = UnityWebRequest.Get(url))
  33. {
  34. yield return webRequest.SendWebRequest();
  35.  
  36. string[] pages = url.Split('/');
  37. int page = pages.Length - 1;
  38.  
  39. if(webRequest.isNetworkError)
  40. {
  41. Debug.Log(pages[page] + ": Error: " + webRequest.error);
  42. }
  43. else
  44. {
  45. resp = webRequest.downloadHandler.text;
  46. Debug.Log(pages[page] + ":\nReceived: " + resp);
  47. stringArray = resp;
  48. EndCheck();
  49. }
  50. }
  51. }
  52. void EndCheck()
  53. {
  54. if(i <= stringArray.Length - 1)
  55. {
  56. //response.text += stringArray[i];
  57. StartCoroutine(VisibleText());
  58. }
  59.  
  60. }
  61. private IEnumerator VisibleText()
  62. {
  63. response.text = "";
  64. response.ForceMeshUpdate();
  65. foreach( char letter in stringArray.ToCharArray() ) {
  66. response.text += letter;
  67. yield return new WaitForSeconds(timeBtChar);
  68. }
  69. }
  70.  
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement