StringToStringEvent.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. using Meta.WitAi.Attributes;
  9. using UnityEngine;
  10. using Utilities;
  11. namespace Meta.WitAi.Utilities
  12. {
  13. [AddComponentMenu("Wit.ai/Utilities/Conversions/String to String")]
  14. public class StringToStringEvent : MonoBehaviour
  15. {
  16. [Tooltip("The string format string that will be used to reformat input strings. Ex: I don't know how to respond to {0}")]
  17. [SerializeField] private string _format;
  18. [Space(WitRuntimeStyles.HeaderPaddingTop)]
  19. [TooltipBox("Triggered when FormatString(float) is called. The string in this event will be formatted based on the format field.")]
  20. [SerializeField] public StringEvent onStringEvent = new StringEvent();
  21. /// <summary>
  22. /// Trigger an onStringEvent with a provided format.
  23. /// </summary>
  24. /// <param name="format">The string format to use in the event</param>
  25. /// <param name="value">The value that will get populated in {0} in the format string.</param>
  26. public void FormatString(string format, string value)
  27. {
  28. if (string.IsNullOrEmpty(format))
  29. {
  30. onStringEvent?.Invoke(value);
  31. }
  32. else
  33. {
  34. onStringEvent?.Invoke(string.Format(format, value));
  35. }
  36. }
  37. /// <summary>
  38. /// Trigger an onStringEvent with the built in format value
  39. /// </summary>
  40. /// <param name="value">The text to insert into {0} in the format value.</param>
  41. public void FormatString(string value)
  42. {
  43. FormatString(_format, value);
  44. }
  45. }
  46. }