ChatWindow
Message display area for the chat UI. Binds to a ChatChannel, listens for new messages, and delegates rendering to a ChatScrollRect. Manages background visibility with animated fade transitions tied to the ChatHUD focus state.
Definition
Namespace: Paragon.Townskeep.ChatSystem.HUD
Assembly: Townskeep.dll
public class ChatWindow : ParagonUIBehaviourInheritance: SerializedMonoBehaviour → ParagonBehaviour → ParagonUIBehaviour → ChatWindow
Remarks
ChatWindow is the bridge between the data model (ChatChannel) and the virtualized display (ChatScrollRect). When a channel is set via SetChannel(), the window:
Unsubscribes from the previous channel's
MessageSenteventClears the scroll rect
Subscribes to the new channel's
MessageSenteventPushes all existing messages to the scroll rect
Focus-Driven Background Fade
The window has a background CanvasGroup (found via transform.Find("Background")) that fades in/out based on the ChatHUD focus state:
On focus — Fades background alpha to
1.0over1.5svia DOTweenOn unfocus — Waits
2.0s(viaYield.WaitForSeconds), then fades to0.0over1.5sif the chat is still unfocused
The background starts hidden (alpha = 0) in Awake().
Message Access
ChatWindow exposes GetMessage(Index) and GetAllMessages() which delegate directly to the bound ChatChannel. These are used by ChatScrollRect during virtualized line cycling.
Quick Lookup
Initialize
window.Initialize(chatHUD)
Switch to a channel
window.SetChannel(channel)
Get a message by index
window.GetMessage(^1) (newest)
Get all messages
window.GetAllMessages()
Read message count
window.MessageCount
Read active channel
window.Channel
Properties
ID
int
Window identifier
Name
string
Name of the currently bound channel (null-safe)
Channel
ChatChannel
The currently bound chat channel
MessageCount
int
Number of messages in the current channel (0 if no channel bound)
Methods
Initialize
Sets up the window with its parent ChatHUD. Initializes the child ChatScrollRect and subscribes to focus/unfocus events for background fading.
chatHUD
ChatHUD
The parent chat HUD coordinator
SetChannel
Switches the window to display messages from a different chat channel. Clears existing messages and loads the new channel's history.
channel
ChatChannel
The channel to bind to (can be null to unbind)
Behavior:
Unsubscribes
OnMessageSentfrom the previous channel (if any)Clears the scroll rect
If
channelis not null: subscribes toMessageSentand pushes all existing messages
GetMessage
Retrieves a message from the current channel by index. Supports C# Index with hat (^) operator for reverse indexing.
index
Index
Message index (e.g., ^1 for newest)
Returns: The ChatMessage at the specified index.
GetAllMessages
Returns all messages from the current channel, or an empty enumerable if no channel is bound.
Returns: All messages in the channel, or Enumerable.Empty<ChatMessage>() if channel is null.
Common Pitfalls
Background child must be named "Background"
The background CanvasGroup is found via transform.Find("Background"). If the child GameObject is named differently, a NullReferenceException occurs in Awake().
SetChannel(null) leaves the window in an unbound state
Passing null clears the scroll rect and unsubscribes from events, but MessageCount returns 0 and GetAllMessages() returns empty. GetMessage() will throw because channel is null.
Unfocus fade has a 2-second async delay
OnUnfocus() uses await Yield.WaitForSeconds(2.0f) before fading. If Focus() is called during this delay, the IsFocused check on chatHUD prevents the fade. However, the async void method is still running — rapid focus/unfocus cycles will accumulate pending coroutines.
OnDestroy unsubscribes from events
The window properly unsubscribes from chatHUD.OnFocus, chatHUD.OnUnfocus, and channel.MessageSent in OnDestroy(). If chatHUD is destroyed first, the null check prevents errors.
See Also
ChatScrollRect — virtualized scroll view that renders messages
ChatTab — triggers channel switching via
ChatTabs.SelectTab()ChatTabs — tab bar that calls
SetChannel()on selectionChatHUD — root coordinator providing focus events
Chat Overview — system architecture
Last updated