GameActionContextVariable.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.Core.GameActions
  8. {
  9. [System.Serializable, HideReferenceObjectPicker]
  10. public abstract class GameActionContextVariable : IComparable<GameActionContextVariable>
  11. {
  12. [ShowIf("@showEdit && canEdit"), ShowInInspector, PropertyOrder(3)] public string name { get => _name; set => _name = value; }
  13. [SerializeField, HideInInspector] private string _name = "New Variable";
  14. protected internal bool showEdit = false;
  15. [HideInInspector] public bool canEdit = true;
  16. [HideInInspector] public string typeName = "GameActionContextVariable";
  17. public virtual string GetTypeName() => "GameActionContextVariable";
  18. public virtual string GetVariableName() => "Generic Variable";
  19. [HideInInspector] public string className;
  20. public int CompareTo(GameActionContextVariable other)
  21. {
  22. if(other == null) return 1;
  23. else return 0;
  24. }
  25. public abstract T GetValue<T>(T defaultValue);
  26. public abstract void SetValue<T>(T newValue);
  27. public static GameActionContextVariableBase InvokeStringMethod(string typeName, string methodName, string data)
  28. {
  29. Type calledType = Type.GetType(typeName);
  30. GameActionContextVariableBase variable = (GameActionContextVariableBase)calledType.InvokeMember($"JSONTo{methodName}",
  31. System.Reflection.BindingFlags.InvokeMethod |
  32. System.Reflection.BindingFlags.Public |
  33. System.Reflection.BindingFlags.Static,
  34. null, null, new object[] { data });
  35. return variable;
  36. }
  37. public virtual void OnBeforeSerialize(GameActionObjectSerializer serializer) { }
  38. public virtual void OnBeforeDeserialize(GameActionObjectSerializer serializer) { }
  39. }
  40. public class GameActionContextVariableBase : GameActionContextVariable
  41. {
  42. public override T GetValue<T>(T defaultValue) => default(T);
  43. public override void SetValue<T>(T newValue) {}
  44. }
  45. }