{
System.Object m_obj;
List<FieldInfo> m_observeFields = new List<FieldInfo>();
List<PropertyInfo> m_observeProperties = new List<PropertyInfo>();
List<object> m_fieldValues = new List<object>();
List<object> m_propertyValues = new List<object>();
public Observer(System.Object obj)
{
m_obj = obj;
var fields = m_obj.GetType().GetFields();
foreach (var f in fields)
{
object[] attributes = f.GetCustomAttributes(typeof(ObserveAttribute), false);
if (attributes.Length > 0)
{
m_observeFields.Add(f);
m_fieldValues.Add(f.GetValue(m_obj));
}
}
var properties = m_obj.GetType().GetProperties();
foreach (var p in properties)
{
if (!p.CanRead)
continue;
object[] attributes = p.GetCustomAttributes(typeof(ObserveAttribute), false);
if (attributes.Length > 0)
{
m_observeProperties.Add(p);
m_propertyValues.Add(p.GetValue(m_obj, null));
}
}
}
public void Update()
{
for(int i = 0; i < m_observeFields.Count; i++)
{
var m = m_observeFields[i];
object newValue = m.GetValue(m_obj);
if (!object.Equals(newValue, m_fieldValues[i]))
{
Notify("valueChanged", m_obj, m.Name);
//Debug.Log("valueChanged: " + m_obj.ToString() + " propName: " + m.Name);
m_fieldValues[i] = newValue;
}
}
for (int i = 0; i < m_observeProperties.Count; i++)
{
var p = m_observeProperties[i];
object newValue = p.GetValue(m_obj, null);
if (!object.Equals(newValue, m_propertyValues[i]))
{
Notify("valueChanged", m_obj, p.Name);
m_propertyValues[i] = newValue;
}
}
}
}
}