Text-To-Image

SkillfulAPI.TextToImage

Overview

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 GenerateImage method 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 Texture2D format) to a Sprite using a utility method.

  • Assigns the Sprite to the Image component for display in the Unity scene.

Usage Example

  1. Add the Script to a GameObject:

    • Attach the TextToImage script to any GameObject in your Unity scene.

  2. Assign the Image Component:

    • In the Unity Inspector, assign the Image component where you want the generated image to be displayed.

  3. Run the Scene:

    • Upon starting the scene, the GenerateImage method 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 Image component.

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