GameActionContextString.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.Core.GameActions
  6. {
  7. public class GameActionContextString : GameActionContextVariableBase
  8. {
  9. [HorizontalGroup("value"), LabelText("@name"), ShowInInspector, PropertyOrder(1), ReadOnly]
  10. public string value { get => this._value; set => this._value = value; }
  11. [HorizontalGroup("value", 0.14f), Button("@editButtonName"), ShowIf("@canEdit"), PropertyOrder(2)]
  12. private void EditValue()
  13. {
  14. if(showEdit == false)
  15. {
  16. showEdit = true;
  17. editButtonName = "Done";
  18. }
  19. else
  20. {
  21. showEdit = false;
  22. editButtonName = "Edit";
  23. }
  24. }
  25. private string editButtonName = "Edit";
  26. [SerializeField, ShowIf("@showEdit && canEdit"), PropertyOrder(4)] internal string _value;
  27. public override string GetTypeName() => "GameActionContextString";
  28. public override string GetVariableName() => "String";
  29. public override T GetValue<T>(T defaultValue)
  30. {
  31. var result = value;
  32. try
  33. {
  34. return (T)System.Convert.ChangeType(value, typeof(T));
  35. }
  36. catch
  37. {
  38. return defaultValue;
  39. }
  40. }
  41. public override void SetValue<T>(T newValue)
  42. {
  43. var result = value;
  44. try
  45. {
  46. value = (string)System.Convert.ChangeType(value, typeof(T));
  47. }
  48. catch
  49. {
  50. Debug.LogError("Error trying to covert T value to string");
  51. }
  52. }
  53. public static GameActionContextString JSONToGameActionContextString(string data)
  54. {
  55. return JsonUtility.FromJson<GameActionContextString>(data);
  56. }
  57. }
  58. }