I am using the following line from Reflection to get all the fields in my object:
FieldInfo[] l_Fields = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
It works fine for all my fields but ignores this one completely:
private OffSet m_Offest;
'Offset' being a struct made as such:
public struct OffSet
{
public float x;
public float y;
}
This field is not in the returned array, is there any way to get it?
Here is the definition of the class. I am getting the fields in the last two functions.
[Serializable]
public abstract class AITreeNode : ISerializable
{
//>----------------------------------------------------------------------------------------
// STRUCT
//-----------------------------------------------------------------------------------------
public struct OffSet
{
public float x;
public float y;
}
//>----------------------------------------------------------------------------------------
// ENUM
//-----------------------------------------------------------------------------------------
public enum Status
{
None,
Available,
Unavailable,
Active,
Success,
Failed
}
//>-----------------------------------------------------------------------------------------
// VARIABLES
//------------------------------------------------------------------------------------------
public String Caption;
public bool Enabled = true;
// Display
private OffSet m_Offest;
// Non Serialized data
[NonSerialized] protected AITree m_AITreeAT;
[NonSerialized] protected AITreeBranch m_BranchATB;
[NonSerialized] protected Status m_LastStatusS = Status.None;
[NonSerialized] protected bool m_IsActiveB;
//>-----------------------------------------------------------------------------------------
// GETTERS
//------------------------------------------------------------------------------------------
public AITreeBranch GetBranchATB() { return m_BranchATB; }
public bool IsActive() { return m_IsActiveB; }
public Status LastStatusS() { return m_LastStatusS; }
//>-----------------------------------------------------------------------------------------
// SETTERS
//------------------------------------------------------------------------------------------
public void SetBranch ( AITreeBranch _BranchATB ) { m_BranchATB = _BranchATB; }
public void SetAITree ( AITree _TreeAT ) { m_AITreeAT = _TreeAT; }
//>-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
public float X
{ get { return m_Offest.x; } set { m_Offest.x = value; } }
//>-----------------------------------------------------------------------------------------
public float Y
{ get { return m_Offest.y; } set { m_Offest.y = value; } }
//>-----------------------------------------------------------------------------------------
//>-----------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
public AITreeNode()
{
m_Offest.y = -5;
}
#region Serialization
//>-----------------------------------------------------------------------------------------
// The special constructor is used to deserialize values.
// Every class inheriting from AITreeNode needs to implement a constructor with such parameters
//------------------------------------------------------------------------------------------
public AITreeNode( SerializationInfo info, StreamingContext context )
{
// Reset the property value using the GetValue method.
FieldInfo[] l_FieldsAFI = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
foreach ( FieldInfo fieldInfo in l_FieldsAFI )
{
if ( fieldInfo.IsNotSerialized ) continue;
try
{
fieldInfo.SetValue( this, info.GetValue( fieldInfo.Name, fieldInfo.FieldType ) );
}
catch
{
UnityEngine.Debug.Log( "Field " + fieldInfo.Name + " is new. Default value is used" );
}
}
}
//>-----------------------------------------------------------------------------------------
// Implement this method to serialize data. The method is called on serialization.
//------------------------------------------------------------------------------------------
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Use the AddValue method to specify serialized values.
FieldInfo[] l_FieldsAFI = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
foreach ( FieldInfo fieldInfo in l_FieldsAFI )
{
if ( fieldInfo.IsNotSerialized )
{
UnityEngine.Debug.Log( "Non serialized Field " + fieldInfo.Name );
continue;
}
info.AddValue( fieldInfo.Name, fieldInfo.GetValue( this ), fieldInfo.FieldType );
UnityEngine.Debug.Log( "Saving Field " + fieldInfo.Name );
}
}
... rest of the class
}