123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using UnityEngine;
- namespace UnityTest
- {
- public abstract class ActionBase : ScriptableObject
- {
- public GameObject go;
- public string thisPropertyPath = "";
- public virtual Type[] GetAccepatbleTypesForA()
- {
- return null;
- }
- public virtual int GetDepthOfSearch() { return 2; }
-
- public virtual string[] GetExcludedFieldNames()
- {
- return new string[] { };
- }
- public bool Compare ()
- {
- var objVal = GetPropertyValue(go,
- thisPropertyPath, GetAccepatbleTypesForA());
- return Compare(objVal);
- }
- protected abstract bool Compare (object objVal);
- protected object GetPropertyValue(GameObject gameObject, string path, Type[] acceptableTypes)
- {
- var objVal = PropertyResolver.GetPropertyValueFromString(gameObject,
- path);
- if (acceptableTypes != null && !acceptableTypes.Contains(objVal.GetType(), new IsTypeComparer()))
- Debug.LogWarning(gameObject.GetType() + "." + thisPropertyPath + " is not acceptable type for the comparer");
- return objVal;
- }
- public object GetPropertyValue()
- {
- return PropertyResolver.GetPropertyValueFromString(go,
- thisPropertyPath);
- }
- private class IsTypeComparer : IEqualityComparer<Type>
- {
- public bool Equals(Type x, Type y)
- {
- return x.IsAssignableFrom(y);
- }
- public int GetHashCode(Type obj)
- {
- return obj.GetHashCode();
- }
- }
- public virtual Type GetParameterType() { return typeof(object); }
- public virtual string GetConfigurationDescription ()
- {
- string result = "";
- foreach (var prop in GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
- .Where (info => info.FieldType.IsSerializable))
- {
- var value = prop.GetValue (this);
- if (value is double)
- value = ((double)value).ToString("0.########");
- if (value is float)
- value = ((float)value).ToString("0.########");
- result += value + " ";
- }
- return result;
- }
- public ActionBase CreateCopy ()
- {
- var newObj = CreateInstance (GetType ()) as ActionBase;
- var fields = GetType ().GetFields (BindingFlags.Public | BindingFlags.Instance);
- foreach (var field in fields)
- {
- var value = field.GetValue (this);
- field.SetValue (newObj, value);
- }
- return newObj;
- }
- }
- public abstract class ActionBaseGeneric<T> : ActionBase
- {
- protected override bool Compare(object objVal)
- {
- return Compare ((T) objVal);
- }
- protected abstract bool Compare(T objVal);
- public override Type[] GetAccepatbleTypesForA()
- {
- return new[] { typeof(T) };
- }
- public override Type GetParameterType ()
- {
- return typeof(T);
- }
- }
- }
|