using System; using System.Collections.Generic; using TankAndHealerStudioAssets; using UnityEngine; using UnityEngine.Events; using UnityEngine.InputSystem; namespace UltimateChatBoxExample { public class CommandsExample : MonoBehaviour { [Serializable] public class Command { public string commandValue; public string messageToSend; public bool requireMessageValue; public UnityEvent unityEvent; } public UltimateChatBox chatBox; public string myUsername = "LocalPlayer"; public List Commands = new List { new Command { commandValue = "/help", messageToSend = "Available commands: /invite [PlayerName], /block [PlayerUsername], /joke" }, new Command { commandValue = "/invite", messageToSend = "Attempting to invite [{0}].", requireMessageValue = true }, new Command { commandValue = "/block", messageToSend = "Blocking [{0}] from future communication.", requireMessageValue = true }, new Command { commandValue = "/joke", messageToSend = "Why did the chicken cross the road?" } }; private void Start() { chatBox.OnInputFieldSubmitted += delegate(string message) { if (!chatBox.InputFieldContainsCommand) { chatBox.RegisterChat(myUsername, message, UltimateChatBoxStyles.boldUsername); } }; chatBox.OnInputFieldCommandSubmitted += delegate(string command, string message) { bool flag = false; for (int i = 0; i < Commands.Count; i++) { if (command == Commands[i].commandValue.ToLower()) { if (Commands[i].requireMessageValue) { if (message == string.Empty) { chatBox.RegisterChat("[SYSTEM]", "Please provide a message after the command.", UltimateChatBoxStyles.noticeUsername); } else { Commands[i].unityEvent?.Invoke(); chatBox.RegisterChat("[SYSTEM]", string.Format(Commands[i].messageToSend, message) ?? "", UltimateChatBoxStyles.errorUsername); } } else { Commands[i].unityEvent?.Invoke(); chatBox.RegisterChat("[SYSTEM]", Commands[i].messageToSend ?? "", UltimateChatBoxStyles.errorUsername); } flag = true; break; } } if (!flag && !chatBox.InputFieldEnabled) { chatBox.RegisterChat("[SYSTEM]", "Unknown Command", UltimateChatBoxStyles.errorMessage); } }; } private void Update() { if (InputSystem.GetDevice().slashKey.wasPressedThisFrame) { chatBox.EnableInputField("/"); } } } }