Class UITKChat
UI Toolkit implementation of the in-world chat window: channel-coloured messages, tab-based channel filtering, rate limiting, sanitisation and the full set of per-channel message handlers.
public class UITKChat : UITKCharacterControl, IChatHelper
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourUITKChat
- Implements
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.GetComponentIndex()Component.CompareTag(TagHandle)Component.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.ToString()Object.nameObject.hideFlags
Remarks
The panel keeps a model (UITKChat.ChatMessageRecord and the tab definitions)
separate from the elements that render it. That separation is not decoration: a
UIDocument re-clones its UXML every time it is enabled, so every element this class
has ever created can be replaced out from under it, and anything held only as a
UnityEngine.UIElements.VisualElement is lost with it. Rebuilding the view from the model in
OnStarting() is what keeps a hide/show from emptying the window — and what
stops it from stacking a second copy of the welcome block and a second default tab on top
of the first every time the tree comes back.
Fields
AllowRepeatMessages
Whether repeated messages are allowed.
public bool AllowRepeatMessages
Field Value
ChannelColors
Colour mapping for each chat channel.
These are drawn on the dark chat panel, so a channel colour has to carry enough lightness to be read there. Trade was Color.black and Region was Color.blue; both are near-invisible against that background rather than merely dim.
public Dictionary<ChatChannel, Color> ChannelColors
Field Value
- Dictionary<ChatChannel, Color>
CurrentTab
The name of the currently active chat tab.
public string CurrentTab
Field Value
ErrorCodes
Error code messages mapped to their respective error keys.
public Dictionary<string, string> ErrorCodes
Field Value
MAX_LENGTH
The maximum allowed length for chat messages.
public const int MAX_LENGTH = 128
Field Value
Remarks
Advisory only. The server enforces its own cap (clamped to MaxTextLength) because a client is free not to apply this.
MessageRateLimit
The rate at which messages can be sent, in milliseconds.
[Tooltip("The rate at which messages can be sent in milliseconds.")]
public float MessageRateLimit
Field Value
WelcomeMessage
The welcome message displayed when the chat is initialised.
public string WelcomeMessage
Field Value
Properties
Layer
Draw order tier for this panel. See UITKPanelLayer.
protected override UITKPanelLayer Layer { get; }
Property Value
Methods
AddTab()
Adds a new chat tab to the UI, ensuring unique tab names.
public void AddTab()
EnableChatInput()
Focuses the chat input field when the chat key is pressed and no field currently has focus.
public void EnableChatInput()
GetChannelCommand(ChatChannel)
Gets the chat command delegate for the specified channel.
public ChatCommand GetChannelCommand(ChatChannel channel)
Parameters
channelChatChannelThe chat channel.
Returns
- ChatCommand
The chat command delegate.
InstantiateChatMessage(ChatChannel, string, string, Color?)
Instantiates a new chat message element and adds it to the chat view.
public void InstantiateChatMessage(ChatChannel channel, string name, string message, Color? color = null)
Parameters
channelChatChannelThe chat channel.
namestringThe sender's name, already resolved. Empty for no sender column.
messagestringThe message text.
colorColor?Optional colour override.
OnAfterStarting()
Re-applies state after the visual tree was rebuilt.
protected override void OnAfterStarting()
Remarks
OnStarting() already rebuilds everything from the model, so all that is left is the scroll position — which cannot be set until the rows it is measuring against have been laid out.
OnClientSet()
Registers the chat broadcast handler when the client is injected.
public override void OnClientSet()
OnClientUnset()
Unregisters the chat broadcast handler when the client is cleared.
public override void OnClientUnset()
OnDiscordChat(ChatBroadcast)
Handles Discord chat messages and displays them in the chat view.
public void OnDiscordChat(ChatBroadcast msg)
Parameters
msgChatBroadcastThe chat broadcast message.
Remarks
The name here is a Discord display name, chosen by whoever sent it and outside the game's control entirely. It is sanitised by the bridge on the way in and again by the server before broadcast, and rendered — like every other chat label — with markup parsing switched off.
OnGuildChat(IPlayerCharacter, ChatBroadcast)
Handles Guild chat messages, including error messages, and displays them in the chat view.
public bool OnGuildChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
OnPartyChat(IPlayerCharacter, ChatBroadcast)
Handles Party chat messages, including error messages, and displays them in the chat view.
public bool OnPartyChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
OnRegionChat(IPlayerCharacter, ChatBroadcast)
Handles Region chat messages and displays them in the chat view.
public bool OnRegionChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
OnSayChat(IPlayerCharacter, ChatBroadcast)
Handles Say chat messages and displays them in the chat view.
public bool OnSayChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
OnStarting()
Resolves and caches visual elements and rebuilds the window from the model.
public override void OnStarting()
Remarks
Runs again every time the visual tree is replaced (see
UITKControl.ReinitializeIfTreeReplaced), so everything it does has to be
idempotent. It used to call AddTab() and write the welcome block
unconditionally, which meant a second default tab and a second welcome banner after
every hide/show — and, because the old rows were still in the discarded tree, a
message list that grew by a full screen of content per login.
OnSubmit(string)
Handles chat message submission, including sanitisation, rate limiting and broadcasting.
public void OnSubmit(string input)
Parameters
inputstring
OnSystemChat(IPlayerCharacter, ChatBroadcast)
Handles System chat messages and displays them in the chat view.
public bool OnSystemChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
OnTellChat(IPlayerCharacter, ChatBroadcast)
Handles Tell (private) chat messages, including error and relay messages, and displays them in the chat view.
public bool OnTellChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
OnTick()
Per-frame hook. Checks for chat input activation and keeps the cursor released while the player is typing.
protected override void OnTick()
Remarks
Overrides the base hook rather than declaring a private void Update(). Unity
binds only the most-derived Update, so declaring one here silently disabled
UITKControl.Update for this panel — taking PollLoseFocus and every other
control that hangs off it with it. The base class documents this trap on its own
Update; this panel was falling into it.
OnTradeChat(IPlayerCharacter, ChatBroadcast)
Handles Trade chat messages and displays them in the chat view.
public bool OnTradeChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
OnWorldChat(IPlayerCharacter, ChatBroadcast)
Handles World chat messages and displays them in the chat view.
public bool OnWorldChat(IPlayerCharacter localCharacter, ChatBroadcast msg)
Parameters
localCharacterIPlayerCharacterThe local player character.
msgChatBroadcastThe chat broadcast message.
Returns
- bool
True if handled successfully.
RenameCurrentTab(string)
Renames the current chat tab if the new name is not already taken.
public bool RenameCurrentTab(string newName)
Parameters
newNamestringThe new name for the tab.
Returns
- bool
True if renamed successfully, false otherwise.
SetInputText(string)
Sets the chat input field text.
public void SetInputText(string input)
Parameters
inputstringText to set in the input field.
ToggleChannel(ChatChannel, bool)
Toggles the active state of a chat channel in the current tab.
public void ToggleChannel(ChatChannel channel, bool value)
Parameters
channelChatChannelThe chat channel to toggle.
valueboolWhether the channel should be active.
ValidateMessages()
Updates which messages are visible based on the active tab and its channels.
public void ValidateMessages()
Remarks
Visibility only. Filtering must never destroy a message or prevent it from being created: the whole point of per-tab channel sets is that switching tabs shows a different view of the same history, and a message dropped because the tab that was active when it arrived did not want it can never appear on the tab that does.