FloatToStringEvent.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 System;
  9. using Meta.WitAi.Attributes;
  10. using UnityEngine;
  11. using UnityEngine.Events;
  12. using UnityEngine.Serialization;
  13. using Utilities;
  14. namespace Meta.WitAi.Utilities
  15. {
  16. [AddComponentMenu("Wit.ai/Utilities/Conversions/Float to String")]
  17. public class FloatToStringEvent : MonoBehaviour
  18. {
  19. [FormerlySerializedAs("format")]
  20. [Tooltip("The format value to be used on the float")]
  21. [SerializeField] private string _floatFormat;
  22. [Tooltip("The format of the string itself. {0} will represent the float value provided")]
  23. [SerializeField] private string _stringFormat;
  24. [Space(WitRuntimeStyles.HeaderPaddingTop)]
  25. [TooltipBox("Triggered when ConvertFloatToString(float) is called. The string in this event will be formatted based on the format fields.")]
  26. [SerializeField] private StringEvent onFloatToString = new StringEvent();
  27. /// <summary>
  28. /// Converts a float to a string using the component format values and emits an onFloatToString event.
  29. /// </summary>
  30. /// <param name="value"></param>
  31. public void ConvertFloatToString(float value)
  32. {
  33. string floatStringValue;
  34. if (string.IsNullOrEmpty(_floatFormat))
  35. {
  36. floatStringValue = value.ToString();
  37. }
  38. else
  39. {
  40. floatStringValue = value.ToString(_floatFormat);
  41. }
  42. if (string.IsNullOrEmpty(_stringFormat))
  43. {
  44. onFloatToString?.Invoke(floatStringValue);
  45. }
  46. else
  47. {
  48. onFloatToString?.Invoke(string.Format(_stringFormat, floatStringValue));
  49. }
  50. }
  51. }
  52. [Serializable]
  53. public class StringEvent : UnityEvent<string> {}
  54. }