Comment on page
To understand the script in even more depth, please check out the comments in the script.
ConvaiChatUIHandlerclass is a versatile component in Unity that facilitates the creation of various types of chat-based user interfaces (UIs). It enables characters and users to exchange messages, and it can be configured for different UI styles, such as subtitles, question-answer interfaces, and chat boxes.
ConvaiChatUIHandler, ensure that:
- The GameObject containing this script is set up with the necessary UI components (e.g., TextMeshProUGUI for displaying text).
- The character and user names are defined, and default texts are provided.
- The chat UI type (
UIType) is selected according to the desired style (Subtitle, QuestionAnswer, or ChatBox).
characterName: Display name of the character.
characterText: Default text of the character.
characterTextColor: Color of the character's text.
userName: Display name of the user.
userText: Default text of the user.
userTextColor: Color of the user's text.
userTalkingMarker: GameObject active when the user is talking.
userTextField: TextMeshProUGUI component for displaying the user's text.
characterTextField: TextMeshProUGUI component for displaying the character's text.
chatUIActive: Indicates whether the chat UI is currently visible.
isCharacterTalking: Indicates whether the character is currently talking.
isUserTalking: Indicates whether the user is currently talking.
uIType: Specifies the type of UI to use (Subtitle, QuestionAnswer, or ChatBox).
- Description: This method finds the necessary game objects based on the selected
uITypeduring Awake. It sets up references to UI elements.
- Description: On Start, this method sets default values for character and user names if not already provided.
- Description: In Update, this method refreshes the UI based on the current state. It updates the user's text, character's text, and user talking marker visibility according to the selected
- Description: This method processes text coming from the character based on the selected
uIType. It updates the character's text with the provided text.
- Description: This method processes text coming from the user based on the selected
uIType. It updates the user's text with the provided text.
- Description: This private method handles the display of the character's message in the chat box UI. It appends messages if they are from the same character or creates new messages if necessary.
- Description: This private method handles the display of the user's message in the chat box UI. It activates the chat UI if the user is talking, appends messages if needed, and updates the last message if the user sends consecutive messages.
ConvaiChatUIHandlerand create chat-based UIs:
- 1.Attach the
ConvaiChatUIHandlerscript to the GameObject representing the chat UI.
- 2.Customize character and user names, default texts, and text colors in the Inspector.
- 3.Assign the necessary UI components (TextMeshProUGUI, GameObjects) to the corresponding fields in the Inspector.
- 4.Choose the appropriate UI type (
uIType) based on your desired chat UI style (Subtitle, QuestionAnswer, or ChatBox).
- 5.Implement logic in your game scripts to call
SendUserTextmethods to update the chat UI with character and user messages.
In a story-driven game, use the
UIType.Subtitlesetting to display character dialogues as subtitles. As the character speaks, call
SendCharacterTextto update the UI with character lines, and similarly, use
SendUserTextto display user responses.
For a chat-based interaction in a game, set up the chat UI using
UIType.ChatBox. As characters and users send messages, call
SendUserTextto update the chat box with the conversation. The chat box will display messages from multiple characters and users.
In an interactive dialog system, configure the UI as
SendCharacterTextto present character questions and
SendUserTextto display user-selected answers. This creates an engaging question-and-answer dialogue interface.