Created
July 29, 2023 23:23
-
-
Save gschian0/c7d8cbb397880618c545838b41c34c24 to your computer and use it in GitHub Desktop.
Call Replicate.com API from Unity Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //use unity to call replicate api and populate the texture of the 3d object with | |
| //put this script on it with the generated image | |
| using UnityEngine; | |
| using UnityEngine.Networking; | |
| using SimpleJSON; | |
| using System.Collections; | |
| public class repCurl : MonoBehaviour | |
| { | |
| private Renderer objectRenderer; // Reference to the Renderer component of the GameObject | |
| private void Start() | |
| { | |
| objectRenderer = GetComponent<Renderer>(); // Get the Renderer component from the current GameObject | |
| StartCoroutine(RunReplicate()); | |
| } | |
| private IEnumerator RunReplicate() | |
| { | |
| string apiUrl = "https://api.replicate.com/v1/predictions"; | |
| string apiToken = "YOUR_API_TOKEN"; // Replace with your actual API token | |
| string jsonData = "{\"version\": \"2b017d9b67edd2ee1401238df49d75da53c523f36e363881e057f5dc3ed3c5b2\", \"input\": {\"prompt\": \"A Private Dancer\"}}"; | |
| using (UnityWebRequest request = new UnityWebRequest(apiUrl, "POST")) | |
| { | |
| byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData); | |
| request.uploadHandler = new UploadHandlerRaw(bodyRaw); | |
| request.downloadHandler = new DownloadHandlerBuffer(); | |
| request.SetRequestHeader("Authorization", "Token " + apiToken); | |
| request.SetRequestHeader("Content-Type", "application/json"); | |
| yield return request.SendWebRequest(); | |
| if (request.result != UnityWebRequest.Result.Success) | |
| { | |
| Debug.LogError("Error: " + request.error); | |
| } | |
| else | |
| { | |
| string responseBody = request.downloadHandler.text; | |
| Debug.Log(responseBody); // Log the response to Unity console | |
| // Parse the JSON response using SimpleJSON | |
| JSONNode jsonResponse = JSON.Parse(responseBody); | |
| if (jsonResponse != null) | |
| { | |
| string predictionId = jsonResponse["id"].Value; | |
| Debug.Log(predictionId); | |
| // Use a coroutine to wait for 1 second | |
| yield return new WaitForSeconds(1.0f); | |
| // Create a new GET request to fetch the updated prediction | |
| string predictionUrl = $"{apiUrl}/{predictionId}"; | |
| using (UnityWebRequest predictionRequest = UnityWebRequest.Get(predictionUrl)) | |
| { | |
| predictionRequest.SetRequestHeader("Authorization", "Token " + apiToken); | |
| yield return predictionRequest.SendWebRequest(); | |
| if (predictionRequest.result != UnityWebRequest.Result.Success) | |
| { | |
| Debug.LogError("Error fetching prediction: " + predictionRequest.error); | |
| } | |
| else | |
| { | |
| string predictionResponse = predictionRequest.downloadHandler.text; | |
| Debug.Log("Complete Prediction Response:\n" + predictionResponse); | |
| // Parse the updated JSON response | |
| JSONNode updatedJsonResponse = JSON.Parse(predictionResponse); | |
| string imageUrl = updatedJsonResponse["output"][0].Value; | |
| Debug.Log(imageUrl); | |
| // Create a UnityWebRequest to download the image from the updated URL | |
| using (UnityWebRequest imageRequest = UnityWebRequestTexture.GetTexture(imageUrl)) | |
| { | |
| yield return imageRequest.SendWebRequest(); | |
| if (imageRequest.result != UnityWebRequest.Result.Success) | |
| { | |
| Debug.LogError("Error downloading image: " + imageRequest.error); | |
| } | |
| else | |
| { | |
| // Load the downloaded image data into a Texture2D | |
| Texture2D imageTexture = DownloadHandlerTexture.GetContent(imageRequest); | |
| // Apply the imageTexture to the current GameObject's material | |
| if (objectRenderer != null && objectRenderer.material != null) | |
| { | |
| objectRenderer.material.mainTexture = imageTexture; | |
| } | |
| else | |
| { | |
| Debug.LogError("Renderer or Material not found on the current GameObject."); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| else | |
| { | |
| Debug.LogError("Invalid or missing JSON response."); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment