-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHowToUseInUnity.cs
More file actions
74 lines (60 loc) · 2.07 KB
/
HowToUseInUnity.cs
File metadata and controls
74 lines (60 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using UnityEngine;
using Gemini;
using System.Threading.Tasks;
using System.IO;
public class Communication : MonoBehaviour
{
// Lock and Synchronization
private bool lproj;
// Gemini API
private GeminiTextRequest geminiTextRequest;
// Global
private string llmOutput;
void Start()
{
lproj = true;
llmOutput = "";
geminiTextRequest = new GeminiTextRequest();
}
/**
<summary>
You can pass as many strings as you want, they can be images, videos, cloud files, etc. as long as you change the config in GeminiTextRequest.cs
The first input must be text input.
</summary>
*/
void Run()
{
// Specify your PNG file path
string filePath = "path/to/your/image.png";
// Convert PNG to Base64 string
string base64String = ConvertToBase64(filePath);
yield return StartCoroutine(CommunicationWithGemini("Looks at my images!", base64String));
Debug.Log($"Response is: {llmOutput}");
}
string ConvertToBase64(string filePath)
{
// Read the file into a byte array
byte[] imageBytes = File.ReadAllBytes(filePath);
// Convert the byte array to a Base64 string
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
IEnumerator CommunicationWithGemini(params string[] strings)
{
yield return new WaitUntil(() => lproj);
lproj = false;
// Start the async operation in a way that doesn't block the Unity main thread
// Directly using GenerateContent within Task.Run
Task<GeminiTextResponse> llmTask = Task.Run(async () => await geminiTextRequest.SendMsg(strings));
// Wait until the async task has completed
while (!llmTask.IsCompleted)
{
yield return null;
}
GeminiTextResponse response = llmTask.Result; // Access the Result property here
string tempOut = response.candidates[0].content.parts[0].text;
llmOutput = tempOut;
// Release lock
lproj = true;
}
}