↓ChatGPTさんにドキュメント読んでもらって書いてもらったものそのまま
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class GoogleGeminiAPI : MonoBehaviour
{
private string apiKey = "A"; // APIキーを設定
private string apiUrl = "https://generativelanguage.googleapis.com/v1beta3/models/text-bison-001:generateText";
void Start()
{
StartCoroutine(SendRequestToGemini("Write a story about a magic backpack"));
}
IEnumerator SendRequestToGemini(string promptText)
{
string requestData = "{\"prompt\": {\"text\": \"" + promptText + "\"}}";
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(requestData);
UnityWebRequest request = new UnityWebRequest(apiUrl + "?key=" + apiKey, "POST");
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error: " + request.error);
}
else
{
Debug.Log("Response: " + request.downloadHandler.text);
// ここでレスポンスの処理を行います
}
}
}