0
\$\begingroup\$

I wrote this code:

var waveOrig = element.FindPropertyRelative("origin");
waveOrig.vector2Value = EditorGUI.Vector2Field(dirRect, "Point of Origin", waveOrig.vector2Value);

However, Unity logs errors because of this piece of code:

Mismatched types in SetValue UnityEditor.SerializedProperty:set_vector2Value (UnityEngine.Vector2)

Mismatched types in GetValue - return value is junk UnityEditor.SerializedProperty:get_vector2Value ()

Not sure how should I fix this, any help?

PS. "origin" element is a float2 type

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Are you aware that Vector2 from the Unity core API and float2 from the Mathematics package are not the same thing? \$\endgroup\$
    – Philipp
    Commented Dec 1, 2021 at 11:36

1 Answer 1

1
\$\begingroup\$

If you just want to allow a user to modify the value, you can use the generic PropertyField() to automatically use the editor's default representation for whatever field type it happens to be:

EditorGUI.PropertyField(dirRect, waveOrigin, new GUIContent("Point of Origin"));

But if you want to extract the value they set and read / modify it, then it would be useful to give yourself a couple helper methods:

public static class SerializedPropertyExtensions {
    public static Vector2 GetFloat2AsVector(this SerializedProperty property) {
        Vector2 output;
        var p = property.Copy();
        p.Next(true);
        output.x = p.floatValue;
        p.Next(true);
        output.y = p.floatValue;
        return output;
    }

    public static void SetFloat2FromVector(this SerializedProperty property, Vector2 value) {        
        var p = property.Copy();
        p.Next(true);
        p.floatValue = value.x;
        p.Next(true);
        p.floatValue = value.y;        
    }
}

Then you can work with the Vector2Field as you were trying to do:

var v = waveOrigin.GetFloat2AsVector();
v = EditorGUI.Vector2Field(dirRect, "Point of Origin", v);
waveOrigin.SetFloat2FromVector(v);

Don't forget to call serializedObject.ApplyModifiedProperties() to commit the changes the user's made to the object's data.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .