Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Networking;
- public class OpenAIManager : MonoBehaviour
- {
- private string serverURL = "https://openaichess.soroushmirzaee.repl.co/query/";
- string query;
- public TextMeshProUGUI response;
- public string stringArray;
- public float timeBtChar, timeBtWords;
- int i = 0;
- private void Start()
- {
- query = "Teach in the simplest way possible how a rook moves in chess";
- SetQuery(query);
- }
- public void SetQuery(string q)
- {
- query = q;
- StartCoroutine(SendGetRequest(serverURL + query));
- }
- IEnumerator SendGetRequest(string url)
- {
- string resp;
- using(UnityWebRequest webRequest = UnityWebRequest.Get(url))
- {
- yield return webRequest.SendWebRequest();
- string[] pages = url.Split('/');
- int page = pages.Length - 1;
- if(webRequest.isNetworkError)
- {
- Debug.Log(pages[page] + ": Error: " + webRequest.error);
- }
- else
- {
- resp = webRequest.downloadHandler.text;
- Debug.Log(pages[page] + ":\nReceived: " + resp);
- stringArray = resp;
- EndCheck();
- }
- }
- }
- void EndCheck()
- {
- if(i <= stringArray.Length - 1)
- {
- //response.text += stringArray[i];
- StartCoroutine(VisibleText());
- }
- }
- private IEnumerator VisibleText()
- {
- response.text = "";
- response.ForceMeshUpdate();
- foreach( char letter in stringArray.ToCharArray() ) {
- response.text += letter;
- yield return new WaitForSeconds(timeBtChar);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement