Assign value to SerializedProperty in EditorWindow

I need to assign a property, in EditorWindow, obtained using SerializedProperty.


SerializedObject(myScript).FindProperty("myClass.myFloat").floatValue = 9.8;

Debug.Log(SerializedObject(myScript).FindProperty("myClass.myFloat").floatValue);

It works when I assign a value in PropertyDrawer.

It does not work when I assign a value in EditorWindow.

So is there a way for me to apply the change I have made to the SerializedProperty, or stop it from being reset ?

alternatively :
I would like to get my custom class from a SerializedProperty and change it there.
How would I do so ?

eg.


var whatIHave : SerializedProperty;
var whatINeed : MyCustomClass;

whatINeed = whatIHave as MyCustomClass;

whatINeed.myFloat = 9.8;

Solved it!

All I had to do was call ApplyModifiedProperties() on the SerializedObject I was changing.

PropertyDrawer does this automatically, EditorWindow does not.

Note : Same applies for EditorGUI.PropertyField() outside of a PropertyDrawer.

Hi. Ia am working on creating EditorWindow tool to automathize prefb creation procedure. The objective is simple, create EditorWindow, drop some textures into it, create prefab variant and set those images to background Image. I have several different window types and it works fine for all of them but one. Eventualy you open the created prefab and it’s background image is blank. No need even to enter play mode.
I tried testit with breakpoints and it started to work properly, but as soon as you disable the breakpoints it again generates a prefab with blank back. Debug.Logs also shown that everything is correct and neither part of code is null. To me it seems like it works but thn resets at the some point.
Here is how it looks like:

##SingleOfferWindow.cs##

prefab = CreatePrefabVariant(single_base_path, $"{offerPath}/{offerName}{dialog_prefab_name}");
            img = prefab.transform.Find("Content/Background").GetComponent<Image>();
            SetProperty(img, "m_Sprite", AssetDatabase.LoadAssetAtPath<Sprite>($"{dialogCompressedImagesPath}/{pack_bg_name}.png"));
            SetAddressableGroup(prefab, $"All/{offerName}Dialog", "RemoteProductsDialogs");

OfferWindow.cs (SingleOffer inherits from this class)##

protected void SetProperty(Object go, string propertyName, object obj) {
        SerializedObject so = new SerializedObject(go);
        var property = so.FindProperty(propertyName);
        if (property == null) {
            FFLogger.LogError($"Cannot find property {propertyName} in object: {go.name}");
            return;
        }
        so.Update();
        property.SetValue(obj);
        EditorUtility.SetDirty(go);
        so.ApplyModifiedProperties();
    }

public static void SetValue(this SerializedProperty p, object value)
    {
      switch (p.propertyType)
      {
        case SerializedPropertyType.Generic:
          Debug.LogWarning((object) "Get/Set of Generic SerializedProperty not supported");
          break;
        case SerializedPropertyType.Integer:
          p.intValue = (int) value;
          break;
        case SerializedPropertyType.Boolean:
          p.boolValue = (bool) value;
          break;
        case SerializedPropertyType.Float:
          p.floatValue = (float) value;
          break;
        case SerializedPropertyType.String:
          p.stringValue = (string) value;
          break;
        case SerializedPropertyType.Color:
          p.colorValue = (Color) value;
          break;
        case SerializedPropertyType.ObjectReference:
          p.objectReferenceValue = value as UnityEngine.Object;
          break;
        case SerializedPropertyType.LayerMask:
          p.intValue = (int) value;
          break;
        case SerializedPropertyType.Enum:
          p.enumValueIndex = (int) value;
          break;
        case SerializedPropertyType.Vector2:
          p.vector2Value = (Vector2) value;
          break;
        case SerializedPropertyType.Vector3:
          p.vector3Value = (Vector3) value;
          break;
        case SerializedPropertyType.Vector4:
          p.vector4Value = (Vector4) value;
          break;
        case SerializedPropertyType.Rect:
          p.rectValue = (Rect) value;
          break;
        case SerializedPropertyType.ArraySize:
          p.intValue = (int) value;
          break;
        case SerializedPropertyType.Character:
          p.stringValue = (string) value;
          break;
        case SerializedPropertyType.AnimationCurve:
          p.animationCurveValue = value as AnimationCurve;
          break;
        case SerializedPropertyType.Bounds:
          p.boundsValue = (Bounds) value;
          break;
        case SerializedPropertyType.Gradient:
          Debug.LogWarning((object) "Get/Set of Gradient SerializedProperty not supported");
          break;
        case SerializedPropertyType.Quaternion:
          p.quaternionValue = (Quaternion) value;
          break;
      }
    }

At the moment I have no idea whats wrong with it and why it works fine with the exact same code in other windows. Will be happy to check your suggestions if you have some.