Text-To-Image
SkillfulAPI.TextToImage
SkillfulAPI.TextToImageOverview
The TextToImage function provides functionality to generate an image based on a specified input text. The generated image is then displayed in a Unity Image component. This feature can be used for visualizing textual descriptions or creating dynamic content in your game.
Class Definition
using SkillfulAI.API;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextToImage : MonoBehaviour
{
public Image image; // The Image component where the generated image will be displayed
/// <summary>
/// Unity's Start method, called when the script is initialized. Initiates the image generation process.
/// </summary>
private void Start()
{
GenerateImage();
}
/// <summary>
/// Initiates image generation based on a specified input text.
/// </summary>
public void GenerateImage()
{
// Input text used to generate the image
string inputText = "A blue fish";
// Perform text to image conversion
SkillfulAPI.TextToImage(inputText, _image =>
{
// Convert the generated image to a Sprite and assign it to the Image component
image.sprite = Utilities.Texture2DToSprite(_image);
});
}
}Detailed Explanation
Start Method
Description: Unity's Start method is called when the script is first initialized. This method triggers the image generation process.
Code:
private void Start()
{
GenerateImage();
}Functionality:
Automatically invokes the
GenerateImagemethod when the script starts.Ideal for initializing processes or setting up functionalities as soon as the script is activated.
GenerateImage Method
Description: The GenerateImage method sends a specified input text to the SkillfulAI API to generate an image and assigns the result to a Unity Image component.
Code:
public void GenerateImage()
{
// Input text used to generate the image
string inputText = "A blue fish";
// Perform text to image conversion
SkillfulAPI.TextToImage(inputText, _image =>
{
// Convert the generated image to a Sprite and assign it to the Image component
image.sprite = Utilities.Texture2DToSprite(_image);
});
}Functionality:
Sends the input text to the SkillfulAI API for image generation.
Converts the generated image (in
Texture2Dformat) to aSpriteusing a utility method.Assigns the
Spriteto theImagecomponent for display in the Unity scene.
Usage Example
Add the Script to a GameObject:
Attach the
TextToImagescript to any GameObject in your Unity scene.
Assign the Image Component:
In the Unity Inspector, assign the
Imagecomponent where you want the generated image to be displayed.
Run the Scene:
Upon starting the scene, the
GenerateImagemethod will be invoked automatically.The specified input text will be sent to the SkillfulAI API, and the generated image will be displayed in the assigned
Imagecomponent.
Conclusion
The TextToImage function illustrates a simple implementation of text-to-image conversion using the SkillfulAI Gaming SDK. By generating and displaying images based on textual descriptions, you can add dynamic visual content to your Unity project, enhancing user experience and creativity.
Last updated