The Unity Function Calling Framework is a simple yet powerful tool that integrates OpenAI into Unity games. It allows you to call functions within your game using AI decision-making, providing flexibility and adaptability to your game mechanics.
The framework utilizes attributes Tool
and Parameter
to define tools and parameters for methods in your Unity scripts. These attributes provide essential information to the AI, enabling it to make informed decisions about which action to take and which method/function to call, and passing in the values of each parameter within the Unity game.
The Tool
attribute is used to define a tool, providing its name and description.
using Indie.Attributes;
[Tool("MyMethod", "Description of the tool.")]
public void MyMethod()
{
// Method implementation
}
The Parameter
attribute defines parameters for a method, including their name, description, and optional list of enums.
using Indie.Attributes;
public class MyClass
{
[Tool("MyMethod", "Description of the tool.")]
[Parameter("myParameter", "Description of the parameter.")]
public void MyMethod(int myParameter)
{
// Method implementation
}
}
using Indie.Attributes;
public class MyClass
{
[Tool("MyMethod", "Description of the tool.")]
[Parameter("param1", "Description of the parameter.")]
[Parameter("param2", "Description of the parameter.", "option1", "option2", "option3")]
public void MyMethod(int param1, string param2)
{
// Method implementation
}
}
To use the Unity Function Calling Framework in your Unity project, follow these simple steps:
-
Download the Framework: Download the framework files and add them to your Unity project.
-
Implement Attributes: Add
Tool
andParameter
attributes to the methods in your scripts to define tools and parameters. -
Call Functions: Let the AI make decisions based on defined tools and parameters, and call functions within your game accordingly.
using UnityEngine;
using Indie.Attributes;
public class MyGameController : MonoBehaviour
{
public Brain brain;
private void OnEnable()
{
brain?.RegisterScript(GetType(), this);
}
private void OnDisable()
{
brain?.UnRegisterScript(GetType());
}
[Tool("MovePlayer", "Move the player to a specified position.")]
[Parameter("position", "Position to move the player to.")]
public void MovePlayer(Vector3 position)
{
// Method implementation
}
// Other methods...
}