How can i get the size or length of property like vector3?
my problem is when i do like this :
do
{
SerializedProperty property = soTarget.FindProperty(Prop.name).Copy();
// this was fine for float, string and etc that have 1 property
// but when property.type is Vector3, in the next iteration it will be error
// cause can't finding x, y, z(Properties of Vector3)
}
while (Prop.NextVisible(true));
There’s no real correct way to do this, however one way I can think would be to count the actual bytes in the struct and compare them with the size of a single element;
public static int NumberOfElements (System.Type a_ObjectType, int a_SizeOfOneElement)
{
//Use marshalling to get the size of an allocation for this type (Vector3 would be 3 * sizeof (float)
int objectSize = System.Runtime.InteropServices.Marshal.SizeOf (a_ObjectType);
//Divide by the size of a single element to get the number of elements
return objectSize / a_SizeOfOneElement;
}
...
//The Vector classes are made up of floats, so pass that as the size of an element
int elementCount = NumberOfElements (System.Type.GetType (property.type), sizeof (float));
Haven’t tested this, but I can’t think of any other way really ( @Ermiq has the most ‘correct’ method, but the Vector classes’ fields don’t necessarily map the way you might expect).