How to get size property like Vector 3?

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));

now i do manualy like these:


private SIZE_OF_TYPE GetCountByProperty(SerializedProperty property)
{
    if (property.propertyType == SerializedPropertyType.Vector3)
    {
        return SIZE_OF_TYPE.Vector3;
    }
    return SIZE_OF_TYPE.Default;
}

 enum SIZE_OF_TYPE { 
    Default = 0,
    Vector3 = 3,
}

but how to get it automatically (doesn’t define one by one by self)?

GameObject.Transform.LocalScale
Should work.

Try the Type.GetFields() and Type.GetProperties() methods. You’ll need to use System and System.Reflections namespaces.

Type myType = typeof(Vector3);
FieldInfo[] fields = myType.GetFields();
PropertyInfo[] properties = myType.GetProperties();
int amountOfFieldsVector3 = fields.Length;
int amountOfPropertiesVector3 = properties.Length;

Not sure if that will work though, because the Vector3 is not a class but a structure.
Info about GetFields(): Type.GetFields Method (System) | Microsoft Learn

About GetProperties(): Type.GetProperties Method (System) | Microsoft Learn

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).