ActionBase.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace UnityTest
  7. {
  8. public abstract class ActionBase : ScriptableObject
  9. {
  10. public GameObject go;
  11. public string thisPropertyPath = "";
  12. public virtual Type[] GetAccepatbleTypesForA()
  13. {
  14. return null;
  15. }
  16. public virtual int GetDepthOfSearch() { return 2; }
  17. public virtual string[] GetExcludedFieldNames()
  18. {
  19. return new string[] { };
  20. }
  21. public bool Compare ()
  22. {
  23. var objVal = GetPropertyValue(go,
  24. thisPropertyPath, GetAccepatbleTypesForA());
  25. return Compare(objVal);
  26. }
  27. protected abstract bool Compare (object objVal);
  28. protected object GetPropertyValue(GameObject gameObject, string path, Type[] acceptableTypes)
  29. {
  30. var objVal = PropertyResolver.GetPropertyValueFromString(gameObject,
  31. path);
  32. if (acceptableTypes != null && !acceptableTypes.Contains(objVal.GetType(), new IsTypeComparer()))
  33. Debug.LogWarning(gameObject.GetType() + "." + thisPropertyPath + " is not acceptable type for the comparer");
  34. return objVal;
  35. }
  36. public object GetPropertyValue()
  37. {
  38. return PropertyResolver.GetPropertyValueFromString(go,
  39. thisPropertyPath);
  40. }
  41. private class IsTypeComparer : IEqualityComparer<Type>
  42. {
  43. public bool Equals(Type x, Type y)
  44. {
  45. return x.IsAssignableFrom(y);
  46. }
  47. public int GetHashCode(Type obj)
  48. {
  49. return obj.GetHashCode();
  50. }
  51. }
  52. public virtual Type GetParameterType() { return typeof(object); }
  53. public virtual string GetConfigurationDescription ()
  54. {
  55. string result = "";
  56. foreach (var prop in GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
  57. .Where (info => info.FieldType.IsSerializable))
  58. {
  59. var value = prop.GetValue (this);
  60. if (value is double)
  61. value = ((double)value).ToString("0.########");
  62. if (value is float)
  63. value = ((float)value).ToString("0.########");
  64. result += value + " ";
  65. }
  66. return result;
  67. }
  68. public ActionBase CreateCopy ()
  69. {
  70. var newObj = CreateInstance (GetType ()) as ActionBase;
  71. var fields = GetType ().GetFields (BindingFlags.Public | BindingFlags.Instance);
  72. foreach (var field in fields)
  73. {
  74. var value = field.GetValue (this);
  75. field.SetValue (newObj, value);
  76. }
  77. return newObj;
  78. }
  79. }
  80. public abstract class ActionBaseGeneric<T> : ActionBase
  81. {
  82. protected override bool Compare(object objVal)
  83. {
  84. return Compare ((T) objVal);
  85. }
  86. protected abstract bool Compare(T objVal);
  87. public override Type[] GetAccepatbleTypesForA()
  88. {
  89. return new[] { typeof(T) };
  90. }
  91. public override Type GetParameterType ()
  92. {
  93. return typeof(T);
  94. }
  95. }
  96. }